在java中转换为父类型时调用子方法

发布于 2024-12-17 07:20:33 字数 506 浏览 2 评论 0原文

我在尝试完成一些课程作业时遇到问题,任何帮助将不胜感激!

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哎呦我呸! 2024-12-24 07:20:33

如果您的所有帐户对象都具有相同的接口,这意味着它们声明相同的方法,并且仅在实现方式上有所不同,那么您不需要为每种类型使用变量。

但是,如果您想调用特定于子类型的方法,那么您将需要该类型的变量,或者您需要先转换引用,然后才能调用该方法。

class A{
    public void sayHi(){ "Hi from A"; }
}


class B extends A{
    public void sayHi(){ "Hi from B"; 
    public void sayGoodBye(){ "Bye from B"; }
}


main(){
  A a = new B();

  //Works because the sayHi() method is declared in A and overridden in B. In this case
  //the B version will execute, but it can be called even if the variable is declared to
  //be type 'A' because sayHi() is part of type A's API and all subTypes will have
  //that method
  a.sayHi(); 

  //Compile error because 'a' is declared to be of type 'A' which doesn't have the
  //sayGoodBye method as part of its API
  a.sayGoodBye(); 

  // Works as long as the object pointed to by the a variable is an instanceof B. This is
  // because the cast explicitly tells the compiler it is a 'B' instance
  ((B)a).sayGoodBye();

}

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.

class A{
    public void sayHi(){ "Hi from A"; }
}


class B extends A{
    public void sayHi(){ "Hi from B"; 
    public void sayGoodBye(){ "Bye from B"; }
}


main(){
  A a = new B();

  //Works because the sayHi() method is declared in A and overridden in B. In this case
  //the B version will execute, but it can be called even if the variable is declared to
  //be type 'A' because sayHi() is part of type A's API and all subTypes will have
  //that method
  a.sayHi(); 

  //Compile error because 'a' is declared to be of type 'A' which doesn't have the
  //sayGoodBye method as part of its API
  a.sayGoodBye(); 

  // Works as long as the object pointed to by the a variable is an instanceof B. This is
  // because the cast explicitly tells the compiler it is a 'B' instance
  ((B)a).sayGoodBye();

}
云淡月浅 2024-12-24 07:20:33

这取决于。如果父类 AccountMortgageAccount 中重写了一个方法,那么当您调用该方法时,您将获得 MortgageAccount 版本。如果该方法仅存在于 MortgageAccount 中,那么您需要转换该变量才能调用该方法。

It depends. If the parent class Account has a method overridden in MortgageAccount, then when you call the method you'll get the MortgageAccount version. If the method only exists in MortgageAccount, then you'll need to cast the variable to call the method.

烟酒忠诚 2024-12-24 07:20:33

您所需要的只是一个 MortgageAccount 类型的对象来调用其方法。您的对象 temp 的类型为 MortgageAccount,因此您只需调用该对象上的 MortageAccountAccount 方法即可。不需要铸造。

All you need is an object of type MortgageAccount to call methods on it. Your object temp is of type MortgageAccount so you can just call MortageAccount and Account methods on that object. No casting is needed.

舟遥客 2024-12-24 07:20:33

这是动态方法分派的概念,

如果您使用基类的引用变量并创建派生类的对象,那么您只能访问在基类中定义或至少声明的方法,并且您正在重写它们在派生类中。

要调用派生类的对象,您将需要派生类的引用变量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文