java study notes - Polymorphism

Characteristics of Java objects: polymorphism

Java object-oriented characteristics of the four

Package inheritance polymorphism (Abstract)

Polymorphism

The same object reflected in a number of different forms (the identity of) an act would show different effects
in order to achieve the desired effect polymorphism existing inheritance

Reflect:
  1. type of the parent class object reference points to subclass
   the Person Teacher new new P = ();
  2. This method can only call or reference attributes defined in the parent class
  3. If the method of the parent class will subclass rewritable , then the result of the execution of the retrieval method is that subclass results after rewriting
   If the parent class and the subclasses of the parent class attribute properties to perform the same name
   method If the parent class and subclass of the same name (overload) perform subclass method after rewriting
  4. If you want to call a unique subclass members
   (mandatory conversion) molding mold (upward / downward transition)
  5. molding (forced downward transition) can occur when a run abnormal
   ClassCastException abnormal shape of the mold
   If you want to avoid abnormal shape instanceof keyword can be used to determine
   the object instanceof class

Case

//动物类
public class Animal {
    public String name = "Animal的name属性";

    public void eat(){
        System.out.println("animal的吃饭方法");
    }
    public void sleep(){
        System.out.println("animal的睡觉方法");
    }
}

//人类
public class Person extends Animal{
    public String name = "person的name属性";
    public void eat(){
        System.out.println("person的吃饭方法");
    }
    public void sleep(){
        System.out.println("人类的睡觉方法");
    }
    public void talk(){
        System.out.println("人类的说话方法");
    }
}

//猪
public class Pig extends Animal{
    public String name = "pig的name属性";
    public void sleep(){
        System.out.println("猪的睡觉方法");
    }
}

//学生
public class Student extends Person {
    public String name = "student的name属性";
    public void talk(){
        System.out.println("学生遵守礼貌 应该好好说话");
    }
    public void study(){
        System.out.println("好好学习 天天向上");
    }
}

//老师
public class Teacher extends Person{
    public String name = "teacher的name属性";
    public void eat(){
        System.out.println("做老师的通常不按时吃饭");
    }
    public void teach(){
        System.out.println("做老师的独有方法 一般人不会讲课 我会");
    }
}

public class Test {
    public static void main(String[] args){
        //1.创建对象
        //这个真是的老师 体现出来的身份是一个人的身份
//        Person p = new Teacher();//自动向上转型  Teacher--->Person
//        p.eat();
//        p.sleep();
//        p.talk();
//        //如果想要调用子类独有的属性或方法
//        //需要将类型还原会真实类型    强制类型转化  造型  向上转型  向下转型
//        Teacher t = (Teacher)p;
//        t.eat();
//        t.sleep();
//        t.talk();
//        t.teach();

        Object o = new Teacher();
        o.hashCode();
        o.toString();
        System.out.println("------------------------");
        Animal a = (Animal)o;
        a.hashCode();
        a.toString();
        System.out.println(a.name);//animal的name属性
        a.sleep();//没有重写  person重写了 person的sleep
        a.eat();//执行teacher的eat方法
        System.out.println("-----------------------");
        Person p = (Person)o;
        p.hashCode();
        System.out.println(p.name);//person的name属性
        p.sleep();//人类的睡觉
        p.eat();//老师的吃饭
        p.talk();//人类的说话
        System.out.println("----------------------");
        Teacher t = (Teacher)o;
        System.out.println(t.name);//老师的name
        t.eat();//老师的吃饭
        t.sleep();//人类睡觉
        t.talk();//人类说话
        t.teach();//老师的独有方法
        System.out.println("-------------------");
        if(o instanceof Person){//对象是否属于后面类型
            System.out.println("类型匹配  可以造型");
//            Student s = (Student)o;//运行时异常 ClassCastException
//            s.study();
        }else{
            System.out.println("对不起 类型不匹配 不帮您造型啦 否则会出问题");
        }

    }
}


Published 30 original articles · won praise 0 · Views 6645

Guess you like

Origin blog.csdn.net/qq_37710756/article/details/103543710