java中多态的理解--摘自StackOverflow
2017-10-23 13:40
274 查看
The clearest way to express polymorphism is via an abstract base class (or interface)
This class is abstract because the
is not definable for Humans. It is only definable for the subclasses Male and Female. Also, Human is an abstract concept — You cannot create a human that is neither Male nor Female. It’s got to be one or the other.
So we defer the implementation by using the abstract class.
and
Now we can tell an entire room full of Humans to go pee.
Running this would yield:
public abstract class Human{ ... 4000 public abstract void goPee(); }
This class is abstract because the
goPee()method
is not definable for Humans. It is only definable for the subclasses Male and Female. Also, Human is an abstract concept — You cannot create a human that is neither Male nor Female. It’s got to be one or the other.
So we defer the implementation by using the abstract class.
public class Male extends Human{ ... @Override public void goPee(){ System.out.println("Stand Up"); } }
and
public class Female extends Human{ ... @Override public void goPee(){ System.out.println("Sit Down"); } }
Now we can tell an entire room full of Humans to go pee.
public static void main(String[] args){ ArrayList<Human> group = new ArrayList<Human>(); group.add(new Male()); group.add(new Female()); // ... add more... // tell the class to take a pee break for (Human person : group) person.goPee(); }
Running this would yield:
Stand Up Sit Down ...
相关文章推荐
- Java中多态的理解
- java中的多态理解
- java提高篇(三)-----理解java的三大特性之多态
- Java 继承与多态的深入理解
- 关于java继承与多态的深入理解
- 理解Java中的多态
- 深入理解java——多态与动态绑定
- Java 多态透析 详细理解
- (第25讲)java中的多态的深入理解
- java多态 我的理解
- 我理解java的多态
- 如何理解Java面向对象的多态
- 我是菜鸟:Java中多态的理解
- 对java继承,多态,抽象类,接口的理解
- java多态,如何理解父类引用指向子类对象
- Java基础 对封装、继承、多态、抽象的理解
- java提高篇(四)-----理解java的三大特性之多态
- java提高篇之理解java的三大特性——多态
- Java多态调用机制理解
- java继承和多态的理解