Java object-oriented features three: Polymorphism

Polymorphism: a variety of phenotypes can be understood as a form of things

Overloading and override method 1, method

2, the target polymorphism --- can be applied directly on the abstract classes and interfaces

3, polymorphism subclass object

 

    Person p2=new Man();

      Premise polymorphism using subclass object: 1) have a class inherits 2) there must be a subclass of a parent class rewrite

4, the program runs into the compiler and operating states

      For the polymorphism, the compiler, the "look left" to see this reference variable understand the type of parent class

       Running, "Look to the right", focused on the real physical object,

 

 

Java reference variable has two types: type type compile time and run

 Compile-time type type used when the variable is declared by the decision, run-time type is determined by the actual target assigned to the variable.

 

5, property no polymorphism

      

P2 = new new Man the Person (); 
// virtual method call by reference parent class object entity subclass point, when invoked, actual implementation is a subclass of the parent class = rewritten. 
p2.eat (); 
p2.walk (); 
//p2.entertainment (); 
will complain // compile time, this person is no defined method 
// at compile time, p2 is considered 
System.out.println ( " whose property "+ p2.id); // who is this property 142 
// this is the parent class Person id, not Man, attributes no polymorphism

 

Code:

public class Person {
    private String name = "oooXXX";
    private String sex;
    private int age;
    int id = 142;//身份证

    public Person() {
        //super();//有没有都行,会默认自动调用
        System.out.println("这是person的无参构造器!!");
    }



    public Person(String sex){
        this();
        this.sex=sex;
        System.out.println("这是person的有参构造器");
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public String getSex() {
        return sex;
    }

    public int getAge() {
        return age;
    }

    public void eat() {
        System.out.println("吃饭喽!!!");
    }

    public void walk() {
        System.out.println("走起来!!!");
    }

    public void sleep() {
        System.out.println("人在睡觉!!!");
    }

}
Man the extends the Person class {public 
    Private Smoke Boolean; 
    int ID = 1001; 
    public Man () { 
        Super (); 
        System.out.println ( "This is the constructor with no arguments Man"); 
    } 
    public Man (Boolean Smoke) { 
        Super (); 
        this.smoke = Smoke; 
        System.out.println ( "this is the argument constructor method Man"); 
    } 
    public void setSmoke (Boolean Smoke) { 
        this.smoke Smoke =; 
    } 
    public Boolean getSmoke () { 
        Smoke return; 
    } 
    // methods override 
    public void eAT () { 
        System.out.println ( "man eat !!!"); 
    }
    }
 
    public void Walk () {
        System.out.println ( "walking man !!!");
    Entertainment void public () { 
        System.out.println ( "Man dinners"); 
    } 

}
 
public class Woman extends Person {
    private boolean beauty;

    public Woman() {
        super();
        System.out.println("这是Woman的无参构造方法");
    }

    public Woman(boolean beauty) {
        super();
        this.beauty = beauty;
        System.out.println("这是Woman的有参构造方法");
    }

    public void setBeauty(boolean smoke) {
        this.beauty = smoke;
    }

    public boolean getSmoke() {
        return beauty;
    }
    //方法重写

    public void eat() {
        System.out.println("女人吃饭!!!");
    }

    public void walk() {
        System.out.println("女人走路!!!");
    }

    public void shopping(){
        System.out.println("女人爱购物");
    }

}
{class DuotaiTeat public 
    public static void main (String [] args) { 
        the Person new new P1 = the Person (); 
        p1.eat (); 
        p1.walk (); 

        Man Man new new M1 = (); 
        m1.eat (); 
        M1 .walk (); 
        // child references polymorphism parent class object points to the class object is a subclass of 
        the Person P2 = new new Man (); 
        // virtual method invocation, a reference point subclass object entity through the parent class when when calling, a method to rewrite the parent class = subclass actually performed. 
        p2.eat (); 
        p2.walk (); 
        //p2.entertainment (); 
        will complain // compile time, this person is no defined method 
        // at compile time, p2 is considered 
        System.out.println ( " whose property "+ p2.id); // who is this property 142 
        // this is the parent class Person id, not Man, attributes no polymorphism 

        Person M2 = new new Woman (); 
       // P2 .shopping ();
        Woman w = (Woman) m2; // downcast, with a strong character conversion 
        w.shopping (); 

        // Woman m11 = (Woman) M1; 
        // error, can not convert a man to a woman 
        System.out.println ( "-------"); 
        IF (M2 the instanceof Woman) { 
            Woman WW = (Woman) M2; // downward transition, using a strong conversion character 
            ww.shopping (); 
        } 
        // the instanceof Object class 1 1 : determining whether the object is an instance of a class 1 is then return true; otherwise to false 
        // if a is an instance of class a, it must also be a class of the instance of the parent class a. 
        IF (M2 the instanceof Man) { 
            Man WW = (Man) M2; // downward transition, using a strong conversion character 
            ww.entertainment (); 
        } 

        IF (M2 the instanceof the Person) { 
            System.out.println ( "if A is a instance of a class, then a must be the instance of the parent class of class a "); 
        } 







    } 
}

 

result:

This is a person, no arguments constructor! !
Eat myself! ! !
To go up! ! !
This is a person, no arguments constructor! !
This is Man's no-argument constructor
man eat! ! !
Man walking! ! !
This is a person, no arguments constructor! !
This is Man's no-argument constructor
man eat! ! !
Man walking! ! !
Whose property 142
This is a no-argument constructor person! !
This is a constructor with no arguments Woman
Women love shopping
-------
women love shopping
, if A is an instance of a class, then, must be the instance of a parent class A class.

 

 

Published 45 original articles · won praise 8 · views 5847

Guess you like

Origin blog.csdn.net/wenyunick/article/details/104297576