简体   繁体   English

如何使用由 class 实现的接口方法,它还扩展了另一个 class?

[英]How to use interface method implemented by a class which also extends another class?

I have some classes named as "Account", "CurrentAccount", "SavingsAccount".我有一些名为“Account”、“CurrentAccount”、“SavingsAccount”的类。 "CurrentAccount" and "SavingsAccount" extends "Account", Also "CurrentAccount" implements an interface "TaxDeduction". “CurrentAccount”和“SavingsAccount”扩展了“Account”,同时“CurrentAccount”实现了一个接口“TaxDeduction”。 "TaxDeduction" has method by name of "deductTax()", whose body is defined in "CurrentAccount". “TaxDeduction”具有名为“deductTax()”的方法,其主体在“CurrentAccount”中定义。

public class CurrentAccount extends Account implements TaxDeduction {
  public void deductTax() {
   double tax = (super.getBalance() * taxRate) / 100;
    super.setBalance(super.getBalance() - tax);
    }
}
public interface TaxDeduction {
    static double taxRate=8.5;
    void deductTax();
}

Now I made an array of Account[] which stores objects of "CurrentAccount" & "SavingsAccount".现在我制作了一个 Account[] 数组,它存储“CurrentAccount”和“SavingsAccount”的对象。 When I retrieve a "CurrentAccount" Object in main class and try to use "deductTax()" method then I get error that "deductTax()" method is not resolved in "Account" whereas I can use all other normal methods in "CurrentAccount" class. How can I resolve this issue?当我在主 class 中检索到“CurrentAccount”Object 并尝试使用“deductTax()”方法时,出现错误“deductTax()”方法未在“帐户”中解析,而我可以在“CurrentAccount”中使用所有其他常规方法" class。我该如何解决这个问题?

Java is a statically typed language. Java 是一种静态类型语言。 If you have a variable of type Account , you can only call methods defined in Account (and its superclasses and implemented interfaces).如果您有一个Account类型的变量,则只能调用在Account (及其超类和实现的接口)中定义的方法。 Attempting to call a method not defined in Account will result in a compiletime error, because as far as the compiler is concerned, the value held in the variable is only an Account .尝试调用未在Account中定义的方法将导致编译时错误,因为就编译器而言,变量中保存的值只是一个Account

So, the compiler will not allow you to call methods of TaxDeduction , then either Account (or one of its superclasses) must implement it, or you must check if the instance held by the variable is an instance of TaxDeduction (using instanceof ), and then cast to TaxDeduction and call the method.因此,编译器将不允许您调用TaxDeduction的方法,然后Account (或其超类之一)必须实现它,或者您必须检查变量持有的实例是否是TaxDeduction的实例(使用instanceof ),并且然后转换为TaxDeduction并调用该方法。

When you use instanceof you check the actual type at runtime, and the cast tells the compiler that you're sure it's actually a TaxDeduction , so you can call the method(s) defined in TaxDeduction .当您使用instanceof时,您会在运行时检查实际类型,并且强制转换告诉编译器您确定它实际上是一个TaxDeduction ,因此您可以调用TaxDeduction中定义的方法。 When you're wrong about the type in the cast, you'll get a runtime exception, ClassCastException (which is why it's recommended to use instanceof before casting).当您对转换中的类型有误时,您将得到运行时异常ClassCastException (这就是为什么建议在转换前使用instanceof )。

In other words, something like:换句话说,类似于:

Account[] accounts = ...;
for (Account account : accounts) {
    if (account instanceof TaxDeduction) {
        ((TaxDeduction) account).deductTax();
    }
}

Or in Java 16 and higher ( JEP 394 ):或者在 Java 16 及更高版本( JEP 394 )中:

Account[] accounts = ...;
for (Account account : accounts) {
    if (account instanceof TaxDeduction td) {
        td.deductTax();
    }
}

暂无
暂无

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

相关问题 扩展可序列化接口的POJO类也称为POJO类? - A POJO class which extends Serializable Interface is also called as POJO class? 如何使用实体类扩展房间中的另一个类? - How to use entity class which extends another class in a Room? 我怎样才能保证实现接口的类也扩展了类? - How can I guarantee that a class that implements an interface also extends a class? 如何在已经扩展了另一个类的活动中使用片段? - How to use fragments in an activity which already extends another class? Java:如何在主类中调用方法,而该方法在另一个类中,该类扩展了抽象类 - Java: How to call upon a method in a main class where the method is in another class which extends an abstract class 如何知道哪个类实现了接口? - How to know which class implemented the interface? 实现接口z的扩展类y的java类x也应该实现接口z - Should a java class x that extends class y, which implements interface z, also implement interface z 如何访问用Java实现的超类的字段,它也有一个同名的方法? - How to access a field of a super-class which is implemented in Java which also has an equal-named method? 从另一个类调用一个扩展Thread的类的方法 - Calling a method of a class which extends Thread, from another class 如何为扩展JDialog的类(或通常的其他类)创建接口 - How to create an Interface for a class that extends JDialog (or in general another class)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM