Java basics 12-polymorphism is a member feature, java-polymorphism Member

Source: Internet
Author: User

Java basics 12-polymorphism is a member feature, java-polymorphism Member
Java basics 12-polymorphism is a feature of Members I. Features

1. member variables.

For compilation and running, see the left side of the equal sign.

Overwriting only occurs on the function and does not matter to the variable.

Fu f = new Zi ();
System. out. println (f. num); // It is the parent class. The answer is 3.

2. member functions (non-static ).

Compile to the left and run to the right.

Because the member functions have the overwriting feature.

Fu f = new Zi ();//
F. show ();
The show method in the subclass is output.

3. Static functions.

Compile and run both on the left.

Static functions do not have polymorphism. polymorphism is the object polymorphism, and then static functions do not involve objects.

Fu f = new Zi ();//
F. show ();
The output is the content in show of the parent class.

Zi z = new Zi ();//
Z. show ();

The output is show in the subclass.

 

Ii. Instances
1/* 2 polymorphism, 3 Member features: 4 1, member variable. 5. Compile time: Check whether the referenced variable belongs to the class that contains the called member variable. If yes, the compilation succeeds. If no, the compilation fails. 6. runtime: Check whether there are called member variables in the class to which the referenced variable belongs, and run the member variables in the class. 7. for compilation and running, see the left side of the equal sign. Oh. 8. 9 override only occurs on the function, and it has nothing to do with the variable. 10 Fu f = new Zi (); 11 System. out. println (f. num); // It is the parent class. The answer is 3 12, which is not found based on the value of f (the address of the subclass object), but based on the type of f. 13 This is not possible during development. When my parent class is ready, I use the subclass directly and it has been transformed down. 14 15 16 17 18 2, member function (non-static ). 19. During Compilation: refer to the class to which the referenced variable belongs to whether there are called functions. Yes, compilation passed, no, compilation failed. 20 runtime: Check whether the called function exists in the class to which the object belongs. 21 to put it simply: Compile to the left and run to the right. 22 23 because the member functions have the overwriting feature. 24 Fu f = new Zi (); // 25 f. show (); 26 The show Method 27 in the subclass is output based on the object. Only when an object has a member function must it be dynamically bound to the specified object, therefore, when running, it is to look at the Child class, while when compiling, it is wrong to check the 28-29 method, so the parent class is checked during compilation. 30. The compilation check syntax is incorrect. The code is run according to the address pointed to by the reference. 31 32 33 34 35 3, static function. 36. During Compilation: refer to the class to which the referenced variable belongs to whether a static method is called. 37 runtime: refer to the class to which the referenced variable belongs to whether there is a static method called. 38 to put it simply, the compiling and running operations are on the left. 39 40 actually does not need objects for static methods. Use the class name directly. 41 Fu f = new Zi (); // 42 f. show (); 43 the content in show of the parent class is output at the end, because static members do not need objects and are directly directed by class names, all point to the method area for storing static methods, 44 45 and show of the parent class. 46 Zi z = new Zi (); // 47 z. show (); 48 here, the zi inherits fu. The show method is static. 49 outputs the show 50 in the subclass. In fact, it can be understood that static functions do not have polymorphism, polymorphism is the polymorphism of the object, and then static functions do not involve object 51 parent class object reference, that is, static functions pointing to the parent class 52 subclass object reference, the object function 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 // The essence of this is that the parent class object points to the subclass reference, which is a bit like a pointer. The value of f is the address of the subclass object. 96 Fu f = new Zi (); // 97 // f. method (); // The output is the static 98 of the parent class // f. show (); // The parent class is checked during compilation. When running, the primary subclass is used. show is overwritten. The running subclass's show 99 // The output is the show of the subclass, 100 // System. out. println (f. num); // is the parent class. The answer is 3101 102 103 // Zi z = new Zi (); 104 // System. out. println (z. num); // is a subclass. The answer is 4105} 106}

 

Iii. Memory storage Analysis

Fu f = new Zi ();

Fu f defines a reference in the stack, that is, a pointer.

New Zi () defines an object in the heap, but this object has the member of the parent class.

1. If you use a subclass to reference this object, All accesses the subclass.

2. If the parent class is referenced to this object, All accesses the parent class of this object, but the parent class function is overwritten, so the Member is the parent class, subclass of a function.

It must be based on the pointer type to access things to be accessed. The cat must eat the cat food before the dog eats the dog food.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.