静态方法Java混淆

时间:2015-03-03 17:32:59

标签: java methods static

房间等级:

public abstract class Room {
    public abstract int cost();
    public static Room mostExpensive = null;
    public static int highestCost = 0;
    public static Room mostExpensive() {
        return mostExpensive;
    }
}

实验班:

public class Lab extends Room{
    public final int LAB_CHAIR_PRICE = 50;
    int labChairs;
    int computers;
    private final int COMPUTER_COST = 1000;

     public Lab(int labChairs, int computers) {
        this.labChairs = labChairs;
        this.computers = computers;
            if (this.cost() > highestCost) {
                mostExpensive = this;
                highestCost = this.cost();
            }        
        }
     public int cost() {
        return ((LAB_CHAIR_PRICE * labChairs) + (COMPUTER_COST * computers));
    }
}

LectureHall Class:

public class LectureHall extends Room {
    private final int LECTURE_CHAIR_PRICE = 100;
    int lectureChairs;
    boolean isData;
    private final int DATA_PROJECTOR_COST = 5000;
    int dataProjector;
    public LectureHall(int lectureChairs, boolean isData) {
        this.lectureChairs = lectureChairs;
        if (this.isData = isData) {
            this.dataProjector = 1;
            if (this.cost() > highestCost) {
                mostExpensive = this;
                highestCost = this.cost();
            }        
        }
    }
    public int cost(){
        return ((LECTURE_CHAIR_PRICE * lectureChairs) + (DATA_PROJECTOR_COST * dataProjector));
    }
}

测试类

public class Test {
    public static void main(String[] args) {
        // An array of rooms for a proposed new building
        Room rooms[] = new Room[4];
        // small lecture hall with a data projector
        rooms[0] = new LectureHall(40, true);
        // large lecture hall seating 500, with no data projector
        rooms[1] = new LectureHall(500, false);
        // lab with seats for 50 and a computer for every two
        rooms[2] = new Lab(50,25);
        // smaller lab with seats for 10 and a computer for each
        rooms[3] = new Lab(10,10);

        int totalCost = 0;
        for (int i = 0; i < rooms.length; i++) {
            System.out.println("room " + i + " costs $" + rooms[i].cost());
            totalCost += rooms[i].cost();
        }
        System.out.println("total cost: $" + totalCost);
        Room r = Room.mostExpensive(); // the most expensive room created
        // by the program
        System.out.println("the most expensive room costs $" + r.cost());
    }
}

所以我对最昂贵的方法()有点麻烦。它是一个静态方法,我在房间类中声明它,但它被其他方法使用。它应该找到最昂贵的房间。然而,它没有找到最昂贵的,它告诉我27500是最昂贵的(不是)。我还有一个关于Room类的问题。在构造函数中我设置了mostExpensive = this。那样有用吗?我对所有这些如何运作以及如何正确地做到这一点感到有些困惑。

1 个答案:

答案 0 :(得分:0)

在此构造函数中,只有在有投影仪时才会修改“最昂贵”。

public LectureHall(int lectureChairs, boolean isData){
    this.lectureChairs = lectureChairs;
    if (this.isData = isData){
        this.dataProjector = 1;
        if (this.cost() > highestCost){
            mostExpensive = this;
            highestCost = this.cost();
        }        
    }
}

修正:

public LectureHall(int lectureChairs, boolean isData){
    this.lectureChairs = lectureChairs;
    if (this.isData = isData){
        this.dataProjector = 1;
    }
    if (this.cost() > highestCost){
        mostExpensive = this;
        highestCost = this.cost();
    }        
}

但是不应该在单个对象的构造函数中计算整体状态。这是糟糕的设计。跟踪某些房间的房间 - 房间或新的东西,例如建筑物。然后从Room中的任何东西计算“最贵”。想象一下:如果移除投影仪会发生什么?或者在另一个现有的演讲室中添加?制造错误的可能性非常大。