- Вот суперкласс:
Код: Выделить всё
public abstract class ImperialCluster implements Serializable,Clusterable {
private static final long serialVersionUID = 1L;
private String clusterType;
private ArrayList alphas;
private ArrayList betas;
private ArrayList gammas;
public ImperialCluster() {
this.alphas = new ArrayList();
this.betas = new ArrayList();
this.gammas = new ArrayList();
}
public String getClusterType() {
return clusterType;
}
public ArrayList getAlphas() {
return alphas;
}
public ArrayList getBetas() {
return betas;
}
public ArrayList getGammas() {
return gammas;
}
public void setClusterType(String clusterType) {
this.clusterType = clusterType;
}
public void setAlphas(ArrayList alphas) {
this.alphas = alphas;
}
public void setBetas(ArrayList betas) {
this.betas = betas;
}
public void setGammas(ArrayList gammas) {
this.gammas = gammas;
}
@Override
public String toString() {
return this.clusterType
+ "\nCluster Type: " + this.clusterType
+ "Alpha Particles: " + this.alphas.size()
+ "Beta Particles: " + this.betas.size()
+ "Gamma Particles: " + this.gammas.size()
+ gammas + ", getClusterType()=" + getClusterType() + ", getAlphas()=" + getAlphas() + ", getBetas()="
+ getBetas() + ", getGammas()=" + getGammas() + ", getClass()=" + getClass() + ", hashCode()="
+ hashCode() + ", toString()=" + super.toString() + "]";
}
}
- Вот подкласс:
Код: Выделить всё
public class Meson extends ImperialCluster {
private static final long serialVersionUID = 1L;
public Meson() {
Alpha A = new Alpha();
Beta B = new Beta();
Gamma G = new Gamma();
getAlphas().add(A);
this.setAlphas(getAlphas().add(A));
this.setBetas(getBetas().add(B));
this.setClusterType("Meson");
}
@Override
public void displayCluster() {
System.out.println(toString());
}
}
this.setAlphas(getAlphas(). add(A));
выдает ошибку, поскольку .add(A) возвращает логическое значение.
Что можно обойти?
Подробнее здесь: https://stackoverflow.com/questions/787 ... -a-subclas