扩展抽象类

时间:2015-02-07 02:09:59

标签: java arraylist abstract

MyMath的构造函数应该调用Homework的构造函数,但是super();返回错误'找不到符号'。它不应该有任何论据。 另外,我对如何使用arraylist调用createAssignment方法感到困惑,但我必须使用它。有什么建议吗?

作业

public abstract class Homework {
    private int pagesToRead;
    private String typeHomework;

    public Homework(int pages, String hw) {
        // initialise instance variables
        pagesToRead = 0;
        typeHomework = "none";
    }

    public abstract void createAssignment(int p);

    public int getPages() {
        return pagesToRead;
    }

    public void setPagesToRead(int p) {
        pagesToRead = p;
    }

    public String getTypeHomework() {
        return typeHomework;
    }

    public void setTypeHomework(String hw) {
        typeHomework = hw;
    }


}

MYMATH

public class MyMath extends Homework {

    private int pagesRead;
    private String typeHomework;

    public MyMath() {
        super();
    }

    public void createAssignment(int p) {
        setTypeHomework("Math");
        setPagesToRead(p);
    }

    public String toString() {
        return typeHomework + " - " + pagesRead;
    }
}

public class testHomework {
    public static void main(String[] args) {
        ArrayList<Homework> list = new ArrayList<Homework>();
        list.add(new MyMath(1));
        list.add(new MyJava(1));

        for (Homework s : list) {
            s.createAssignment();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

编译错误:

关于编译器错误,您必须将MyMath约束器更改为:

 public MyMath() {
        super(someInt, someString);
    }

或者,您可以在Homework类中添加非arg构造函数:

 public Homework() {
        this(someInt,someString);
    }

您可以在Javadocs tutoriel中了解super()关键字:

  

如果构造函数没有显式调用超类构造函数,   Java编译器自动插入对无参数的调用   超类的构造函数。如果超级班没有   无参数构造函数,您将得到编译时错误。宾语   确实有这样的构造函数,所以如果Object是唯一的超类,   没问题。

代码建议:

由于您的问题还有很多其他问题,我修改了您的所有课程,如下所示:

Homework.java:

public abstract class Homework {
    private int pagesToRead;
    private String typeHomework;

    {
        // initialise instance variables
        pagesToRead = 0;
        typeHomework = "none";
    }

    public Homework(int pages, String hw) {

        this.pagesToRead = pages;
        this.typeHomework = hw;

    }

    public abstract void createAssignment(int p);

    public int getPages() {
        return pagesToRead;
    }

    public void setPagesToRead(int p) {
        pagesToRead = p;
    }

    public String getTypeHomework() {
        return typeHomework;
    }

    public void setTypeHomework(String hw) {
        typeHomework = hw;
    }

}

MyMath.java

public class MyMath extends Homework {

    private int pagesRead;
    private String typeHomework;

    public MyMath(int pages, String hw) {
        super(pages,hw);
    }

    public void createAssignment(int p) {
        setTypeHomework("Math");
        setPagesToRead(p);
    }

    public String toString() {
        return typeHomework + " - " + pagesRead;
    }
}

TestHomework.java:

class TestHomework {
    public static void main(String[] args) {
        ArrayList<Homework> list = new ArrayList<Homework>();

        // will create a homework with type Math and one page to read
        list.add(new MyMath(1,"Math"));

        // Assuming MyJava is similar to MyMath
        list.add(new MyJava(1,"Java"));


        for (Homework s : list) {

            if (s instanceof MyMath) {
            // modify the number of pages to read for the Math homework
            s.createAssignment(3);
            } else if (s instanceof MyJava) {
            // modify the number of pages to read for the Java homework
            s.createAssignment(5);
            } else {
            s.createAssignment(7);
            }
        }
    }

}