• 多态语法详解


    一:概念

    1:同一个引用,调用了同一个方法,因为引用的对象不一样,所表现出来的行为也不一样。

    1:多态实现条件

    1:必须在继承体系下;
    2:子类必须对父类中的方法进行重写;
    3:通过父类引用调用重写的方法;

    二:重写:

    重写也称覆盖。重写是子类对父类非静态,非private,非final修饰,非构造方法等的实现过程进行重新编写。
    重写规则
    1:方法名,参数列表(参数类型,个数,顺序),返回类型都要相同,(返回类型可以构成父子类关系)。
    2:子类重写父类同名的方法时,子类方法的访问权限要大于父类的。
    3:当在父类的构造方法中,调用了子类和父类同名的方法时,此时会调用子类的方法。
    提醒: 不要在构造方法中调用重写的方法。

    class Person{
        public String name;
        public int age;
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
            fun();
        }
    
        public void fun(){
            System.out.println("父类的fun()方法");
        }
    }
    class Student extends Person{
        public Student(String name, int age) {
            super(name, age);
        }
        public void fun(){
            System.out.println("子类的fun()方法");
        }
    }
    public class Test {
        public static void main(String[] args) {
            Student student=new Student("张三",20);
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    在这里插入图片描述4:父类方法被static ,final,private修饰不能重写

    三:向上转型和向下转型

    1:向上转型:

    子类对象给到了父类对象,也可以理解为:父类引用引用的是子类对象,通过父类的引用去调用父类和子类同名的方法,不过调用的是子类的方法。(也叫作动态绑定)

    1:直接赋值:

    class Animal{
        private String name;
        private int age;
    
        public Animal(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public void eat(){
            System.out.println(this.age+"在吃饭");
        }
    }
    class Dog extends Animal{
        public Dog(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println(this.getName()+"吃狗粮");
        }
    }
    public class Test {
        public static void main(String[] args) {
            Animal animal=new Dog("旺财",3);//父类引用引用了子类对象
            animal.eat();//通过父类引用访问了和父类同名的子类方法,
    
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    在这里插入图片描述

    2:方法传参

    class Animal{
        private String name;
        private int age;
    
        public Animal(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public void eat(){
            System.out.println(this.age+"在吃饭");
        }
    }
    class Dog extends Animal{
        public Dog(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println(this.getName()+"吃狗粮");
        }
    }
    class Cat extends Animal{
    
        public Cat(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println(this.getName()+"吃猫粮");
        }
    }
    public class Test {
        public static void fun(Animal animal){
            animal.eat();//同一个引用,引用了同一个方法,因为引用的对象不一样,所表现出来的行为不一样,我们把这种思想叫做多态
        }
        public static void main(String[] args) {
          Dog dog=new Dog("旺财",3);
          fun(dog);
          fun(new Cat("喵喵",2));
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    在这里插入图片描述

    3:返回值

    作返回值,返回任意子类对象

    class Animal{
        private String name;
        private int age;
    
        public Animal(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public void eat(){
            System.out.println(this.age+"在吃饭");
        }
    }
    class Dog extends Animal{
        public Dog(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println(this.getName()+"吃狗粮");
        }
    }
    class Cat extends Animal{
    
        public Cat(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println(this.getName()+"吃猫粮");
        }
    }
    public class Test {
        public static Animal fun(){
            return new Dog("旺财",3);
        }
        public static void main(String[] args) {
          Animal animal=fun();
          animal.eat();
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    2:向下转型

    将一个子类对象经过向上转型后当成父类方法使用,再也无法调用子类特有的方法,

    class Animal{
        private String name;
        private int age;
    
        public Animal(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public void eat(){
            System.out.println(this.age+"在吃饭");
        }
    }
    class Dog extends Animal{
        public Dog(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println(this.getName()+"吃狗粮");
        }
    }
    class Cat extends Animal{
    
        public Cat(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println(this.getName()+"吃猫粮");
        }
        public void barks(){
            System.out.println(this.getName()+"摇尾巴");
        }
    }
    public class Test {
    
        public static void main(String[] args) {
         Animal animal =new Dog("旺财",3);
         animal.barks();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    在这里插入图片描述

    但有时需要调用子类特有的方法,此时:将父类引用在还原为子类对象,也就是向下转型。

    class Animal{
        private String name;
        private int age;
    
        public Animal(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public void eat(){
            System.out.println(this.age+"在吃饭");
        }
    }
    class Dog extends Animal{
        public Dog(String name, int age) {
            super(name, age);
        }
        public void barks(){
            System.out.println(this.getName()+"摇尾巴");
        }
        @Override
        public void eat() {
            System.out.println(this.getName()+"吃狗粮");
        }
    }
    class Cat extends Animal{
    
        public Cat(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println(this.getName()+"吃猫粮");
        }
    
    }
    public class Test {
    
        public static void main(String[] args) {
            Dog dog=new Dog("旺财" ,2);
            Animal animal =dog;
            dog=**(Dog)** animal;
            dog.barks();
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    在这里插入图片描述向下转型用的比较少,而且不完全,万一转换失败,运行时就会抛出异常,Java中为了提高向下转型的安全性,引入了instance,如果表达式为true,则可以安全转换。

    class Animal{
        private String name;
        private int age;
    
        public Animal(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public void eat(){
            System.out.println(this.age+"在吃饭");
        }
    }
    class Dog extends Animal{
        public Dog(String name, int age) {
            super(name, age);
        }
        public void barks(){
            System.out.println(this.getName()+"摇尾巴");
        }
        @Override
        public void eat() {
            System.out.println(this.getName()+"吃狗粮");
        }
    }
    class Cat extends Animal{
    
        public Cat(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println(this.getName()+"吃猫粮");
        }
    
    }
    public class Test {
    
        public static void main(String[] args) {
            Animal animal = new Dog("旺财", 3);
           
            if (animal instanceof Dog) {
                Dog dog = (Dog) animal;
                 ((Dog) animal).barks();
            }
    
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    在这里插入图片描述

  • 相关阅读:
    cmake练习一
    Elasticsearch 7.X 聚合查询 及 ElasticsearchRestTemplate 操作
    Git Cherry Pick命令
    二叉搜索树
    C#教程13:委托
    jQuery UI简单的讲解
    adb详细教程(四)-使用adb启动应用、关闭应用、清空应用数据、获取设备已安装应用列表
    java泛型的深入 泛型还可以在很多地方进行定义 泛型类 泛型方法 泛型接口 泛型的继承和通配符 泛型类练习
    Docker中安装mysqld-exporter
    故障分析 | MySQL 无监听端口故障排查
  • 原文地址:https://blog.csdn.net/2302_77978695/article/details/134518255