访问继承的成员? [英] Accessing inherited members?

查看:66
本文介绍了访问继承的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们通过对象引用调用方法时,对象的实际类控制使用哪个实现。

当您访问该字段时,使用声明的引用类型。 />


任何人都可以解释为什么这个约定是用java制作的?意味着为什么我们只在需要访问方法时才需要实际对象?为什么不进入这个领域呢?



我对这个惯例很困惑:(



先谢谢为你的帮助!

When we invoke a method through an object reference ,the actual class of the object governs which implementation is used.
when you access the field,the declared type of the reference is used.

Can Anyone Explain me why this convention was made in java? means Why we need actual object only when we need to access the method? Why not in accessing the field ?

I am confused with this convention :(

Thanks in Advance for your help!

推荐答案

没有转换。你需要的答案是 polymorphism 和类型转换(向下转换/向上转换)。 />
阅读以下两个简短教程:

http://www.tutorialspoint。 com / java / java_polymorphism.htm [ ^ ]

http://www.javabeginner.com/learn-java/ java-object-typecasting。 [ ^ ]。



很简单:T 类的对象同时是hier中所有祖先类的一个实例从根到T的archy。如果要处理对象,则需要对它进行引用。引用具有自己的类类型。如果要访问上述类型T的对象,可以使用从根到最多为T的层次结构中的任何类型的引用(这样您将访问对象 as 该类型)。现在,如果要以与引用声明指定的方式不同的方式访问对象,则需要进行类型转换。
There is no conversion. The answer you need is polymorphism and type casting (downcasting/upcasting).
Read these two short tutorials:
http://www.tutorialspoint.com/java/java_polymorphism.htm[^]
http://www.javabeginner.com/learn-java/java-object-typecasting.[^].

Shortly: an object of class T is in the same time an instance of all ancestor classes in the hierarchy leading from the root to T. If you want to handle the object, you need a reference to it. A reference has it's own class type. If you want to access an object of above type T, you can use references of any type in the hierarchy from the root to up to T (this way you will access the object as that type). Now, if you want to access the object in a different way than you specified by the reference declaration, you need type casting.


这篇关于访问继承的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆