使用静态变量为对象的每个实例创建唯一代码

时间:2013-09-08 12:28:01

标签: java

每次创建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()));

请原谅我的命名惯例,只是试图搞砸这段代码。

3 个答案:

答案 0 :(得分: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;
    }

}

答案 1 :(得分: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中提供唯一的propertyCodes,但也许这就是你所需要的。

答案 2 :(得分: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);

    }

}