[英]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.