简体   繁体   English

Java中的抽象类-创建对象

[英]Abstract class in Java - creating objects

I'm writing a code in Java using abstract classes for the first time and I have a problem. 我第一次使用抽象类在Java中编写代码,但遇到了问题。 I have a list (array) of users, each of them is an object of userClass. 我有一个用户列表(数组),每个用户都是userClass的对象。 I have created an abstract class 'account' and 2 classes which extends it 'basicAccount' and 'premiumAccount' and I would like to create basic or premium account for every user depending on an input. 我创建了一个抽象类'account'和2个类,将其扩展为'basicAccount'和'premiumAccount',并且我想根据输入为每个用户创建基本或高级帐户。 My problem is that I don't know how to implement userClass, so it would create object of basic or premium class depending on a string 'accountType' Example: 我的问题是我不知道如何实现userClass,因此它将根据字符串'accountType'创建基本或高级类的对象。示例:

public class userClass {
private String email;
private String accountType;
private account account;

public userClass(String email, String accountType){
    this.setEmail(email);
    this.setAccountType(accountType);

    if(accountType.equals("Basic")){
        basicAccount account = new basicAccount();
    }
    else if(accountType.equals("Premium")){
        premiumAccount account = new premiumAccount();
    }
}

Could you please help me? 请你帮助我好吗? Can I put those if's in userClass method? 我可以将这些if放在userClass方法中吗?

A very important concept in Object-oriented Programming is polymorphism . 在面向对象编程中,一个非常重要的概念是多态 Polymorphism allows you to use an object A as if it were an instance of another type B if A and B follow any one of these rules 如果A和B遵循以下任一规则,则多态性使您可以将对象A当作另一类型B的实例来使用

  • B is a class and A's type is a subclass of B B是一个类,A的类型是B的子类
  • B is an interface and A's type implements B B是一个接口,A的类型实现B

In your case, the first rule applies. 您的情况适用第一个规则。 Both basicAccount and premiumAccount are a subclass of account . basicAccountpremiumAccount都是account的子类。 Thus, you can use your premium account and basic account objects as account objects! 因此, 您可以将高级帐户和基本帐户对象用作帐户对象! So just ASSIGN the newly created object to the account field! 因此,只需将新创建的对象分配给account字段即可!

if(accountType.equals("Basic")){
    basicAccount account = new basicAccount();
    this.account = account; // This works! Don't worry!
}
else if(accountType.equals("Premium")){
    premiumAccount account = new premiumAccount();
    this.account = account;
}

A side effect, though, is that after using basicAccount and premiumAccount objects as account s, you cannot access the methods of basicAccount and premiumAccount by directly accessing this.account . 一个副作用,不过,是使用后basicAccountpremiumAccount对象为account S,你不能访问的方法basicAccountpremiumAccount通过直接访问this.account

If you really want to do so, try this method: 如果您确实想这样做,请尝试以下方法:

Say you have a doStuff method defined in basicAccount and you want to call it: 假设您在basicAccount定义了一个doStuff方法,并且您想调用它:

this.account.doStuff();

That does not work because this.account is of type account and account does not define a method called doStuff . 那是行不通的,因为this.accountaccount类型,而account没有定义名为doStuff的方法。

What you need to do is check whether the account field is of type basicAccount : 您需要做的是检查account字段是否为basicAccount类型:

if (this.account instanceof basicAccount)

In the if statement, cast this.account to basicAccount and call the method: 在if语句中, this.accountbasicAccount并调用该方法:

((basicAccount)this.account).doStuff();

Something like this would probably do it: 这样的事情可能会做到:

public class UserClass{
    Account account;

    public UserClass(String accountType){
         if(accountType.equals("Basic"){
              this.account = new basicAccount();
         } else {
              this.account = new premiumAccount();
         {
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM