public class SaveBiz {
byte[] message_type = new byte[ConstantSize.MESSAGE_TYPE_SIZE];
byte[] processing_code = new byte[ConstantSize.PROCESSING_CODE_SIZE];
byte[] trans_no = new byte[ConstantSize.TRANS_NO_SIZE];
public byte[] getMessage_type() {
return message_type;
}
public void setMessage_type(byte[] message_type) {
this.message_type = message_type;
}
public byte[] getProcessing_code() {
return processing_code;
}
public void setProcessing_code(byte[] processing_code) {
this.processing_code = processing_code;
}
public byte[] getTrans_no() {
return trans_no;
}
public void setTrans_no(byte[] trans_no) {
this.trans_no = trans_no;
}
}
public class SubSaveBiz extends SaveBiz{
byte [][]sortedBiz= new byte[][]{
super.message_type,
super.processing_code,
super.trans_no,
};
public byte[][] getSortedBiz() {
return sortedBiz;
}
}
如上code中,在父类SaveBiz 通过set方法来设置值,影响不到子类的sortedBiz数组里面的值...谁能解释一下??
如何才能让其同步??
你的子类这么写:public class SubSaveBiz extends SaveBiz{
public byte[][] getSortedBiz() {
return new byte[][]{
super.message_type,
super.processing_code,
super.trans_no,
};
}
}
public class Test5 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[] test = {1,2,3,4};
SaveBiz s = new SaveBiz();
s.setProcessing_code(test);
byte[] test2 =s.getProcessing_code();
for(int i=0;i<test2.length;i++){
System.out.println("the array in super class is:"+test2[i]);
}
SubSaveBiz sb = new SubSaveBiz();
byte[][] subtest = sb.getSortedBiz();
for(int i=0;i<subtest[1].length;i++){
System.out.println(subtest[1][i]);
}
}}class SaveBiz {
static byte[] message_type = new byte[3];
static byte[] processing_code = new byte[4];
static byte[] trans_no = new byte[3];
public byte[] getMessage_type() {
return message_type;
}
public void setMessage_type(byte[] message_type) {
this.message_type = message_type;
}
public byte[] getProcessing_code() {
return processing_code;
}
public void setProcessing_code(byte[] processing_code) {
this.processing_code = processing_code;
}
public byte[] getTrans_no() {
return trans_no;
}
public void setTrans_no(byte[] trans_no) {
this.trans_no = trans_no;
}
}class SubSaveBiz extends SaveBiz{
byte [][]sortedBiz= new byte[][]{
super.message_type,
super.processing_code,
super.trans_no,
};
public byte[][] getSortedBiz() {
return sortedBiz;
}
}
打印结果
the array in super class is:1
the array in super class is:2
the array in super class is:3
the array in super class is:4
1
2
3
4
super.processing_code,
super.trans_no,
每一行前的这些super没有用,可以去掉,最好用geter代替。个人认为在子类中设sortedBiz这个属性不太合理,因为里面的数据是由其它属性组成的。只设一方法把要组合的属性组合在一起返回就行了。
举个简单的例子来说明,下面的代码和lz代码的构造是一样的,只不过简化了public class Test6 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub SuperClass sc = new SuperClass();
sc.setInt(5);
SubClass ssc = new SubClass();
int test1 = ssc.getSuperInt();
int test2 = ssc.getSubInt();
System.out.println("super int1 is:"+test1);
System.out.println("super int2 is:"+test2);
}
}class SuperClass{
int superint = 1;
void setInt(int s){
superint = s;
}
int getInt(){
return superint;
}
}class SubClass extends SuperClass{
int subint = super.superint;
int getSuperInt(){
return super.superint;
}
int getSubInt(){
return subint;
}
}
在这个例子中,
int superint = 1;
这个superint不是静态的,也就是说这个变量不是属于类,而是属于对象的
所以,执行
SuperClass sc = new SuperClass();
sc.setInt(5);
之后,只能说sc这个对象的superint属性变成了5
而不能说父类SuperClass的superint变成了5
所以,不管你子类如何实例化,得到的父类的superint变量始终是初始化后的1可以看到
System.out.println("super int1 is:"+test1);
System.out.println("super int2 is:"+test2);
的结果是
super int1 is:1
super int2 is:1
所以,如果lz非要让他们同步
那末我们就不能让那个属性属于对象,而要让它属于类
由此,加上static关键字
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub SuperClass sc = new SuperClass();
sc.setInt(5);
SubClass ssc = new SubClass();
int test1 = ssc.getSuperInt();
int test2 = ssc.getSubInt();
System.out.println("super int1 is:"+test1);
System.out.println("super int2 is:"+test2);
}
}class SuperClass{
static int superint = 1; //加上static关键字
void setInt(int s){
superint = s;
}
int getInt(){
return superint;
}
}class SubClass extends SuperClass{
int subint = super.superint;
int getSuperInt(){
return super.superint;
}
int getSubInt(){
return subint;
}
}
打印结果
super int1 is:5
super int2 is:5
this.processing_code = processing_code;
}
这样的set方法可能和你的要求不适合,从代码上看,processing_code应该是有固定长度的,但这样的set方法可以把任意长度的字节数组替换掉你原来的processing_code = new byte[ConstantSize.PROCESSING_CODE_SIZE];