码迷,mamicode.com
首页 > 编程语言 > 详细

java多态实现示例

时间:2020-03-01 22:08:34      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:类重写   rri   不同   ace   method   nal   提升   子类   缺陷   

多态

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

输出结果为100

多态的缺陷

  • "重写私有方法"
//: 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()被当作一个全新的方法。

  • 域(类变量成员变量或参数)
//: 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
*///:~

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

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

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

在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:

函数参数实现多态性

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

多态示例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);
    }
}

java多态实现示例

标签:类重写   rri   不同   ace   method   nal   提升   子类   缺陷   

原文地址:https://www.cnblogs.com/innndown/p/12392148.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!