Thinking in java-24 Polymorphism 多态

1.多态Polymorphism

多态的初衷是为了让方法的适用性更强,即:当我们写了一个方法时,可以把基类作为参数,而可以将扩展类作为其参数传入;而不是将扩展类作为参数,此时该方法的适用度就没有前者高了。
Polymorphism is the ability of a class instance to behave as if it were an instance of another class in its inheritance tree, most often one of its ancestor classes. For example, in Java all classes inherit from Object. Therefore, you can create a variable of type Object and assign to it an instance of any class.
多态的定义:一个类的实例对象表现起来就像它是其所在继承树其他类的实例一样,通常该类表现地更像其父类。极端情况是,我们创建一个参数为Object类对象的方法,那么我们可以把任意对象传给这个函数。

2.实例Demo

stackoverflow上有该问题相关的Q&A.
这里有个interesting的例子。

public abstract class Human{
    public abstract void goPee();
}
public class Male extends Human{
    @Override
    public void goPee(){
        System.out.println("First, stand up...");
    }
}
public class Female extends Human{
    @Override
    public void goPee(){
        System.out.println("First, sit down...");
    }
}
public class TestPoly{
    public static void main(String[] args){
        ArrayList<Human> list = new ArrayList<>();
        list.add(new Male());
        list.add(new Female());
        for(Human human: list)
            human.goPee();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《Thinking in Java》是由Bruce Eckel撰写的一本Java编程的经典教材,该书以英文语言出版。以下是该书的目录大纲: Chapter 1: Introduction to Objects - Introduction - The progress of abstraction - How objects communicate - Every object has an interface - The process of object-oriented design - Implementation hiding - Inheritance and reuse - Polymorphism and dynamic binding - Summary of object-oriented principles Chapter 2: Everything Is an Object - Primitive types - How the objects are created - Aliasing: All arguments are passed by value - Documentation comments - Controlling access to members of a class - First exposure to Java syntax Chapter 3: Operators - Thinking recursively - Operator precedence - Assignment with objects - Mathematical operators and precedence - Autoboxing and unboxing - Increment and decrement - Java logic operators - Bitwise operators - The instanceof operator - Summary of operators Chapter 4: Control Structures: Part 1 - True and false - The if-else statement - The switch statement - The while statement - The do-while statement - The for statement - Summary of control structures Chapter 5: Control Structures: Part 2 - The break and continue keywords - Foreach and multidimensional arrays - Using the comma operator - The return keyword - Summary of control structures Chapter 6: Initialization & Cleanup - Member initialization - Constructor initialization - Method overloading & generic methods - Order of initialization - Constructor & parameter lists - Garbage collection - The finalize() method - Summary of initialization and cleanup ......(接下去的章节继续)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值