多态语法详解

一:概念

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);

    }
}

在这里插入图片描述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();//通过父类引用访问了和父类同名的子类方法,

    }
}


在这里插入图片描述

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));

    }
}

在这里插入图片描述

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();

    }
}

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();
    }
}

在这里插入图片描述

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

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();

    }
}

在这里插入图片描述向下转型用的比较少,而且不完全,万一转换失败,运行时就会抛出异常,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();
        }


    }
}

在这里插入图片描述

  • 77
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 27
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十一.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值