在java中转换为父类型时调用子方法
我在尝试完成一些课程作业时遇到问题,任何帮助将不胜感激!
我有 3 种类型的帐户,它们扩展了抽象类型“帐户”.. [CurrentAccount、StaffAccount 和 MortgageAccount]。
我正在尝试从文件中读取一些数据并创建帐户对象以及用户对象以添加到存储在程序中的哈希图。
当我创建帐户对象时,我使用 Account 类型的临时变量,并根据读入的数据定义其子类型。
例如:
Account temp=null;
if(data[i].equalsIgnoreCase("mortgage"){
temp= new MortgageAccount;
}
问题是当我尝试调用属于 MortgageAccount 类型的方法时。
我是否需要每个变量的临时变量输入 StaffAccount MortgageAccount 和 CurrentAccount 并相应地使用它们以便使用它们的方法?
提前致谢!
I am having problems with some course work i'm trying to finish off and any help would be appreciated!
I have 3 types of accounts which extend an abstract type "Account".. [CurrentAccount, StaffAccount and MortgageAccount].
I am trying to read in some data from file and create account objects along with user objects to add to hashmaps stored within the program.
When I create account objects I use a temporary variable of type Account and define its subtype dependant on the data read in.
for example:
Account temp=null;
if(data[i].equalsIgnoreCase("mortgage"){
temp= new MortgageAccount;
}
Problem is when I try to call a method belonging to type MortgageAccount..
Do I need a temp variable of each type, StaffAccount MortgageAccount and CurrentAccount and use them coresspondingly in order to use their methods?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的所有帐户对象都具有相同的接口,这意味着它们声明相同的方法,并且仅在实现方式上有所不同,那么您不需要为每种类型使用变量。
但是,如果您想调用特定于子类型的方法,那么您将需要该类型的变量,或者您需要先转换引用,然后才能调用该方法。
If all your account objects have the same interface, meaning they declare the same methods and they only differ in how they are implemented then you do not need a variable for each type.
However, if you want to call a method that is specific to the sub-type then you would need a variable of that type, or you would need to cast the reference before you would be able to call the method.
这取决于。如果父类
Account
在MortgageAccount
中重写了一个方法,那么当您调用该方法时,您将获得MortgageAccount
版本。如果该方法仅存在于MortgageAccount
中,那么您需要转换该变量才能调用该方法。It depends. If the parent class
Account
has a method overridden inMortgageAccount
, then when you call the method you'll get theMortgageAccount
version. If the method only exists inMortgageAccount
, then you'll need to cast the variable to call the method.您所需要的只是一个
MortgageAccount
类型的对象来调用其方法。您的对象temp
的类型为MortgageAccount
,因此您只需调用该对象上的MortageAccount
和Account
方法即可。不需要铸造。All you need is an object of type
MortgageAccount
to call methods on it. Your objecttemp
is of typeMortgageAccount
so you can just callMortageAccount
andAccount
methods on that object. No casting is needed.这是
动态方法分派
的概念,如果您使用基类的引用变量并创建派生类的对象,那么您只能访问在基类中定义或至少声明的方法,并且您正在重写它们在派生类中。
要调用派生类的对象,您将需要派生类的引用变量。
This is concept of
Dynamic method dispatch
If you are using Reference Variable of Base class and making object of Derived class then you can only access methods that are defined or at least declared in the base class and you are overriding them in derived class.
To call objects of derived class, you will need reference variable of derived class.