java 初始化抽象类的ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36357287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initialize ArrayList of Type Abstract class
提问by Naved Ali
I want to fill values in my arraylist of abstract class type.Below is my code
我想在我的抽象类类型的数组列表中填充值。下面是我的代码
public abstract class Account {
private int accountId;
private int customerId;
private double balance;
public Account(int accountId, int customerId, double balance) {
this.accountId = accountId;
this.customerId = customerId;
this.balance = balance;
}
public abstract double deposit(double sum);
public abstract double withdraw(double sum);
}
Above is my abstract class.
Now I have another class bank
in which i want to define and declare an arraylist
in which i can fill my values .
I declared arraylist as
以上是我的抽象类。现在我有另一个类bank
,我想在其中定义和声明an arraylist
我可以填充我的值。我将 arraylist 声明为
ArrayList<Account> al=new ArrayList<>();
Now i want to pass values to this arraylist for further use but i couldnot as we cannot instantiate abstract class.I tried this code to fill values in class with main method but couldnot get it because of above reason
现在我想将值传递给这个数组列表以供进一步使用,但我不能,因为我们无法实例化抽象类。我尝试使用此代码使用 main 方法填充类中的值,但由于上述原因无法获取它
Account ac= new Account(1,100,25000);
ArrayList<Account>ac= new ArrayList<Account>();
ac.add(ac);
采纳答案by JanLeeYu
You can add this following code just to get you started :
您可以添加以下代码以开始使用:
public class ConcreteAccount extends Account{
public ConcreteAccount (int accountId, int customerId, double balance) {
super(accountId, customerId, balance);
}
public abstract double deposit(double sum) {
//implementation here
}
public abstract double withdraw(double sum) {
//implementation here
}
}
Then after that, you can have :
然后在那之后,你可以有:
Account ac= new ConcreteAccount(1,100,25000);
ArrayList<Account> acList= new ArrayList<Account>();
acList.add(ac);
回答by Vytautas
Abstract classes can't be instantiated, but they can be extended. If the child class is concrete, it can be instantiated.
抽象类不能实例化,但可以扩展。如果子类是具体的,它可以被实例化。
You would also have to implement both your abstract methods to make the class concrete.
您还必须实现两个抽象方法才能使类具体化。
Read more here: Java inheritance.
在此处阅读更多信息:Java 继承。
回答by Ji aSH
The whole point of your abstract class is to factorize some code in your application. Using it as a super type is in my opinion a bad practice since you should be using interfaces for that.
抽象类的全部意义在于分解应用程序中的一些代码。在我看来,将它用作超类型是一种不好的做法,因为您应该为此使用接口。
To get a complete response to your problem, I would:
为了得到对你的问题的完整答复,我会:
Create an interface: Account.java
创建接口:Account.java
public interface Account {
public double deposit(double sum);
public double withdraw(double sum);
}
Create an abstract class: AbstractAccount.java
创建一个抽象类:AbstractAccount.java
public abstract class AbstractAccount {
protected int accountId;
protected int customerId;
protected double balance;
public Account(int accountId, int customerId, double balance) {
this.accountId = accountId;
this.customerId = customerId;
this.balance = balance;
}
}
And finally provide a default implementation for your interface BankAccount.java
最后为你的接口 BankAccount.java 提供一个默认实现
public class BankAccount extends AbstractAccount implements Account {
public Account(int accountId, int customerId, double balance) {
super(accountId, customerId, balance);
}
public double deposit(double sum) {
this.balance += sum;
}
public double withdraw(double sum) {
this.balance -= sum;
}
}
Then you should manipulate:
那么你应该操作:
List<Account> accounts = new ArrayList<Account>();
accounts.add(new BankAccount(1, 1, 10000));
and never care about the implementing type :)
并且从不关心实现类型:)
回答by Keqiang Li
Marking a class abstract means it might have unimplemented methods, therefore, you're not able to create an instance of an abstract class directly due to undefined behavior. What you can do is to define a non-abstract class which extends your Account
class and implement the two abstract methods in Account
. Something like class BankAccount extends Account { implementations }
. After that, you can create instances of class BankAccount
and add them into your ArrayList
instance. Other instances of class that extends Account
can also be added into your ArrayList
instance.
将类标记为抽象意味着它可能具有未实现的方法,因此,由于未定义的行为,您无法直接创建抽象类的实例。您可以做的是定义一个非抽象类,它扩展您的Account
类并在Account
. 类似的东西class BankAccount extends Account { implementations }
。之后,您可以创建类的实例BankAccount
并将它们添加到您的ArrayList
实例中。扩展类的其他实例Account
也可以添加到您的ArrayList
实例中。