0
点赞
收藏
分享

微信扫一扫

PG数据库提示: FATAL: sorry, too many clients already

云岭逸人 2023-06-02 阅读 76
java

目录

1.什么是多态

比如:动物吃饭,但是狗吃的话就是吃狗粮,猫吃的话就是吃猫粮,都是同一种行为,不同的对象去完成就会有不同的结果状态。

2.多态的条件

对于多态来说我们要满足三个条件:这三个条件缺一不可

如果你想重写,那么你就必须要在继承的条件下,

class  Animal
{
    public String name;
    public int age;

    public void eat()
    {
        System.out.println(name+"在吃饭");
    }
}
class Dog extends Animal{

    public void eat()
    {
        System.out.println(name+"在狗粮");
    }

    public void wangwang()
    {
        System.out.println(name+"在旺旺");
    }
}

class Brid extends Animal{

    public void eat()
    {
        System.out.println(name+"在鸟粮");
    }

    public void wing(){
        System.out.println(name+"在飞");
    }
}
public class Test {
    public static void main(String[] args) {

        Animal animal=new Dog();//向上转型
        //父类对象的引用指向子类对象
        animal.name="小狗";
        animal.eat();
        System.out.println("============");

        Animal animal1=new Brid();
        animal1.name="小鸟";
        animal1.eat();
    }
}

在这里插入图片描述
当类的调用者在编写 eat 这个方法的时候, 参数类型为 Animal (父类), 此时在该方法内部并不知道, 也不关注当前的 引用指向的是哪个类型(哪个子类)的实例. 此时 这个引用调用 eat方法可能会有多种不同的表现(和 引用的实例相关), 这种行为就称为 多态.

3.向上转型

向上转型:其实就是创建一个子类对象,将其当做父类对象来使用

父类对象的引用指向子类对象,是一个从小范围的向大范围转换

3.1向上转型的三种写法

1.直接通过父类引用
父类对象 对象名=new 子类对象类型();

    public static void main(String[] args) {
        Shape shape=new Rect();
        Shape shape1=new Cycle();
        Shape shape2=new Flower();
        
    }
  1. 通过一个方法,里面传参的是父类的参数
    public static void func(Shape shape)
    {
        //向上转型
        shape.draw();
    }

    public static void main(String[] args) {
        Rect rect=new Rect();
        Cycle cycle=new Cycle();
        func(rect);
        func(cycle);

    }

这个就是不需要实例化对象,实例化的这一步在传参的时候进行
public static void func(Shape shape)
{
//向上转型
shape.draw();
}

public static void main(String[] args) {
    func(new Rect());
    func(new Cycle());
    func(new Flower());

}

3.2 优缺点

向上转型的优点:让代码实现更简单灵活。

  1. 能够降低代码的 “圈复杂度”, 避免使用大量的 if - else

例如我们现在需要打印的不是一个形状了, 而是多个形状,那么这个时候多态就很容易了,只需要添加一个就可以了。

import java.util.concurrent.Callable;

class Shape
{
    public void draw()
    {
        System.out.println("画图形");
    }
}

class Rect extends Shape{
    @Override
    public void draw() {
        System.out.println("画矩形");
    }
}
class Cycle extends Shape{
    @Override
    public void draw() {
        System.out.println("画圆");
    }
}

class Flower extends Shape{
    @Override
    public void draw() {
        System.out.println("画花");
    }
}
public class Test2 {


    public static void drawShapes() {
        Rect rect = new Rect();
        Cycle cycle = new Cycle();
        Flower flower = new Flower();
        String[] shapes = {"cycle", "rect", "cycle", "rect", "flower"};
        for (String shape : shapes) {
            if (shape.equals("cycle")) {
                cycle.draw();
            } else if (shape.equals("rect")) {
                rect.draw();
            } else if (shape.equals("flower")) {
                flower.draw();
            }
        }
    }

    public static void drawShapes1() {
        Shape rect = new Rect();
        Shape cycle = new Cycle();
        Shape flower = new Flower();
        Shape[] shapes = {cycle,rect,cycle,rect,flower};
        for (Shape shape:shapes) {
            shape.draw();
        }

    }


    public static void drawShapes2() {
        Rect rect = new Rect();
        Cycle cycle = new Cycle();
        Flower flower = new Flower();
        Shape[] shapes = {cycle,rect,cycle,rect,flower};
        for (Shape shape:shapes) {
            shape.draw();
        }

    }
    public static void func(Shape shape)
    {
        //向上转型
        shape.draw();
    }

    public static void main2(String[] args) {
        drawShapes();
        System.out.println("=========");
        drawShapes1();
        System.out.println("===========");
        drawShapes2();
    }
}
  1. 可扩展能力更强
    如果要新增一种新的形状, 使用多态的方式代码改动成本也比较低

向上转型的缺陷:不能调用到子类特有的方法。
3. 属性没有多态性
当父类和子类都有同名属性的时候,通过父类引用,只能引用父类自己的成员属性
4. 构造方法没有多态性

4.向下转型(用得少)

什么是向下转型?

将一个子类对象经过向上转型之后当成父类方法使用,再无法调用子类的方法,但有时候可能需要调用子类特有的方法,此时:将父类引用再还原为子类对象即可,即向下转换。

Cat cat=(Cat)new Animal();
注意这里需要强转

向下转型用的比较少,而且不安全,万一转换失败,运行时就会抛异常。Java中为了提高向下转型的安全性,引入了 instanceof ,如果该表达式为true,则可以安全转换。

public class TestAnimal {
public static void main(String[] args) {
Cat cat = new Cat("元宝",2);
Dog dog = new Dog("小七", 1);
// 向上转型
Animal animal = cat;
animal.eat();
animal = dog;
animal.eat();
if(animal instanceof Cat){
cat = (Cat)animal;
cat.mew();
}
if(animal instanceof Dog){
dog = (Dog)animal;
dog.bark();
}
}
}

5.避免在构造方法中调用重写的方法

当我们创建两个类的时候,一个子类,一个父类,当我们在父类的构造方法中调用一个方法,并且这个方法在子类中重写了。结果会如何呢?

class B
{
    public B() {
        func();
    }
    public void func()
    {
        System.out.println("B func()");
    }
}
class D extends  B
{
    @Override
    public void func() {
        System.out.println("D func()");
    }
}
public class Test3 {
    public static void main(String[] args) {
        D d =new D();
    }
}

在这里插入图片描述
构造 D 对象的同时, 会调用 B 的构造方法.
B 的构造方法中调用了 func 方法, 此时会触发动态绑定, 会调用到 D 中的 func
此时 D 对象自身还没有构造, 此时 num 处在未初始化的状态, 值为 0. 如果具备多态性,num的值应该是1.
所以在构造函数内,尽量避免使用实例方法,除了final和private方法。

6.重写

6.1什么是重写

重写(override):也称为覆盖。重写是子类对父类非静态、非private修饰,非final修饰,非构造方法等的实现过程
进行重新编写, 返回值和形参都不能改变。即外壳不变,核心重写!重写的好处在于子类可以根据需要,定义特定于自己的行为。 也就是说子类能够根据需要实现父类的方法。

在这里插入图片描述
就比如,父类中有一个方法func,但是他的子类中也有一个方法func,这就是重写

6.2 重写和重载的区别

在这里插入图片描述

举报

相关推荐

0 条评论