Home > Java > javaTutorial > What is polymorphism? Characteristics of polymorphism

What is polymorphism? Characteristics of polymorphism

PHP中文网
Release: 2017-06-21 16:52:38
Original
3077 people have browsed it

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 }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template