用两个抽象类继承

时间:2016-03-08 01:34:52

标签: java

我试图创建一个至少包含每个类的两个实例的arraylist,我需要使用一个arraylist来实现这一点。问题是类HourlyWorker和ContractWorker都实现了抽象类Smarty,其中Class Hobbit实现了抽象类Simpleton,我无法弄清楚如何在一个arraylist中使用它们。

public class MainClass {

private ArrayList<Smarty> list;

    public MainClass() {
        list = new ArrayList<Smarty>();
        list.add(new HourlyWorker("Bob", 50, 40, 8.0));
        list.add(new HourlyWorker("Bill", 70, 50, 12.0));
        list.add(new ContractWorker("Jill", 80, 8, 100.0));
        list.add(new ContractWorker("Amy", 125, 12, 85.0));
        list.add(new Hobbit("Frodo", 12));
        list.add(new Hobbit("Merry", 8));
    }

无效的部分是

list.add(new Hobbit("Frodo", 12));
list.add(new Hobbit("Merry", 8));

抽象类Smarty也实现了Simpleton

我所有的课程

抽象类Smarty

public abstract class Smarty extends Simpleton {
    private int IQ;
    public Smarty(){

    }

    public Smarty(String name, int IQ) {
        setFirstName(name);
        this.IQ = IQ;
    }

    public Smarty(int IQ){
        this();
        this.IQ = IQ;
    }

    public int getIQ(){
        return IQ;
    }
    public void setIQ(int IQ){
        this.IQ = IQ;
    }

    public abstract double getIncome();

}

抽象类Simpleton

public abstract class Simpleton {
    private String name;
    public Simpleton(){

    }
    public Simpleton(String name){
        this.name = name;
    }
    public String getFirstName(){
        return name;
    }
    public void setFirstName(String name){
        this.name = name;
    }
    public abstract String getName();

    public abstract String doMath();

    public abstract String saySomethingSmart();
}

class HourlyWorker

public class HourlyWorker extends Smarty{
private int hoursWorked;
private double hourlyPay;

public HourlyWorker(String name,int IQ, int hoursWorked, double hourlyPay){
    super(IQ);
    setFirstName(name);
    setHoursWorked(hoursWorked);
    this.hourlyPay = hourlyPay;
}

public int getHoursWorked(){
    return hoursWorked;
}
public void setHoursWorked(int hoursWorked){
    this.hoursWorked = hoursWorked;
}
public double getHourlyPay(){
    return hourlyPay;
}
public void setHourlyPay(double hourlyPay){
    this.hourlyPay = hourlyPay;
}
public String multiply(int x, int y){
    return x + " x " + y + " = " + x * y;
}

@Override
public String getName() {
    // TODO Auto-generated method stub
    return getFirstName();
}

@Override
public String doMath() {
    // TODO Auto-generated method stub
    return "I can Multiply! " + multiply(2, 3);
}

@Override
public String saySomethingSmart() {
    // TODO Auto-generated method stub
    return "Hippos are dangerous!";
}

@Override
public double getIncome() {
    // TODO Auto-generated method stub
    double x = getHoursWorked();
    double y = getHourlyPay();
    double income = x * y;
    return income;
}

}

类ContractWorker

public class ContractWorker extends Smarty{
private int contractsCompleted;
private double payPerContract;

public ContractWorker(String name,int IQ, int contractsCompleted, double payPerContract) {
    // TODO Auto-generated constructor stub
    super(IQ);
    setFirstName(name);
    this.contractsCompleted = contractsCompleted;
    this.payPerContract = payPerContract;
}
public int getContractsCompleted(){
    return contractsCompleted;
}
public void setContractsCompleted(int contractsCompleted){
    this.contractsCompleted = contractsCompleted;
}
public double getPayPerContract(){
    return payPerContract;
}
public void setPayPerContract(double payPerContract){
    this.payPerContract = payPerContract;
}
public String division(int x, int y){
    return x + " / " + y + " = " + x/y;
}

public double getIncome() {
    // TODO Auto-generated method stub
    double x = getContractsCompleted();
    double y = getPayPerContract();
    double income = x*y;
    return income;
}
@Override
public String doMath() {
    // TODO Auto-generated method stub
    return ": I can do division! " + division(10,2);
}
@Override
public String saySomethingSmart() {
    // TODO Auto-generated method stub
    return ": The stars are aligned tonight.";
}
@Override
public String getName() {
    // TODO Auto-generated method stub
    return getFirstName();
}

}

班霍比特人

public class Hobbit extends Simpleton{
private int carrots;

public Hobbit(String name, int carrots){
    super(name);
    this.carrots = carrots;
}
public int getCarrotsPicked(){
    return carrots;
}
public void setCarrotsPicked(int carrots){
    this.carrots = carrots;
}
public String add(int x, int y){
    return x + " + " + y + " = " + x+y;
}

@Override
public String getName() {
    // TODO Auto-generated method stub
    return getFirstName();
}

@Override
public String doMath() {
    // TODO Auto-generated method stub
    return "I can add! " + add(2,2);
}

@Override
public String saySomethingSmart() {
    // TODO Auto-generated method stub
    return "Leave me alone Sam!";
}

}

2 个答案:

答案 0 :(得分:0)

这不起作用,因为Hobbit类没有实现抽象类Smarty。

你说“类HourlyWorker和ContractWorker都实现了抽象类Smarty,其中Class Hobbit实现了抽象类Simpleton”,并且Smarty实现了Simpleton。

创建树时很容易看到问题:

Simpleton / \ Smarty Hobbit / \ HourlyWorker ContractWorker

如你所见,霍比特人没有实现抽象类Smarty。

因此,当您创建Smarty对象的ArrayList时,您不能放入Hobbit对象,因为它不是Smarty。

相反,请改为使ArrayList包含Simpleton对象。你的三个非抽象类都是Simpleton的子类,所以你可以将它放在Simpleton ArrayList中。

ArrayList<Simpleton> list = new ArrayList<Simpleton>();

然后,您的添加操作应该可以正常工作。

答案 1 :(得分:0)

使您的列表包含Simpleton类型的对象,因为这是您要在列表中存储的所有对象的共享父级:

public MainClass() {
    list = new ArrayList<Simpleton>();
    list.add(new HourlyWorker("Bob", 50, 40, 8.0));
    list.add(new HourlyWorker("Bill", 70, 50, 12.0));
    list.add(new ContractWorker("Jill", 80, 8, 100.0));
    list.add(new ContractWorker("Amy", 125, 12, 85.0));
    list.add(new Hobbit("Frodo", 12));
    list.add(new Hobbit("Merry", 8));
}