0

每次创建 House 类的新实例时,我都会尝试创建一个唯一的属性代码。例如,当我在程序中创建第三个房子时,我需要它为它分配整数“3”,以便我可以使用该唯一代码引用该房子。我已经尝试使用全局静态变量进行此操作,并且在保留创建的正确数量的对象的同时,它只返回最后一个实例的值。

private static int houseNo = 0;
private int propertyCode;

public House(String s, Town t, Person o){
    owner = o;
    street = s;
    town = t;
    houseNo++;
    propertyCode = houseNo;
}
public String toString(){
    String temp = "";
    temp = "Code: " + this.getPropCode() + " \nAddress:\n" + this.getStreet() + ", " + town.getTownName();
    return temp;
}

假设我在主类中创建了 6 个房子,使用 toString 访问任何房子只会返回 6 来代替 getPropCode()。

有任何想法吗?

编辑(更多代码):

public int getPropCode(){
    return propertyCode;
}

从我的主要课程:

public static void main(String[] args) {
   House house1 = new House("blueberry", town1, fred);
   House house2 = new House("blackberry", town2, barney);
   House house3 = new House("redberry", town3, fred);
   int whichHouse = Integer.parseInt(JOptionPane.showInputDialog("Select a house to create a lease for \n1. " + house1.toString() + "\n2. " + house2.toString() + "\n3. " + house3.toString()));

请原谅我的命名约定,只是想弄乱这段代码。

4

3 回答 3

1

您是否期望这样,为简单起见更新了属性

public class Test {     
    private static int count = 0;

    public static void main( String [] args) { 
        House h1 = new House("blueberry", "town1", "fred");
        System.out.println(h1.toString());
        House h2 = new House("blackberry", "town2", "barney");
        System.out.println(h2.toString());
        House h3 =new House("redberry", "town3", "john");;
        System.out.println(h3.toString());              
    } 
}

class House {
    String name;
    String person;
    String town;
    private int propertyCode;
    static int count = 0;

    public House(String name,String town,String person){
        count = count +1;
        this.propertyCode = count;
        this.town = town;
        this.person = person;
        this.name = name;

    }
    public String toString(){
        String temp = "";
        temp = "Code: " + this.name + " " + this.propertyCode;
        return temp;
    }

}
于 2013-09-08T12:38:36.287 回答
1

您的代码不是线程安全的,因此您应该考虑使用 AtomicInteger

private static AtomicInteger houseNo = new AtomicInteger(0);

public House(String s, Town t, Person o){
    owner  = o;
    street = s;
    town   = t;
    propertyCode = houseNo.incrementAndGet();
}

具体来说,正是这两行,通过构造函数运行的多个线程可能会给您带来意想不到的结果

...
houseNo++;
propertyCode = houseNo;
...

当然,AtomicInteger 只会在单个 JVM 中提供唯一的 propertyCode,但也许这就是您所需要的。

于 2013-09-08T12:57:10.493 回答
0

这工作正常,所以再次检查你的代码,特别是你的“getPropCode”

public class House{

    private static int houseNo = 0;
    private int propertyCode;

    public House(){
        houseNo++;
        propertyCode = houseNo;
    }

    public int getPropCode(){
        return propertyCode;
    }

    public String toString(){
        return "code: "+getPropCode();
    }

}

public class Test {

    public static void main(String[] args){

        House a = new House();
        House b = new House();
        House c = new House();

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

    }

}
于 2013-09-08T12:41:20.233 回答