ZJU-java Advanced Notes Week 4 (Inheritance and Polymorphism)

  1. Inheritance is one of the important characteristics of object-oriented languages. Languages ​​without inheritance can only be called "object-based languages".

  2. The subclass inherits all the members from the parent class
    ① Except for the constructor, after all, the constructor has the same name as the parent class
    ② Getting it does not mean you can use it casually
    Insert picture description here

  3. If we try to redefine a member variable that already exists in the parent class, then we are defining a variable that is completely unrelated to the member variable of the parent class. In the child class, we can access the variable defined in the child class (the parent The class is hidden), access the parent class in the method of the parent class. Although they have the same name, they do not affect each other .

  4. The constructor of the parent class will be called before the constructor of the subclass

  5. The first sentence super() of the constructor of the word class indicates which constructor of the parent class is called according to the parameters

  6. When constructing an object of a subclass, you must first ensure that the member variables owned by the parent class are properly initialized.
    Refers to: define initialization and constructor

  7. Constructor which not only have such this.title = title

  8. There is no way to change the member variables of the parent class from private to protected.
    Example: Subclass Zhongyuan

System.out.println("DVD:"+title+":"+director);

Now changed to

System.out.print("DVD:");
super.print();
System.out.print(director);

Join in the parent class

public void print(){
    
    
    System.out.print(title+" ");
}

In this way, there is no need to change the title of the parent class member from private to protected

  1. All variables that hold object types in Java are polymorphic variables. The term "polymorphism" (literally means many forms) refers to a variable that can hold objects of different types (that is, its declared type or any subtype).
Car myCar = new Car();

That is, the object of the subclass can be used as the object of the parent class, including:
① The variable assigned to the parent class ( upward modeling occurs )
(the modeling is to assign one type of object to another type of variable)
upward modeling No need to put parentheses in front, put the type in the parentheses, it is always safe

② Pass to the function that needs the parent object

③ Put it into the container that stores the parent object

public void list(){
    
    
   for(Item item:listItem){
    
    
      item.print();
   }
}

Think that every variable has two types: static and dynamic.
Static is seen. For example, the Item type here.
Dynamic is the type of object that the program runs to here and actually manages.

11. If the parent class object wants to be assigned to the subclass object, Only by modeling
example 1

Vechicle v;
Car c = new Car();
v = c;//可以
c = v;//编译报错
c = (Car) v;//向下造型,当且仅当v这个变量实际管理的是Car类型

Example 2 (Note: Item is the parent class of the CD class)

Item item = new Item("行路易知难", 0, "优秀公众号");
CD cd = new CD("深夜书店","许嵩",2, 10, "超好听");
item = cd;(item的动态类型变成cd了,静态未变)
CD cd2 = (CD)item;
  1. The assignment of objects to objects in Java actually allows two managers to manage a common object (this is the case in OOP languages)
String s = “hi”
s = “bye”

Instead of replacing hi with bye, it's just that the content managed by s has changed, originally pointing to hi, now pointing to bye

  1. Standard definition of modeling cast
    Insert picture description here

What is type conversion

int i = (int)10.2

The styling just treats you as another type, but does not transform you into another type

Upward styling is always safe

  1. Java default dynamic binding
    Binding: When an object variable calls a function, decide which one to call.
    Static binding: Determine which
    dynamic binding to bind according to the declaration (static) type of the variable: Determine the binding according to the dynamic type of the variable Which
    one calls other member functions in a member function, because it can be expressed as this..., it can also be considered as dynamic binding.
    For example, the print() function of the parent class Item is as follows
    public void print(){
    
    
        System.out.print(title+" ");
}

And the output is

CD:深夜书店 许嵩(title是”深夜书店”)

This is because the print() function of the subclass CD is as follows

    public void print() {
    
    
        System.out.print("CD:");
        super.print();
        System.out.print(artist);
}

item.print(); actually called print() of the subclass

  1. Coverage: There are functions with the same name and parameters in the subclass and the parent class, and this pair of functions constitutes a coverage relationship.
    When calling a function with an overlay relationship through the variable of the parent class, the function of the class to which the object managed by the variable belongs will be called

  2. All classes in Java, whether declared or not, are subclasses of Object (almost all OOP languages ​​implement such a single-root structure, except C++)

The Object class provides the function
toString() [returns a string representation of an object] [overloaded by generate]
equals() [compare whether two objects are the same]
...
how to get
Object o = new Object();
o. ...

Example

CD cd = new CD("奇妙能力歌","陈粒",1, 5, "好听");
CD cd1 = new CD("奇妙能力歌","陈粒",1, 5, "好听");
System.out.println(cd.equals(cd1));

Output

false

Reason: Now the CD class does not have its own equals(), it uses Object to determine whether two managers manage the same object.
Solution: Use generate to overload equals in CD, that is, add the following code

    @Override
    public boolean equals(Object obj) {
    
    
        CD cc = (CD)obj;//向下造型
        return artist.equals(cc.artist);
}

New output

true

Note: See @Override, the signature (function name and parameters) of the subclass and parent class function must be the same

Guess you like

Origin blog.csdn.net/weixin_44997802/article/details/108515120