多态

多态和Instanceof类的理解

多态

同一方法可以根据发送对象的不同而采用多种不同的

在这里插入图片描述

父类

package 多态.demo;

public class person {
    public void run(){
        System.out.println("run");
    }

}
/*
多态注意事项
1.多态是方法的多态,属性没有多态
2.父类与子类,有联系  类型转换异常 ClassCastException
3.存在条件:继承关系,方法需要重写,父类引用指向子类对象   父类 f=new 子类();
    1.static  方法,属于类,它不属于实例
    2.final   常量  在常量池
    3.private方法
    这些没有多态
 */

main方法

public static void main(String[] args) {
    student s1= new student();
        person s2=new student();
        Object s3 = new student();
        ((student)s2).eat();//子类重写了父类的方法,执行子类的方法
        s1.eat();
        s1.run();
        s2.run();
}
/*
多态注意事项
1.多态是方法的多态,属性没有多态
2.父类与子类,有联系  类型转换异常 ClassCastException
3.存在条件:继承关系,方法需要重写,父类引用指向子类对象   父类 f=new 子类();
    1.static  方法,属于类,它不属于实例
    2.final   常量  在常量池
    3.private方法
    这些没有多态
*/

子类

public class student extends person{
      public void run(){
          System.out.println("son");
      }
   
}

Instanceof类

instanceof类主要结果为true或false

引用类型 判断一个对象是什么类型

main方法

package 多态.demo;

public class app {
    public static void main(String[] args) {
        //object > person > student
        //object > person > teacher
        //object > string
       // System.out.println(x instanceof y);能不能编译成功
        Object s = new student();
        System.out.println(s instanceof student);//true
        System.out.println(s instanceof person);//true
        System.out.println(s instanceof teacher);//false
        System.out.println(s instanceof Object);//true
        System.out.println(s instanceof String);//false
        System.out.println("========================");
        person s1 = new student();
        System.out.println(s1 instanceof student);//true
        System.out.println(s1 instanceof person);//true
        System.out.println(s1 instanceof teacher);//false
        System.out.println(s1 instanceof Object);//true
        System.out.println("==============================");
        student s2 = new student();
        System.out.println(s2 instanceof student);//true
        System.out.println(s2 instanceof person);//true
        System.out.println(s2 instanceof Object);//true
    }
}

父类方法

package 多态.demo;

public class person {
    public void run(){
        System.out.println("run");
    }

}

子类1 & 子类2

package 多态.demo;

public class student extends person{


}


package 多态.demo;

public class teacher extends person {

}

继承:高转低

  public static void main(String[] args) {
        //类型之间的转化:父 子
        //高                  低
        person s = new student();
        //student将这个对象转换为student类型,我们就可以使用student类型的方法
        student s1 = (student) s;//高到低,强制转换
        s1.go();//这两句可以合成   ((student) s1).go();
        
    }
}

父类

public class person {
   

}

子类

public class student extends person{
      public void go(){
          System.out.println("go");
      }

}

低转高

main方法

   public static void main(String[] args) {
        //类型之间的转化:父 子
        //高                  低
        person s = new student();
        //student将这个对象转换为student类型,我们就可以使用student类型的方法
        student s1 = (student) s;//高到低,强制转换
        s1.go();//这两句可以合成   ((student) s1).go();
        student student = new student();
        student.go();
        person person=student;//子类转父类
        //子类转换为父类,可能丢失自己的一些方法
    }
}
/*
1,父类引用指向子类的对象
2. 把子类转换父类,向上转型;
3. 把父类转换子类,向下转型;需要强制转换
4. 方便方法的调用,减少重复的代码
*/

父类

public class person {
    public void run(){
        System.out.println("run");
    }

}


子类

public class student extends person{
      public void go(){
          System.out.println("go");
      }

}

子类

 class person {
    public void run(){
        System.out.println("run");
    }

}

子类

public class student extends person{
      public void go(){
          System.out.println("go");
      }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值