调用方法后,Int值不会改变

时间:2015-02-07 17:37:02

标签: java abstract-class implements

在调用doReading()之后,页面的值应该改变,但它不会改变。是否可以在不声明int页面的情况下执行此操作;在MyMath2和MyScience2类中。输出应该是这样的: 读之前: 数学 - 必读5页 看完之后: 数学 - 必读3页

抽象作业2

 public abstract class Homework2 implements Processing
{
  private int pagesToRead;
  private String typeHomework;

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

 public Homework2(int pages, String hw)
 {
    pagesToRead = pages;
    typeHomework = hw;
 }

 public abstract void createAssignment(int pages);

 public int getPages() 
 {
    return pagesToRead;
 }

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

 public String getTypeHomework() 
 {
    return typeHomework;
 }

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

 public String toString() 
 {
    return "reading: \n" + typeHomework + " - must read " + pagesToRead + " pages";
 }

}

TestHomework2

public class TestHomework2
{

public static void main(String []args)
{   

  List<Homework2> tester = new ArrayList<Homework2>();

 tester.add( new MyMath2(5, "Math"));                 
 tester.add( new MyScience2(7, "Science"));


 for (Homework2 c: tester)
  {
    System.out.print("Before ");
    System.out.println(c);
    c.doReading();
    System.out.print("After ");
    System.out.println(c); 
  }


}
}

界面处理

public interface Processing
{
 void doReading()
}

MyMath2

 public class MyMath2 extends Homework2 implements Processing
 {
  int pages; 

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

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

 public void doReading()
 {
   pages = pages - 2;
 }
 }

MyScience2

public class MyScience2 extends Homework2 implements Processing
{
   int pages;

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

 public void createAssignment(int pages) 
 {
    setTypeHomework("Science");
    setPagesToRead(pages);
 }

 public void doReading()
 {
  pages = pages - 3; 
 }
}

3 个答案:

答案 0 :(得分:1)

不会覆盖字段。这意味着在调用pagesToRead时,初始doReading()不会更新,因为该方法会更改pages

你应该这样做:

public void doReading(){
    setPagesToRead(getPages() - 2);
}

答案 1 :(得分:1)

System.out.print("After ");
System.out.println(c); 

对Homework2的toString()进行分类

public String toString() 
{
     return "reading: \n" + typeHomework + " - must read " + pagesToRead + "     pages";
}

你返回pagesToRead。但是在doReading中你减少了页面而不是pagesToRead:

public void doReading()
{
     pages = pages - 2; 
}

reduce pagesToRead:

public void doReading()
{
     pagesToRead = pagesToRead - 2; 
}

答案 2 :(得分:1)

当您通过调用doReading更改页数时,您正在更改MyMath2对象中的页面值(MyMath2对象的属性)。您正在打印的数字来自Homework2.pagesToRead字段。

当您调用doReading方法时,Homework2中的此字段永远不会更改,因此您获得相同的值。您可以通过设置在MyMath2对象中设置的值来解决此问题,如:

public void doReading(){
    pages = pages - 2;
    super.setPagesToRead(pages);
}

或者您从MyMath2中完全删除了pages字段,并使用父对象中的字段,如:

public void doReading(){
    super.setPagesToRead(super.getPagesToRead() - 2);
}

同样适用于MyScience2课程。