java多态实现示例

1. 多态

多态存在的三个必要条件:继承重写父类引用指向子类对象
当使用多态方式调用方法时,首先检查父类中是否有该方法,如果没有,则error;如果有,调用子类的同名方法。

java多态实现原理

package demo;

public class people {
    public people(){

    }
    public void run(){
        System.out.println("10");
    }
}

package demo;

public class player extends people{
    player(){

    }

    @Override
    public void run() {
        //super.run();
        System.out.println("100");
    }

    public static void main(String[] args) {
        people bob = new player();
        bob.run();
    }
}

输出结果为100

1.1. 多态的局限性

1.1.1. 不能重写私有方法

只能重写public,以及protected权限修饰符修饰的方法,以及如果在同一个包下的defult方法

//: polymorphism/PrivateOverride.java
// Trying to override a private method.
package polymorphism;
import static net.mindview.util.Print.*;

public class PrivateOverride {
  private void f() { print("private f()"); }
  public static void main(String[] args) {
    PrivateOverride po = new Derived();
    po.f();
  }
}

class Derived extends PrivateOverride {
  public void f() { print("public f()"); }
} /* Output:
private f()
*///:~

期望输出的是public f(),但是父类中的private方法被自动认为是final方法,对子类是屏蔽的,所以不能被重载;所以在这种情况下,子类的f()被当作一个全新的方法。

1.1.2. 不能动态绑定域(类变量成员变量或参数)

动态绑定的只有类,而变量没有动态绑定

//: polymorphism/FieldAccess.java
// Direct field access is determined at compile time.

class Super {
  public int field = 0;
  public int getField() { return field; }
}

class Sub extends Super {
  public int field = 1;
  public int getField() { return field; }
  public int getSuperField() { return super.field; }
}

public class FieldAccess {
  public static void main(String[] args) {
    Super sup = new Sub(); // Upcast
    System.out.println("sup.field = " + sup.field +
      ", sup.getField() = " + sup.getField());
    Sub sub = new Sub();
    System.out.println("sub.field = " +
      sub.field + ", sub.getField() = " +
      sub.getField() +
      ", sub.getSuperField() = " +
      sub.getSuperField());
  }
} /* Output:
sup.field = 0, sup.getField() = 1
sub.field = 1, sub.getField() = 1, sub.getSuperField() = 0
*///:~

所有的域操作都不是多态的,对于子类,域有不同的存储空间。

1.1.3. 不能重写私有方法静态方法

静态方法属于类,不能被重写

1.2. 子类重写父类方法调用父类方法

在c++中,可以通过向上提升的类型操作实现对父类行为的调用,而在Java中,无论对其进行什么样的类型转换,其类型实际上是不变的,只能通过super调用。
example:

package demo;

public class people {
    public people(){

    }
    public void run(){
        System.out.println("10");
    }
}

package demo;

public class player extends people{
    player(){

    }

    @Override
    public void run() {
        super.run();
        System.out.println("100");
    }

    public static void main(String[] args) {
        people bob = new player();
        bob.run();
    }
}

1.3. 多态示例1:

函数参数实现多态性

public class people {
    public people(){

    }
    public void run(){
        System.out.println("10");
    }
}
public class player extends people{
    player(){

    }

    @Override
    public void run() {
        //super.run();
        System.out.println("100");
    }


}
public class running {
    public void competition(people p1,people p2){//父类引用指向子类对象
        p1.run();
        p2.run();
    }
    public static void main(String[] args) {
        people bob = new player();
        people sam = new player();
        running round1 = new running();
        round1.competition(bob,sam);
    }
}

1.4. 多态示例2:

接口实现多态性,解耦

public interface runrunrun {
    public void run();
}

public class people implements runrunrun {
    public people(){

    }
    public void run(){
        System.out.println("10");
    }
}


public class player implements runrunrun{
    player(){

    }
    //@Override
    public void run() {
        //super.run();
        System.out.println("100");
    }
}


public class running {
    public void competition(runrunrun p1,runrunrun p2){
        p1.run();
        p2.run();
    }
    public static void main(String[] args) {
        player bob = new player();
        player sam = new player();
        running round1 = new running();
        round1.competition(bob,sam);
    }
}
posted @ 2020-03-11 15:28  Ysalng  阅读(805)  评论(0编辑  收藏  举报