What is polymorphism? Characteristics of polymorphism
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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
