多态

多态

多态

  • 即同一方法可以根据发送对象的不同而采用多种不同的行为方式。
  • 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多
  • 多态存在的条件
    • 有继承关系
    • 子类重写父类方法
    • 父类引用指向子类对象
  • 注意:多态是方法的多态,属性没有多态性。
  • instanceof (类型转换)引用类型,判断一个对象是什么类型,判断有无父子关系

多态注意事项:

1.多态是方法的多态,属性没有多态
2.父类和子类,有联系,否则会出现类型转换异常! ClassCastException !
3.存在条件:继承关系,方法需要重写,父类引用指向子类对象!Father f1 = new Son();

以下3种无多态:

  1. static方法,属于类,它不属于实例
  2. final常量;
  3. private方法;|
package com.oop.demo06;

public class Person {
    public void run(){
        System.out.println("run");
    }
}
===========
    package com.oop.demo06;

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


import com.oop.demo06.Person;
import com.oop.demo06.Student;

public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        //
        //
        //可以指向的引用类型不确定:父类的引用指向子类
        Student s1=new Student();//Student(子类)能调用的方法都是自己的,或者继承父类的
        Person s2 = new Student();//Person父类,可以指向子类,但是不能调用子类独有的方法
        Object s3 = new Student();
        Person s4 = new Person();

        s2.run();//son
        s1.run();//son
        s4.run();//run
        //s2.eat();父类没有,子类有,父类不能调用
        //对象能够执行哪些方法,主要看左边的类型,和右边关系不大
    }
}


注意点

1.父类引用指向子类的对象
2.把子类转换为父类,向上转型;
3.把父类转换为子类,向下转型;强制转换

4.方便方法的调用,减少重复的代码!简洁
抽象:封装、继承、多态!

package com.oop.demo06;

public class Person {
    public void run(){
        System.out.println("run");
    }
}
==========
  package com.oop.demo06;

public class Teacher extends Person{
}
============
  package com.oop.demo06;

public class Student extends Person{
    public void go(){
        System.out.println("go");
    }
}
=============
   package com.oop;


import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;

public class Application {
    public static void main(String[] args) {
        //类型之间的转换:父  子   ,高转低需要强转,父》》子
        //高            低
        Person a=new Student();
        //Student将这个对象a转换为Student,我们就可以使用Student类型的方法了
        Student a1 = (Student) a;
        a1.go();
        //子》》父,Person a2=a,子转换到父类,可能丢失自己本来的一些方法
    }
}
/*
   //Object>String
        //Object>Person>Teacher
        //Object>Person>Student
        Object object = new Student();
        System.out.println(object instanceof Student);//ture
        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);//ture
        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);//ture
        System.out.println(student instanceof Person);//true
        System.out.println(student instanceof Object);//true
       // System.out.println(student instanceof Teacher);//编译报错,只有父子关系,没有兄弟关系
       // System.out.println(person instanceof String);//编译报错
 */ 
posted @   新至所向  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示