你的位置:首页 > 软件开发 > Java > TIJ英文原版书籍阅读之旅——Chapter Eight:Polymorphism

TIJ英文原版书籍阅读之旅——Chapter Eight:Polymorphism

发布时间:2015-06-16 12:00:12
The twist|_Method-call bindingConnecting a method call to a method body is called binding. When binding is p ...

The twist

|_Method-call binding

Connecting a method call to a method body is called binding. When binding is performed before the program is run(by the compiler and linker, if there is one), it's called early binding. You might not hava heard the term before because it has never been an option with procedural languages. C compilers hava only one kind of method call, and that's early binding.

When a language implements late binding, there must be some mechanism to determine the type of the object at run time and to call the appropriate method. That is, the compiler still doesn't know the object type, but the method-call mechanism finds out and calls the correct method body. The late-binding mechanism varies from language to language, but you can imagine the some sort of type information must be installed in the objects.

All method binding in Java uses late binding unless the method is static or final (private method are implicitly final). This means that ordinarily you don't need to make any decision about whether late binding will occur-it happens automatically.

|_Pitfall:"overriding" private methods

Here's something you might innocently try to do:

public class PrivateOverride{	private void f() {System.out.println("private f()");}		public static void main(String[] args)	{		PrivateOverride po = new Derived();		po.f(); 	}}class Derived extends PrivateOverride{	public void f() {System.out.println("public f()");}}/*Output:private f()*/

原标题:TIJ英文原版书籍阅读之旅——Chapter Eight:Polymorphism

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录