java面向对象的多态特性是什么?如何实现?

1.什么是多态性?

多态是指“许多形式”。 在OOP中,多态性意味着类型可以在不同时间指向不同的对象。 换句话说,引用类型所引用的实际对象可以在运行时确定。
在Java中,多态性基于继承和重写。

2.如何用Java实现多态?

在Java中,如果您具有带有两个或多个子类的超类(或超接口),则可以实现多态。

superInterface:Animal.interface

package Bean;

public interface Animal {

    public void move();

}

subclass:Bird.class

package Bean;

public class Bird implements Animal {
    @Override
    public void move() {
        System.out.println("Flying...");
    }
}

subclass:Fish.class

package Bean;

public class Fish implements Animal {
    @Override
    public void move() {
        System.out.println("Swimming...");
    }
}

As you can see, we have Animal as the super interface, and 2 sub classes: Bird and Fish.
Because the Bird implements Animal, or Bird is an Animal, we can write:

Animal animal = new Bird();

Because Fish is an Animal, it’s legal to write:

Animal animal1 = new Fish();

As you can see, we declare a reference variable called anim, which is of type Animal. Then we assign this reference variable to 3 different kinds of object: Dog, Bird and Fish.
You see? A reference type can take different objects (many forms). This is the simplest form of polymorphism, got it?

Now we come to a more interesting example to see the power of polymorphism.

java多态的概念

多态性是OOPs特性之一,它允许我们以不同的方式执行单个操作。例如,假设我们有一个类Animal,它有一个方法sound()。因为这是一个泛型类,所以我们不能给它一个实现,比如:咆哮,喵喵,呜呜等等。我们必须给出一个通用的消息。

示例1:运行时多态

Animal.java

package Bean;

public class Animal {

    public void sound()
    {
        System.out.println("Animal is making a sound");
    }
}

现在,让我们说说动物类的两个子类:扩展了动物类的马和猫(请参见继承)。 我们可以为相同的方法提供实现,如下所示:

Horse.java:

package Bean;

public class Horse extends Animal {

    @Override
    public void sound() {
        System.out.println("Neigh");
    }
}

and

Cat.java:

package Bean;

public class Cat extends Animal {
    @Override
    public void sound() {
        System.out.println("Meow");
    }
}

As you can see that although we had the common action for all subclasses sound() but there were different ways to do the same action.

This is a perfect example of polymorphism (feature that allows us to perform a single action in different ways).

It would not make any sense to just call the generic sound() method as each Animal has a different sound. Thus we can say that the action this method performs is based on the type of object.

什么是程序设计中的多态性?

多态性是一个方法基于它所作用的对象做不同事情的能力。换句话说,多态性允许定义一个接口并有多个实现。正如我们在上面的例子中看到的,我们定义了sound()方法,并在不同的-2个子类中实现了它。
将在运行时确定将调用哪个sound()方法,因此我们上面给出的示例是一个运行时多态示例。

测试Animal,Horse,Cat的运行时多态性

    public static void main(String[] args) {
        Animal animal = new Horse();
        animal.sound();

        Animal animal1 = new Cat();
        animal1.sound();
    }

示例2:编译时多态

另一方面,方法重载是一个编译时多态性示例。

package Bean;

public class OverLoad {

    void demo(int a) {
        System.out.println("a:" + a);
    }

    void demo(int a, int b) {
        System.out.println("a and b:" + a + "," + b);
    }

    double demo(double a) {
        System.out.println("double a:" + a);
        return a * a;
    }

    public static void main(String[] args) {
        OverLoad overLoad = new OverLoad();
        double result;

        overLoad.demo(10);
        overLoad.demo(10, 20);
        result = overLoad.demo(5.5);

        System.out.println("O/P:" + result);
    }
}

这里的方法demo()被重载了3次:第一个方法有1个int参数,第二个方法有2个int参数,第三个有double参数。 调用哪种方法取决于我们在调用方法时传递的参数。 这是在运行时编译时发生的,因此这种类型的多态被称为编译时多态。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值