【问题标题】:Abstract class in Java - creating methodsJava中的抽象类——创建方法
【发布时间】:2016-07-22 10:52:42
【问题描述】:

我有一个关于 Java 中抽象类的问题。 我创建了这样的抽象类:

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();
     }
}

如您所见,我有两种类型的帐户:基本帐户和高级帐户,它们都是帐户抽象类的扩展。现在我只想将方法添加到高级课程,但是当我尝试这样做时,我收到一个错误,说我必须在帐户类中实现它,这是我不想要的。是否有可能以其他方式做到这一点?避免在 account 和 basicAccount 类中放置空方法?

【问题讨论】:

  • 班级不是abstract...您发布了正确的代码吗?
  • 没有理由不能将方法添加到抽象类的一个扩展中,但不能添加到基类中。请发布导致错误的实际代码以获得更多帮助。
  • 请添加account 类的定义。
  • 账户类是一个抽象类。我想为 account.shareFile(fileReceiver,fileName); 创建方法但我不想在基本帐户中使用该方法,因为我的假设是基本帐户不允许此共享。
  • @gariaable 请发布实际上给您错误的代码。没有它,我们真的无能为力。

标签: java oop object abstract-class abstract


【解决方案1】:

如果你有一个Account 类,比如:

public abstract class Account{
    public abstract void doSomethingAccountLike();
    //more stuff
}

你可以有子类:

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
    }
}

doSomethingPremiumLike() 方法仅在您有 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();
    }
}

您的问题

在我看来,您的问题很可能是在 UserClass 类中,您有一个字段 Account account。在您的构造函数中,您将new BasicAccount()new PremiumAccount() 分配给字段account。这很好,但是一旦你这样做了,编译器在任何给定情况下都不再知道account 字段是指PremiumAccount 实例还是BasicInstance这意味着如果你尝试调用account.doSomethingPremiumLike(),你会得到一个编译器错误。你可以在运行时绕过这个限制:

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();
}

注意:我已将您的类名称更改为以大写字母开头,这是 Java 和大多数其他 OOP 语言中的通用约定。养成遵循此约定的习惯以帮助他人阅读您的代码是件好事。

【讨论】:

  • 是的,完全正确。这就是它的样子,但是在我的代码中,当我创建对象时,我使用 account newAccount = new basicAccount();因此我收到一条错误消息,说我必须将这个新方法 doSomethingPremiumLike() 也放在帐户抽象方法中(以及我想避免的基本帐户中的后续内容)
  • 你应该反过来创建它 - BasicAccount account = new PremiumAccount()。如果您想使用某些特定方法,然后将帐户变量转换为 PremiumAccount。
  • 但是可以用这种方式创建对象吗? Eclipse 向我展示了要创建 premiumAccount() 的新对象,它必须类似于“帐户帐户”或“基本帐户帐户”
  • 现在完美了!非常感谢!
  • @gariaable:我相信我知道您的问题出在哪里。查看我刚刚在 Your Issue 之后添加的内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 2016-07-21
  • 2017-04-10
  • 2011-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多