Java Basics 12 - Polymorphism is the characteristic of members
1. Characteristics
1, member variables.
Compilation and operation refer to the left side of the equal sign.
Overwriting only occurs on functions and has nothing to do with variables.
Fu f = new Zi();
System.out.println(f.num);//It is the parent class, the answer is 3
2, member function (non-static ).
Look at the left when compiling and the right when running.
Because member functions have overwriting characteristics.
Fu f = new Zi();//
f.show();
The output is the show method in the subclass
3, static function.
Look to the left for compilation and operation.
Static functions do not have polymorphism. Polymorphism is the polymorphism of objects, and static functions do not involve objects.
Fu f = new Zi();//
f.show();
The final output here is the content of the parent class’s show.
Zi z = new Zi();//
z.show();
The output is the show in the subclass
2. Example
1 /* 2 多态时, 3 成员的特点: 4 1,成员变量。 5 编译时:参考引用型变量所属的类中的是否有调用的成员变量,有,编译通过,没有,编译失败。 6 运行时:参考引用型变量所属的类中的是否有调用的成员变量,并运行该所属类中的成员变量。 7 简单说:编译和运行都参考等号的左边。哦了。 8 作为了解。 9 覆盖只发生在函数上,和变量没关系。 10 Fu f = new Zi(); 11 System.out.println(f.num);//是父类,答案是3 12 没根据f的值(子类对象的地址)去找,而是根据f的类型去找。 13 开发时不可能出现这样的情况,我父类有了,我子类就直接拿来用了,而且用的时候一般都已经向下转型了。 14 15 16 17 18 2,成员函数(非静态)。 19 编译时:参考引用型变量所属的类中的是否有调用的函数。有,编译通过,没有,编译失败。 20 运行时:参考的是对象所属的类中是否有调用的函数。 21 简单说:编译看左边,运行看右边。 22 23 因为成员函数存在覆盖特性。 24 Fu f = new Zi();// 25 f.show(); 26 输出的是子类里面的show方法 27 依赖的是对象,有对象才有成员函数,必须动态的绑定到指定的对象上,所以运行的时候是看子类,而编译的时候检查语 28 29 法错误,所以编译的时候检查父类,所以看父类。 30 编译检查语法错误,运行时根据引用指向的地址运行。 31 32 33 34 35 3,静态函数。 36 编译时:参考引用型变量所属的类中的是否有调用的静态方法。 37 运行时:参考引用型变量所属的类中的是否有调用的静态方法。 38 简单说,编译和运行都看左边。 39 40 其实对于静态方法,是不需要对象的。直接用类名调用即可。 41 Fu f = new Zi();// 42 f.show(); 43 这里最后输出的是父类的show里面的内容,因为静态成员不需要对象,直接是被类名指向,都指向存静态方法的方法区, 44 45 而那个里面存的就是父类的show。 46 Zi z = new Zi();// 47 z.show(); 48 这里的zi是继承fu的,show方法是静态的 49 输出的是子类里面的show 50 其实可以理解为静态函数不具备多态性,多态性是对象的多态性,然后静态函数不涉及对象 51 父类对象引用,就是指向父类的静态函数 52 子类对象引用,就是指向子类的对象函数 53 54 55 56 57 */ 58 59 class Fu 60 { 61 // int num = 3; 62 void show() 63 { 64 System.out.println("fu show"); 65 } 66 67 static void method() 68 { 69 System.out.println("fu static method"); 70 } 71 } 72 73 class Zi extends Fu 74 { 75 // int num = 4; 76 void show() 77 { 78 System.out.println("zi show"); 79 } 80 81 static void method() 82 { 83 System.out.println("zi static method"); 84 } 85 } 86 87 88 89 class DuoTaiDemo3 90 { 91 public static void main(String[] args) 92 { 93 Fu.method(); 94 Zi.method(); 95 //这个的实质是父类对象指向子类引用,就是有点像指针,f的值是子类对象的地址。 96 Fu f = new Zi();// 97 // f.method();//输出是父类的静态 98 // f.show();//编译的时候检查的是父类,运行的时候以子类为主,show被覆盖,运行的子类的show 99 //输出是子类的show,100 // System.out.println(f.num);//是父类,答案是3101 102 103 // Zi z = new Zi();104 // System.out.println(z.num);//是子类,答案是4105 }106 }
3. Memory storage analysis
Fu f = new Zi();
Fu f defines a reference, which is a pointer, on the stack.
new Zi() defines an object in the heap, but this object has some members of the parent class.
1. If you use a subclass reference to point to this object, all access will be from the subclass.
2. If you use a parent class reference to point to this object, all access will be to the parent class in this object, but the parent class functions will be overwritten, so the members will be the parent class, and the functions will be Time subclass.
It must be based on the pointer type to access the things to be accessed. Cats must eat cat food, dogs must eat dog food.
The above is the detailed content of What is polymorphism? Characteristics of polymorphism. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

This article explains Java's Remote Method Invocation (RMI) for building distributed applications. It details interface definition, implementation, registry setup, and client-side invocation, addressing challenges like network issues and security.

This article details Java's socket API for network communication, covering client-server setup, data handling, and crucial considerations like resource management, error handling, and security. It also explores performance optimization techniques, i

This article details creating custom Java networking protocols. It covers protocol definition (data structure, framing, error handling, versioning), implementation (using sockets), data serialization, and best practices (efficiency, security, mainta


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.