简体   繁体   English

Java继承调用对象的超类的方法

[英]Java inheritance call object's superclass's method

I have a class A: 我有一个A类:

A
- void method1

And a class B that extends A that overrides A's method1: 并且扩展A的B类覆盖A的方法1:

B->A
- void method1

Later on, I create an instance of B that is referenced as an A: 稍后,我创建了一个引用为A的B实例:

A a = new B();

I want to use A's version of method1 in a specific case. 我想在特定情况下使用方法1的A版本。 Is there a way to call this, like: 有没有办法称之为,如:

a.super.method1();

Edit: 编辑:

To clear up any confusion, here is what I am trying to accomplish: 为了消除任何困惑,这是我想要实现的目标:

I have a parent class called Account. 我有一个名为Account的父类。 Beneath that I have 3 subclasses, SavingsAccount, CheckingAccount, and MoneyMarketAccount. 在那之下我有3个子类,SavingsAccount,CheckingAccount和MoneyMarketAccount。

Each of these has its own deposit/withdraw methods that have specific fees associated to them. 其中每一种都有自己的存款/取款方式,这些方法具有与之相关的特定费用。

There is a manager class called Bank that has an ArrayList of Account objects in it. 有一个名为Bank的管理器类,其中包含一个Account对象的ArrayList。 I have a method, void transfer that I am attempting to create that will allow me to take the two accounts and transfer the money between them. 我有一个方法,无法void transfer ,我试图创建,这将允许我拿两个帐户,并在他们之间转移资金。

Sometimes the different methods will throw exceptions if there are insufficient funds or other errors, so I need to be able to reverse a transaction if it fails for one of the two accounts. 如果没有足够的资金或其他错误,有时不同的方法会抛出异常,所以如果两个账户中的一个账户失败,我需要能够撤销交易。

This is where I am having problems: 这是我遇到问题的地方:

I need to redeposit the money withdrawn from the from-account if the to-account can't receive deposits, but the to-account may have deposit fees, so I would like to just call the parent Account class's deposit method, as it does not have any fees associated; 如果to-account无法收到存款,我需要重新存入从from-account提取的款项,但是to-account可能有存款费用,所以我想拨打父账户类的存款方式,就像它一样没有任何费用; however, from the comments and answers that I have received, I may have to just write another method that is not overridden that simply performs the same operation as Account's deposit. 但是,根据我收到的评论和答案,我可能只需编写另一种不被覆盖的方法,只需执行与帐户存款相同的操作即可。

You can't use super directly since it only exists inside of B. But you can do it indirectly: 你不能直接使用super,因为它只存在 B 内部 。但是你可以间接地使用它:

public B extends A {
  public void method1() {
    // B's method 1
  }

  public void superMethod1() {
     super.method1();
  }
}

So if a class uses a B variable, then they can call the superMethod1() and get A's method1. 因此,如果一个类使用B变量,那么他们可以调用superMethod1()并获取A的method1。

But having said this, this won't work on A variables, as your code is currently set up, not without casting. 但话虽如此,这对A变量不起作用,因为您的代码当前已设置,而不是没有强制转换。

ie, you could do: 即,你可以这样做:

B b = new B();
b.superMethod1();

But this won't work for your set up: 但是这对你的设置不起作用:

A a = new B();
// not without casting
((B)a).superMethod(); // ugh... ugly! AND dangerous!

Note that your question, while itself not bad, suggests possibly a bad program design and code needing this has at least for me, a bad code smell. 请注意,你的问题,虽然本身并不坏,但表明可能是一个糟糕的程序设计和需要它的代码至少对我来说,代码味道很差。

You can have super or this keywords only with in the class. 您只能在课堂上使用superthis关键字。 You cannot use them outside of that class. 不能在该课程之外使用它们。

So with in the class you can do (Mock code) 所以在课堂上你可以做(​​模拟代码)

class A {    
  someMethod(){    
  super. someMethod()
 }
}

You can't do that from the client code, ie. 你不能从客户端代码那里做到这一点,即。 the code calling the method. 调用方法的代码。 You can only do it within the sub class method 您只能在子类方法中执行此操作

super.method1();

and only for one level up the hierarchy. 并且仅适用于层次结构的一个级别。

use A a = new A(); 使用A a = new A(); or you can call super.method1() from B's method1 if you want to go like A a = new B(); 或者你可以从B的方法1调用super.method1(),如果你想要像A a = new B();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JAVA:当我调用 object 的 getter 方法时发生了什么,它实际上是从它的超类扩展方法 - JAVA: what happend when i call a getter method of a object which actually extend the method from it's superClass 在层次结构之外调用对象的超类方法 - Call an object's superclass methods outside the hierarchy Java 继承 - 调用超类方法 - Java Inheritance - calling superclass method 继承调用超类方法java - Inheritance calling superclass method java 有什么方法可以通过子类对象调用超类的方法,而不必通过同名的子类方法吗? - Is there any way to call superclass's method on a subclass object without going through the subclass method of the same name? 在Java中,有没有更简单的方法可以在子类和超类的ArrayList上调用子类的方法 - Is there an easier way to call a subclass's method on an ArrayList of both the subclass and superclass in Java Java Inheritance:子类中的方法覆盖超类中的方法 - Java Inheritance: method in subclass overrides method in superclass 尝试调用超类方法,但传递子类的参数? - Trying to call on a superclass method, but pass a subclass's parameters? Java 继承:在超类中调用子类方法 - Java Inheritance: Calling a subclass method in a superclass Java继承; 将子类传递给超类的抽象方法 - Java inheritance; passing a subclass to an abstract method of a superclass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM