第一种情况:在同一个public类中可以使用“.”的方式获取对象实例;
public static class pp{
private int nu;
private int pin;
private int second;
}
static pp[] bt; //全局定义
bt[i]=new pp();
bt[i].nu=p;
注:如果建立数组对象,一定要对数组中的对象再进行一次实例,否则在使用时会出现NullPointerException空指针的现象;
第二种情况:通过set\get的方法引用对象,由于类的成员变量都是私有的,在项目开发中更为严谨,方便其他类中的调用使用set\get方法更为合适;
public static class pp{
private int nu;
private int pin;
private int second;
public int getNu() {
return nu;
}
public void setNu(int nu) {
this.nu = nu;
}
public int getPin() {
return pin;
}
public void setPin(int pin) {
this.pin = pin;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
}
static pp[] bt; //全局定义
bt[i]=new pp();
bt[0].setNu=p;
result=bt[0].getNu;
第三种情况:使用构造方法进行引用,这里又分为无参构造与有参构造;
默认情况为无参构造,int类型默认为0,string类型默认为null;
有参可以将java的多态特性体现出来;
private static class pp{
private int nu;
private int pin;
private int second;
public pp(int nu){
this.nu=nu;
}
public pp(int nu,int pin){
this.nu=nu;
this.pin=pin;
}
public pp(int nu,int pin,int second){
this.nu=nu;
this.pin=pin;
this.second=second;
}
}
static pp[] bt;
bt[0]=new pp(12);