[英]Abstract class in Java - creating methods
I have a question about abstract classes in Java. 我对Java中的抽象类有疑问。 I have created abstract class like this: 我创建了这样的抽象类:
public class userClass {
private String email;
String accountType;
account account;
public userClass(String email, String accountType){
this.setEmail(email);
this.setAccountType(accountType);
if(accountType.equals("basic")){
this.account = new basicAccount();
}
else if(accountType.equals("premium")){
this.account = new premiumAccount();
}
}
As you see I have accounts of two types: basic and premium, they are both extensions of account abstract class. 如您所见,我有两种类型的帐户:基本帐户和高级帐户,它们都是帐户抽象类的扩展。 Now I would like to add method only to premium class, but when I'm trying to do so, I receive an error saying that I have to implement it in account class, which I don't want. 现在,我只想将方法添加到高级类中,但是当我尝试这样做时,我收到一条错误消息,说我必须在帐户类中实现它,这是我所不希望的。 Is it possible to do it in other way? 有可能以其他方式做到吗? To avoid putting empty method in account and basicAccount classes? 为了避免将空方法放在account和basicAccount类中?
So you if you have an Account
class like: 因此,如果您有一个Account
类,例如:
public abstract class Account{
public abstract void doSomethingAccountLike();
//more stuff
}
You can have child classes: 您可以有子班:
public class BasicAccount extends Account{
public void doSomethingAccountLike(){
//implementation specific to basic accounts
}
}
public class PremiumAccount extends Account{
public void doSomethingAccountLike(){
//implementation specific to premium accounts
}
public void doSomethingPremiumLike(){
//something that only makes sense
// in the context of a premium account
}
}
The method doSomethingPremiumLike()
is only available when you have an instance of a PremiumAccount
ie: doSomethingPremiumLike()
方法仅在拥有PremiumAccount
实例的情况下PremiumAccount
即:
public class AccountDemo{
public static void main(String[] args){
PremiumAccount premium = new PremiumAccount();
BasicAccount basic = new BasicAccount();
Account generalAccount = premium;
//valid- the compiler knows that the
//premium object is an instance of the
//premium class
premium.doSomethingPremiumLike();
//Would cause a compile error if uncommented.
//The compiler knows that basic is an instance
//of a BasicAccount, for which the method
//doSomethingPremiumLike() is undefined.
//basic.doSomethingPremiumLike();
//Would generate a compiler error if uncommented.
//Even though generalAccount actually refers to
//an object which is specifically a PremiumAccount,
//the compiler only knows that it has a reference to
//an Account object, it doesn't know that it's actually
//specifically a PremiumAccount. Since the method
// doSomethingPremiumLike() is not defined for a general
//Account, this won't compile
//generalAccount.doSomethingPremiumLike();
//All compile- all are instances of Account
//objects, and the method doSomethingAccountLike()
//is defined for all accounts.
basic.doSomethingAccountLike();
premium.doSomethingAccountLike();
generalAccount.doSomethingAccountLike();
}
}
Your issue 你的问题
It sounds to me like your issue is likely that in the class UserClass
, you have a field Account account
. 在我看来,您的问题很可能是在UserClass
类中,您有一个字段Account account
。 In your constructor, you assign either a new BasicAccount()
or a new PremiumAccount()
to the field account
. 在构造函数中,您可以将new BasicAccount()
或new PremiumAccount()
分配给字段account
。 This is perfectly fine, but once you do that, the compiler no longer knows in any given case whether the account
field refers to a PremiumAccount
instance or a BasicInstance
. 这样做很好,但是一旦执行此操作, 编译器就不再在任何给定情况下都知道account
字段是引用PremiumAccount
实例还是BasicInstance
。 This means that if you try to call account.doSomethingPremiumLike()
, you will get a compiler error. 这意味着,如果您尝试调用account.doSomethingPremiumLike()
,则会收到编译器错误。 You can get around this restriction at runtime: 您可以在运行时解决此限制:
In UserClass
somewhere: 在UserClass
某处:
if(account instanceof PremiumAccount){
//if we're sure that account is actually a PremiumAccount,
//cast it to a PremiumAccount here to let the compiler know
//that doSomethingPremiumLike() can be called
((PremiumAccount)account).doSomethingPremiumLike();
}
Note: I have changed the names of your classes to start with upper case letters as is the common convention in Java and most other OOP languages. 注意:我已经将类的名称更改为以大写字母开头,这是Java和大多数其他OOP语言中的常见约定。 It's good to get in the habit of following this convention to help others read your code. 养成遵循此约定以帮助他人阅读您的代码的习惯是很好的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.