JavaSE:多态

向上转型:

先看一段代码:

 为何Animal animal=new Dog这个代码不报错。就是因为使用了向上转型:父类引用引用子类对象

向上转型一共有三种方式可以实现向上转型:1.直接赋值,2.通过传参,3.返回值

1.直接赋值:Animal animal=new Dog

2.通过传参:

通过func1的参数进行向上转型。

3.返回值:

通过func2的返回值来向上转型。

注意事项:

从上面代码中可以看出,第二个对象animal进行了向上转型,向上转型之后,animal就不能调用子类bark的方法。

对于进行了向上转型的对象,通过父类的引用,调用子类特有的方法,是无法直接调用的,这里只能调用父类自己有的。

所以向上转型的缺点:不能调用子类特有的方法。

重写:

当父类和子类的两个方法:

1.方法名相同

2.参数列表相同(个数,类型,顺序)

3.返回值也相同

就说明这两个方法构成了重写。

重写的两个方法对代码有什么影响呢?请看下面这个代码:

父类的eat方法和子类的eat方法构成了重写

 

然后我们看结果:

运行结果:

重写的注意事项:

1.不能重写一个静态方法,父类的构造方法也不能被重写

2.被final修饰的方法不能被重写,这个方法被称为密封方法

public final void fun(){
    System.out.println(this.name+"正在做作业");
}

3.访问权限的问题,如果子类重写父类的方法,则子类的成员访问修饰符的权限要大于等于父类的成员访问修饰符的权限。

访问权限大小:

private<default<protected<public

 4.如果方法是由private修饰的,则不能被重写。

private void eat(){
    System.out.println("正在吃饭");
}

5.被重写的方法返回值可以不同,但必须构成父子类关系,在重写方法时,子类的返回值类型必须与父类返回值类型相同或者是其子类

//Animal类的方法
public Animal eat(){
        System.out.println(this.name+"正在吃饭...");
        return null;
    }
//Dog重写父类的eat方法
 public Dog eat(){
       System.out.println(this.name+"正在吃狗粮...");
       return null;
   }

动态绑定:

上面构成重写代码中,通过反汇编来看,程序在编译的时候,程序确实调用的是父类的eat方法但是在运行的时候,通过父类的引用,调用了父类和子类重写的那个方法,结果实际调用了子类的方法,此时我们把这个情况叫做动态绑定

动态绑定是理解 多态 的基础。

发生动态绑定的前提是:(大前提:就是一定要在继承的情况下)

1.父类引用引用子类对象(向上转型)

2.通过父类引用,调用了父类和子类重写的那个方法 

 静态绑定:

静态绑定:在编译时,根据用户所传递实参类型就确定了具体调用那个方法,典型代表函数重载

重载:

方法名相同,方法参数不同(参数个数,类型,顺序)

方法返回值类型可以相同可以不同

方法的修饰符可以相同可以不同

 向下转型:

将一个子类对象经过向上转型之后当成父类方法使用,再无法调用子类的方法,但有时候又需要调用子类所特有的方法,此时:将父类引用转化为子类对象,称为向下转型

//Animal类
public class Animal {
    public String name;
    public int age;
    public Animal(String name,int age){
        this.name=name;
        this.age=age;
    }
    public void eat(){
        System.out.println(this.name+"正在吃饭...");
    }
}
//Dog类
public class Dog extends Animal {
   public Dog(String name,int age){
       super(name,age);
   }
   public void bark(){
       System.out.println(this.name+"正在狗叫...");
   }
   public void eat(){
       System.out.println(this.name+"正在吃狗粮...");
   }
}
//鸟类
public class Bird extends Animal{
    public Bird(String name,int age){
        super(name,age);
    }
    public void fly(){
        System.out.println(this.name+"正在飞...");
    }
   public void eat(){
     System.out.println(this.name+"正在吃鸟粮...");
    }
}

//测试类
 class Test {
  public static void main(String[] args) {
     Animal animal1=new Dog("旺财",10);//先向上转型
     Dog dog=(Dog)animal1;//向下转型,可以使用子类的方法
     dog.bark();//使用子类特有的方法
  }
 }

 此时就完成了向下转型。

但是并不是所有的向下转型都能成功:
将上面代码的测试类修改为:

class Test {
  public static void main(String[] args) {
     Animal animal1=new Dog("旺财",10);
     Bird bird=(Bird)animal1;
     bird.fly();
  }
 }

 编译的时候不会提醒你有错,但是运行时就会报错

向下转型用的少,而且不安全,万一转换失败就会抛异常,所以java为了提高向下转型的安全性,引入了instanceof,如果表达式为true,则可以成功转换

instanceof运算符:

instanceof用法:

对象 instanceof

 对象是要检查的对象,类是要检查的类,instanceof运算符会返回一个布尔值,表示对象是否属于该类或者其子类,如果属于就返回true,反之则返回false。

再次修改上面的测试类:

 class Test {
  public static void main(String[] args) {
     Animal animal1=new Dog("旺财",10);
     if(animal1 instanceof Bird){
         Bird bird=(Bird)animal1;
        bird.fly();
     }else{
         System.out.println("animal1 instanceof Bird not!!!");
     }
  }
 }

 animal1不属于Bird类。也不属于Bird的子类,所以不能安全转换。

 这样就能使代码更加安全,如果animal1属于Bird类或者属于其子类就可以安全向下转型

多态的概念:

通俗来说,就是多种形态,具体点就是去完成某个行为,当不同的对象去完成时会产生出不同的形态。

先看代码:

//Animal类
public class Animal {
    public String name;
    public int age;
    public Animal(String name,int age){
        this.name=name;
        this.age=age;
    }
    public void eat(){
        System.out.println(this.name+"正在吃饭...");
    }
}
//Bird类
public class Bird extends Animal{
    public Bird(String name,int age){
        super(name,age);
    }
    public void fly(){
        System.out.println(this.name+"正在飞...");
    }
    public void eat(){
        System.out.println(this.name+"正在吃鸟粮...");
    }
}
//Dog类
public class Dog extends Animal {
   public Dog(String name,int age){
       super(name,age);
   }
   public void bark(){
       System.out.println(this.name+"正在狗叫...");
   }
   public void eat(){
       System.out.println(this.name+"正在吃狗粮...");
   }
}
测试类
class Test {
    public static void func(Animal animal){
        animal.eat();
    }
     public static void main(String[] args) {
        Dog dog=new Dog("旺财",19);
         func(dog);
         System.out.println("===========");
         Bird bird=new Bird("小鸟",12);
         func(bird);
     }
 }

运行结果:

 这里调用了两次相同的func方法,但是结果不一样

调用相同的方法,不同的对象去调用产生的结果不一样,这就是多态。

多态实现的条件:

1.必须在继承体系下

2.子类必须要对父类的方法进行重写

3.通过父类的引用调用重写的方法

 多态的优缺点:

 先给几个类:

//画一个图形的类
public class Shape {
    public void draw() {
        System.out.println("画一个图像");
    }
}
//画一个圆形的类
public class Cycle extends Shape{
    public void draw(){
        System.out.println("画一个○");
    }
}
//画一个三角形的类
public class Triangle extends Shape{
    public void draw(){
        System.out.println("画一个△");
    }
}
//画一个正方形的类
public class Rect extends Shape {
    public void draw() {
        System.out.println("画一个□");
    }
}

多态的优点:

1.可以降低代码的“圈复杂度”,避免使用大量的if-else语句。

圈复杂度:圈复杂度是一种描述一段代码复杂程度的方式,一段代码如果平铺直叙,那这段代码就更容易理解,而如果有很多的条件分支和循环语句,那么认为代码就更难理解,更复杂。

在上面打印图形代码中,如果我们要打印多个图形,且不用多态,代码如下:

//测试类
class Test {
  public static void main(String[] args) {
   Cycle cycle=new Cycle();
   Rect rect=new Rect();
   Triangle triangle=new Triangle();
   String[] shape={"Cycle","Rect","Triangle"};
   for (int i = 0; i < shape.length; i++) {
    if(shape[i].equals("Cycle")){
     cycle.draw();
    }else if(shape[i].equals("Rect")){
     rect.draw();
    }else {
     triangle.draw();
    }
   }
  }
 }

但是如果使用多态,就不用写这么多的if-else,从而降低圈复杂度:

class Test {
  public static void main(String[] args) {
//创建一个Shape对象的数组
   Shape[] shape={new Cycle(),new Rect(),new Triangle()};
   for (Shape x: shape) {
    x.draw();
   }
  }
 }

 打印结果:

 2.扩展能力强:

如果要新填一种形式,使用多态成本也比较低,只需要调用者创建一个新类就可以了,比如添加画一个花的类。

public class Flower extends Shape{
    public void draw() {
        System.out.println("画一个❀");
    }
}

创建一个花类后,然后修改Shape对象数组中的参数

class Test {
  public static void main(String[] args) {
   Shape[] shape={new Cycle(),new Rect(),new Triangle(),new Flower()};
   for (Shape x: shape) {
    x.draw();
   }
  }
 }

打印结果:

但是如果不用多态,就要增加if-else分支语句,从而使得圈复杂度更高。

 多态的缺点:

1.属性没有多态性,当父类和子类都有同名的属性时,通过父类引用只能调用父类自己的属性。

2.构造方法没有多态性。

 避免在构造方法中调用重写方法:

B是父类,D是子类,D重写B的func方法,并在B的func方法中调用func方法。

public class B {
    public B() {
        func();
    }
    public void func(){
        System.out.println("func-父类B");
    }
}
public class D extends B{
    public int num=1;
    public void func(){
        System.out.println("func-子类"+num);
    }
}
public class Test {
    public static void main(String[] args) {
        D d=new D();
    }
}

执行结果:

func-子类0

 为什么最后的num等于0呢?

之前讲过静态代码块,实例代码块,构造方法的执行顺序。(可以去看)

这里在创建d的同时,会调用B的构造方法,因为默认在子类D中提供了构造方法。

B的构造方法在调用func的方法时,会触发动态绑定,然后会调用子类的重写的func方法

D方法中没有构造方法,此时num处于未初始化状态,所以默认为0,所以在构造函数内,尽量不要调用实例方法,除了final和private方法。

结论:尽量不要在构造器中调用方法(如果这个方法被子类重写, 就会触发动态绑定, 但是此时子类对象还没构造完成), 可能会出现一些隐藏的但是又极难发现的问题。

  • 14
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值