当前位置:网站首页>2021-05-10 method rewrite polymorphism considerations

2021-05-10 method rewrite polymorphism considerations

2022-06-23 10:07:00 Deer like deer

Method rewriting

package oop.demo6;

// Rewriting is the rewriting of methods , It has nothing to do with attributes 
public class B {
    
    public static void test(){
    
        System.out.println("B=>test()");
    }
}
package oop.demo6;

public class A extends B{
    
    public static void test(){
    
        System.out.println("A=>test()");
    }
}
package oop.demo6;

public class Application {
    
    public static void main(String[]args){
    

        // There is a big difference between static and non static methods 

        // Static methods : Method is only related to the data type defined on the left 
        // The static : rewrite 
        A a = new A();
        a.test();
        // A reference to a parent class points to a child class 
        B b = new A();// The subclass overrides the method of the parent class 
        b.test();
    }
}

rewrite : There needs to be an inheritance relationship , Subclasses override methods of the parent class

  1. Method name must be the same
  2. The parameter list must be the same
  3. Modifier : The scope can be expanded (public>Protected>Default>private)
  4. Exception thrown : The scope can be narrowed , But not to expand (ClassNotFoundException–>Exception( Big ))
    rewrite , The method of the subclass must be consistent with that of the parent class , Different methods

Why rewrite :
Functions of the parent class , Subclasses don't have to be , Or not necessarily satisfied
Alt+Insert:override

polymorphic

That is, the same method can adopt a variety of different behavior modes according to the different sending objects
The actual type of an object is certain , But there are many reference types that can point to objects

The condition of polymorphism :

  1. There is an inheritance relationship
  2. Subclass override parent method
  3. The parent class reference points to the subclass object

Be careful : Polymorphism is the polymorphism of method , Property is not polymorphic
instanceof

public class Person {
    
    public void run(){
    

    }
}
public class Student extends Person {
    
    @Override
    public void run() {
    
        System.out.println("son");
    }
}

public class Application {
    
    public static void main(String[]args){
    
        // The actual type of an object is certain 
        //new Student();
        //new Person();
        // The type of reference that can be pointed to is uncertain : A reference to a parent class points to a child class 

        //Student All methods that can be called are their own or inherited from the parent class 
        Student s1 = new Student();
        //Person Parent type , Can point to subclasses , But you can't call methods unique to subclasses 
        Person s2 = new Student();

        Object s3 = new Student();
    // The methods that an object can execute mainly depend on the type on the left of the object , It has nothing to do with the right side 

        s2.run();// The subclass overrides the method of the parent class , Then execute the subclass method 
        s1.run();
    }
}

Polymorphism considerations :
Polymorphism is the polymorphism of method , Property is not polymorphic
Parent and child classes are related , Type conversion exception :ClassCastException
Existence condition : Inheritance relationships , Method needs to be overridden

Can not rewrite ( You can't achieve polymorphism ):
static Method , Belong to category , Not an example
final Constant
private Method

原网站

版权声明
本文为[Deer like deer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230954195599.html