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


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值