Java面向对象11——多态

多态

 package oop.demon01.demon06;
 
 public class Application {
     public static void main(String[] args) {
         // 一个对象的实际类型是确定的
         // new Student();
         //new Person();
 
         //可以指向的引用类型不确定了: 父类的引用指向子类
 
         //Student 能调用的方法都是自己的或者继承父类的
         //Person 父类型,可以指向子类,但是不能调用子类独有的方法(private)
         Student s1 = new Student();
         Person s2 = new Student();
         Object s3=new Person();
 
         //对象执行那些方法,主要看对象左边的类型(同---执行),和右边关系不大!
         //子类重写了父类的方法,执行子类方法
         s2.run();//son
         s1.run();//son
 
         s1.eat();
         // s2.eat(); 不行 报错--- 方法未重写
         
         //s2.shout(); 不能调用子类独有的方法(private)
    }
 }
 ----------------------------
 package oop.demon01.demon06;
 
 public class Person {
 
     public void run(){
         System.out.println("run");
    }
 
 }
 -----------------------------
 package oop.demon01.demon06;
 
 public class Student extends Person{
 
     @Override
     public void run() {
         System.out.println("son");
    }
 
     public void eat(){
         System.out.println("eat");
    }
 
     private void shout(){
         System.out.println("shout");
    }
 }
 
 /*
 多态注意事项:
 1. 多态是方法的多态,属性没有多态
 2. 父类和子类,有联系 类型转换异常,父类引用指向子类对象! ClassCastException !
 3.存在条件: 继承关系,方法需要重写,父类引用子类对象 Father s1 = new Son();
 
    有些方法不能重写:
      1. static 方法,属于类,它不属于实例
      2. final 常量:
      3. private方法:
  */

instanceof 和类型转换

instanceof
 package oop.demon01.demon06;
 
 public class Application {
     public static void main(String[] args) {
         
         //Object > String
         //Object > Person > Teacher
         //Object > Person > Student
         
         Object object=new Student();
         //System.out.println(X instanceof Y);//能不能编译通过!
         
         System.out.println(object instanceof Student);//true
         System.out.println(object instanceof Person);//true
         System.out.println(object instanceof Object);//true
         System.out.println(object instanceof Teacher);//false
         System.out.println(object instanceof String);//false
         System.out.println("------------------------------------------");
         Person person=new Student();
         System.out.println(person instanceof Student);//true
         System.out.println(person instanceof Person);//true
         System.out.println(person instanceof Object);//true
         System.out.println(person instanceof Teacher);//false
        // System.out.println(person instanceof String);// 编译报错! 没有联系
         System.out.println("------------------------------------------");
         Student student=new Student();
         System.out.println(student instanceof Student);//true
         System.out.println(student instanceof Person);//true
         System.out.println(student instanceof Object);//true
         //System.out.println(student instanceof Teacher);//没有关系
         //System.out.println(student instanceof String);//没有关系
    }
 }
子类与父类的转换
 package oop.demon01.demon06;
 
 public class Application {
     public static void main(String[] args) {
         //类型之间的转化: 父 子
 
         //高               低
         Person obj=new Student();// 低---->高
         //student.go(); 报错 Person里没有go()方法
         // 将这个对象转换为 Student类型,我们就可以使用Student类型的方法! 高---->低 :强制转换
         /*
          Student student=(Student) obj;
          student.go();
          */
        ((Student) obj).go()  
             
 -----------注意://子类转换为父类,可能丢失自己本来的一些方法!Student student=new Student();
         student.go();
         Person person=student;
         //person.go(); go()方法丢失
         
    }
 }
 --------------
     package oop.demon01.demon06;
 
 public class Person {
 
     public void run(){
         System.out.println("run");
    }
 }
 --------------
     package oop.demon01.demon06;
 
 public class Student extends Person{
 
     public void go(){
         System.out.println("go");
    }
 }
 --------------
     
总结
 /*
 1.父类引用指向子类对象
 2.把子类转换为父类,向上转型
 3.把父类转换为子类,向下转换; 强制转换
 4.方便方法的调用,减少重复代码! 简介
 
 抽象: 封装、继承、多态 !     抽象类、接口
  */

 

学习内容源自视频:b站狂神说Java

 

posted @ 2021-08-01 16:32  时间最考验人  阅读(193)  评论(0编辑  收藏  举报