Java基本特性(1)-Polymorphism: 论多态

来源:互联网 发布:卖淘宝店铺的网站 编辑:程序博客网 时间:2024/06/08 15:41

The dictionary definition of polymorphism refer to a principle in biology in which an organism or species can have many different forms or stages. This principles can also applied to Object-Oriented programming and languages like Java language. Subclasses of a class can define their own unique behaviours and yet share some of the same functionaliy of the parent class.


Definition:
1) Polymorphism is the association between a generalized reference and a more specific object.
2) Polymorphism , means "many forms", is the ability to treat an object of any subclass of a base class as if it were the object of the base class. A base class has, therefore, many forms : the base class itself and any of its subclasses.
3) Polymorphism is the ability of a object to take on many forms, the most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

4)父类型的引用可以指向子类型的对象.

5)一种将不同的特殊行为和单个泛化记号相关联的能力

public class Software{void declare(){System.out.println("hello, all my software friends.");}}public class QQ extends Software{void declare(){System.out.println("I hate 360");}}public class Safe360 extends Software{void declare(){System.out.println("I hate QQ");}}public class SoftwareMeeting{public void saySomeThing(Software aSoftware){   aSoftware.declare();}public static void main(String[] args){SoftwareMeeting aMeeting=new SoftwareMeeting();Software aQQSoftware=new QQ();Software a360Software=new Safe360();aMeeting.saySomeThing(aQQSoftware);aMeeting.saySomeThing(a360Software);}}

继承是多态得以实现的基础。The underlying mechanism that makes polymorphism possible is dynamic binding.多态实现的底层机制是动态绑定。
所谓“绑定(binding)”,是指建立method call(函数调用)和method body(函数本体)的关联。分为前期绑定和后期绑定。
1)前期绑定(Early Binding):在程序运行之前绑定,由编译器和连接程序实现,又叫静态绑定(Static Binding),比如static方法和final方法。注意也包括private方法,因为它是隐式final的。
2)后期绑定(Late Binding):如果绑定动作在程序执行期根据对象的类型进行绑定,由方法调用机制实现,因此又叫做动态绑定(Dynamic Binding),或者运行时绑定(Run-time Binding)。Exception for three special cases, all instance methods in Java programs are dynamically bound. The instance method that is invoked at run-time will be determined by the actual class of the object, not by the type of the reference.


多态的好处:消除了类之间的耦合关系,使得程序更容易扩展,增加新的子类型而不需要对原来逻辑做任何修改。



重载 vs 重写

重载和重写都是针对方法的概念。

Method Signature:a method is part of the method declaration. It is the combination of the method name and the parameter list.型构就是指方法的组成结构,具体包括方法的名称和参数,涵盖参数的数量、类型以及出现的顺序,但是不包括方法的返回值类型、访问权限修饰符,抛出的异常,以及abstract、static、final等修饰符。


1)重写:Overriding,是指在继承的情况下,子类中定义了与其基类中方法中具有相同型构的新方法,就叫做子类把基类的方法重写了。

A:参数列表需要完全相同

B:返回值类型要完全相同

C:访问级别的限制性可以比被重写方法的弱,但一定不能比被重写方法的强

D:重写的方法可以抛出更少或更有限的异常,但是不能抛出更广泛类型的异常。

E:final方法不可重写。不能被继承的方法不能重写


2)重载:Overloading,是指在同一个类中定义了一个以上具有相同名称但是型构不同的方法。在同一个类中,是不允许定义多余一个具有相同型构的方法的。

A:the same method name

B: the different signature(parmeter type or parmeter order )

C:返回值类型、异常类型、修饰符可以不同

  

调用:

1)重载:参数类型决定哪个重载版本,发生在编译时。被调用的实际方法仍然是发生在运行期间的虚拟方法调用,但是编译器已经知道所调用方法的签名,因此运行期间参数匹配已经明确,只是还不知道方法所在的实际类。

2)重写:实现多态的机制,堆上实际实例的类型决定调用选择哪个方法,发生在运行期间。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信语音聊天音量很小怎么办 一手软件崩溃钱卡住了怎么办 身上皮肤很黑怎么办?好想穿短裙 家里有很多小飞虫怎么办 家里有垃圾中飞出虫子怎么办 雷蛇笔记本很烫怎么办 登录监控器的账号锁了怎么办 悦借钱登录账号被锁怎么办 365账号登录被锁怎么办 台式电脑cpu温度过高怎么办 UG2.0打不开错误—15怎么办 键盘空格键删除键失灵怎么办 耳麦检测声音不分左右怎么办 吃生萝卜胃疼怎么办 幼兔拉稀怎么办没精神 自酿米酒酸了怎么办 用冰敷脸感觉红烫了怎么办 吃了海兔的内脏怎么办 吃了鱿鱼的吸盘怎么办 想开个烧烤店没学过怎么办 墨鱼汁弄衣服上怎么办 干鱿鱼泡开发黄怎么办 吃了芥末胃疼怎么办 手撕鱿鱼咸了怎么办 孕妇吃了点芥末怎么办 葡萄酒上面有一层白霉怎么办 手机一不小心把视频删了怎么办 柑橘7月份果实小怎么办 鹦鹉鱼身上有小白点怎么办 鹦鹉鱼身上烂了怎么办 红鹦鹉鱼变黑了怎么办 唱美声总夹嗓子怎么办 发财树树干出褶怎么办 月季花夏天换盆出现黄叶怎么办 刚种的月季枯了怎么办 新买的月季黄叶了怎么办 月季花叶孑轰发黄怎么办? 新种的月季苗弱怎么办? 月季换盆后浇透水叶子黄了怎么办 肉肉移栽后浇透水怎么办 月季花扦插的没长根发芽了怎么办