多态

概念:同一个接口/父类方法,得到多种不同输出结果

多态的核心: 父类/接口引用 , 子类执行

类多态

实现类多态的必备条件

  1. 继承:子类继承父类
  2. 重写:子类重写父类方法
  3. 父类引用: 父类引用指向子类对象

下面是运用继承父类实现多态的代码:

package 多态;
//创建父类
class car{
    public void version(){

    }
}
//子类1
class car1 extends car{
    public void version(){
        System.out.println("我是小轿车");
    }
}
//子类2
class car2 extends car{
    public void version(){
        System.out.println("我是SUV");
    }
}
//子类3
class car3 extends car{
    public void version(){
        System.out.println("我是皮卡");
    }
}

public class main3 {
    public static void main(String[] args) {
        //创建实例对象,并通过父类引用指向
        car tast1 = new car1();
        car tast2 = new car2();
        car tast3 = new car3();

        tast1.version();
        tast2.version();
        tast3.version();
    }
}

此时的调用的version方法为各子类重写的方法

接口多态

实现接口多态的必备条件:

  1. 实现接口
  2. 实现接口方法
  3. 接口引用指向实例对象     

下面是运用接口实现多态的代码:

package 多态;
//创建接口
 interface  car{
    public void version();
    
}
//实现类1
class car1 implements car{
    public void version(){
        System.out.println("我是小轿车");
    }
}
//实现类2
class car2 implements car{
    public void version(){
        System.out.println("我是SUV");
    }
}
//实现类3
class car3 implements car{
    public void version(){
        System.out.println("我是皮卡");
    }
}
public class main2 {
    public static void main(String[] args) {
        // 创建接口引用实例对象
        car tast1 = new car1();
        car tast2 = new car2();
        car tast3 = new car3();
        tast1.version();
        tast2.version();
        tast3.version();
    }

}

此时调用的version方法为实现类中实现接口的方法

接口/父类引用指向的限制

在多态中 

  1. 在类多态和接口多态中 , 实例对象无法再调用子类的其他独特方法,
  2. 在类多态中 , 无法直接调用被重写的父类方法

下面是融合两种多态的代码:

package 多态;
//创建一个接口
interface Animals {
    //创建抽象方法
    public void eat();
    
}
//创建一个父类
class Animal{
    public void move(){
        System.out.println("动物在移动");
    }
}
//创建一个类实现接口
class dog implements Animals{
    //实现接口方法
    @Override
    public void eat() {
        System.out.println("动物吃东西");
    }
    
    // 独自的方法
    public void sleep(){
        System.out.println("狗在睡觉");
    }
}
//创建一个子类继承父类
class cat extends Animal{
    //重写父类方法
    public void move(){
        System.out.println("猫在走");
    }
    //独自的方法
    public void sleep() {
        System.out.println("猫在睡觉");
    }
}

public class main1 {
    public static void main(String[] args) {
        //用接口引用的实例对象
        Animals tast1=new dog();
        //用父类引用的子类对象
        Animal tast3=new cat();
        // 用子类引用
        dog tast2 = new dog();
        cat tast4=new cat();

        //无法用子类引用父类的实例对象
        // cat tast5=new Animal();

        //接口调用
        tast1.eat(); 
        // tast1.sleep();
        tast2.eat();
        tast2.sleep();
        //父类调用
        tast3.move();
        // tast3.sleep();
        tast4.sleep();
        // tast3.super.move();
    }   
}

类多态

父类引用指向的子类实例无法调用子类独有的方法:

//用父类引用的子类对象
        Animal tast3=new cat();

        tast3.sleep();
发生报错

父类引用指向子类的对象 , 执行的方法为子类重写的方法

tast1.eat();//结果为dog类中的"动物在吃东西"

若需要执行父类中被重写的方法 , 则需要运用super关键字 , 在子类方法中调用或者创建专门调用的方法

class cat extends Animal{
    //重写父类方法
    public void move(){

        //先调用父类被重写的代码

        super.move();
        System.out.println("猫在走");
    }  
}

接口多态:

在接口中 , 同样无法调用实现类中独自的方法 , 也只能调用实现类的实现方法

this()和super()
 

尽情期待!!!

重要内容!!!

文章到这里就暂告一段落啦,真心希望能为你带来一些启发和帮助。❤️

你的每一次阅读,都是我坚持分享的最大动力 ✨。如果文中有任何不清楚、或者你觉得有误的地方,非常欢迎在评论区指出来 🤔,我们一起探讨,共同进步~

如果觉得内容不错,别忘了点个赞 👍 让我知道哦!

关注我 👉,不要错过系列后续的更多干货分享!期待在下一篇文章里,再次与你相遇哦 !😊

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐