2013-05-20 131 views
0
static LifeInsurance[] LIArray = new LifeInsurance[20]; 

public LifeInsurance (float dMonth, int startD,int startM,int startY,int label){ 

    LifeInsurance.LIArray[LifeInsurance.counterLI] = this; 

    this.dMonth = dMonth; 
    this.startD = startD; 
    this.startM = startM; 
    this.startY = startY; 
    this.label = Individual.l; 
    this.codeLΙ = counterLI; 




    counterLI++; 

} 

我有了这个阵列中的寿险类,我想有一个从其他类访问this.label = Individual.l;访问阵列

这怎么可能?提前致谢!

+1

任何只要使用这个类的构造函数其他类并使用xxx.getLabel(); – Makky

回答

1

为静态变量创建一个geter和setter。获取该类的实例以获取静态变量(本例中为数组)并从数组中获取所需的对象。然后使用getter作为标签。

0

要通过类来访问它,你应该做的:

lInsurance = new LifeInsurance(args......); 
lInsurance.label; //if label is visible from the class you're calling, otherwise:lI 
lInsurance.getLabel(); //you'll need to define this method. 
0

创建的getter:

public String getLabel(){ 
    return this.label; 
} 

而且拨打:

lInsurance = new LifeInsurance(args......); 
lInsurance.getLabel(); 
0

有一个微妙的安全问题与您的代码。在完全构建this之前,您将this添加到包变量LIArray变量中。因此,另一个线程可以查看半建的this,这可以(很少)导致各种问题。

那么,CounterLI是什么?您可能应该使用ListMap作为所有人寿保险单的列表,而不是阵列。

0

leo21是对的。看来你想你的保险之一稍后访问,因此这将是:

一种对保险阵列吸气:

public static LifeInsurance[] getInsurances() { 
    return LIArray; 
} 

另外一个getter方法所需的属性:

public String getLabel() { 
    return this.label; 
} 

String a_label = LifeInsurance.getInsurances()[an_index].getLabel(); 

注:

然后你就可以从静态的任何地方访问它需要getter该数组必须是静态的。

这是一个相当长的时间,我都没有用Java编码,所以语法可能不正确(编辑我如果需要的话),但是这是想法...