java多态实现示例
Posted innndown
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java多态实现示例相关的知识,希望对你有一定的参考价值。
多态
多态存在的三个必要条件:继承,重写,父类引用指向子类对象,
当使用多态方式调用方法时,首先检查父类中是否有该方法,如果没有,则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多态实现示例的主要内容,如果未能解决你的问题,请参考以下文章
LockSupport.java 中的 FIFO 互斥代码片段