类之间的赋值问题

ww_goddess 2006-12-12 03:01:50
我的源程序如下
public class plasmid{

plasmid1 a=new plasmid1();

static String PlasmidName;
static int[] j;
static int[] startAngle;
static int[] arcAngle;
static String[] name;

}



class plasmid1 extends plasmid{

static String PlasmidName="PCDNA3.1";
static int[] j=new int[]{1,1,1,3,2,2,3,1};//
static int[] startAngle=new int[]{70,35,-20,-72,240,180,82,100};//
static int[] arcAngle=new int[]{10,25,40,35,30,30,9,35};//
static String[] name=new String[]
{"f1 ori","sv40ori","Neomycin","sv40 pA","pUC","Ampicillin","BGH pA","Pcmv"};

}
class plasmid2 extends plasmid{

static String PlasmidName="PA8081";
static int[] j=new int[]{1,2,1,1,2,2,3,3};//
static int[] startAngle=new int[]{70,35,-20,-72,240,180,82,100};//
static int[] arcAngle=new int[]{10,25,40,35,30,30,9,35};//
static String[] name=new String[]
{"f1 ori","sv40ori","Neomycin","sv40 pA","pUC","Ampicillin","BGH pA","Pcmv"};

}
class plasmid3 extends plasmid{

}还有plasmid4,plasmid5等,大约80个,每个子类只是变量的值不同。现在我想做的是:通过写一个方法来把子类的变量的值一一赋给父类的同名变量。这个方法的参数是子类的

static String PlasmidName的值。就是根据static String PlasmidName变量的值来确定该把哪个子类的变量的值一一赋给父类的同名变量?我想了好久没有结果,大家有好的方

法帮我写写吗?谢谢
...全文
306 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ww_goddess 2006-12-13
  • 打赏
  • 举报
回复
问题解决,感谢所有参与讨论的网友,感谢fool_leave()
修改后代码如下
import java.lang.reflect.Field;

class p{
public static void putStaticValueToFather(String son)throws Exception{
Class sonClass=Class.forName(son);
Class fatherClass=sonClass.getSuperclass();

Field fs1=sonClass.getDeclaredField("PlasmidName");
Field sonField1=fs1;
String fieldName1=fs1.getName();
Field fatherField1=fatherClass.getField(fieldName1);
Object value1=sonField1.get(sonClass);
fatherField1.set(fatherClass,value1);
plasmid.PlasmidName=(String)value1;

Field fs2=sonClass.getDeclaredField("j");
Field sonField2=fs2;
String fieldName2=fs2.getName();
Field fatherField2=fatherClass.getField(fieldName2);
Object value2=sonField2.get(sonClass);
fatherField2.set(fatherClass,value2);
plasmid.j=(int[])value2;

Field fs3=sonClass.getDeclaredField("startAngle");
Field sonField3=fs3;
String fieldName3=fs3.getName();
Field fatherField3=fatherClass.getField(fieldName3);
Object value3=sonField3.get(sonClass);
fatherField3.set(fatherClass,value3);
plasmid.startAngle=(int[])value3;

Field fs4=sonClass.getDeclaredField("arcAngle");
Field sonField4=fs4;
String fieldName4=fs4.getName();
Field fatherField4=fatherClass.getField(fieldName4);
Object value4=sonField4.get(sonClass);
fatherField4.set(fatherClass,value4);
plasmid.arcAngle=(int[])value4;

Field fs5=sonClass.getDeclaredField("name");
Field sonField5=fs5;
String fieldName5=fs5.getName();
Field fatherField5=fatherClass.getField(fieldName5);
Object value5=sonField5.get(sonClass);
fatherField5.set(fatherClass,value5);
plasmid.name=(String[])value5;
}



public static void main(String[] args) throws Exception{
}
}

public class plasmid{
public static String PlasmidName;
public static int[] j;//
public static int[] startAngle;//
public static int[] arcAngle;//
public static String[] name;

}

class plasmid1 extends plasmid{

public static String PlasmidName="PCDNA3.1";
public static int[] j=new int[]{1,1,1,3,2,2,3,1};//
public static int[] startAngle=new int[]{70,35,-20,-72,240,180,82,100};//
public static int[] arcAngle=new int[]{10,25,40,35,30,30,9,35};//
public static String[] name=new String[]
{"f1 ori","sv40ori","Neomycin","sv40 pA","pUC","Ampicillin","BGH pA","Pcmv"};

}

class plasmid2 extends plasmid{

public static String PlasmidName="PA8081";
public static int[] j=new int[]{2,1,1,1,2,2,3,3};//
public static int[] startAngle=new int[]{70,35,-20,-72,240,180,82,100};//
public static int[] arcAngle=new int[]{10,25,40,35,30,30,9,35};//
public static String[] name=new String[]
{"f1 ori","sv40ori","Neomycin","sv40 pA","pUC","Ampicillin","BGH pA","Pcmv"};

}
fool_leave 2006-12-12
  • 打赏
  • 举报
回复
木用过.net
看过.net的代码,觉得就是个新java,现在一说更像了
凋零的老树 2006-12-12
  • 打赏
  • 举报
回复
java有这么好的reflect机制,其他语言想用都没有,你们怎么不知道用用呢


.net也有 ^.^
88324877 2006-12-12
  • 打赏
  • 举报
回复
fool_leave() ( ) 信誉:92 Blog 2006-12-12 16:04:54 得分: 0


88324877(寂寞呆头鱼)的方法没办法通用呀

java有这么好的reflect机制,其他语言想用都没有,你们怎么不知道用用呢


---------------------------------------
恩我的是按具体需求定制的,
reflect机制,老实说我是不知道啊。看来还要多多学习-_-!
sxnucseven 2006-12-12
  • 打赏
  • 举报
回复
我只是认为这么实现好像很别扭,你不觉得吗? 更像是面向过程编成。
fool_leave 2006-12-12
  • 打赏
  • 举报
回复
88324877(寂寞呆头鱼)的方法没办法通用呀

java有这么好的reflect机制,其他语言想用都没有,你们怎么不知道用用呢
sxnucseven 2006-12-12
  • 打赏
  • 举报
回复
有什么需求限制非搞这么多子类啊?
不能用一个类的N个对象解决吗?
在这个类里重载clone(),在该方法里根据PlasmidName值来进行复制。
88324877 2006-12-12
  • 打赏
  • 举报
回复
public class A {
public A(String PlasmidName,int[] j,int[] startAngle,int[] arcAngle,String[] name){
this.PlasmidName=PlasmidName;
this.j=j;
this.startAngle=startAngle;
this.arcAngle=arcAngle;
this.name=name;
}

public static A makeClassA(String PlasmidName,int[] j,int[] startAngle,int[] arcAngle,String[] name){
A pp=new A(PlasmidName,j,startAngle,arcAngle,name);
return pp;
}
public static void main(String[] args){
String PlasmidName="start";
int[] j={1,2};
int[] startAngle={3,4};
int[] arcAngle={5,6};
String[] name={"end","."};
A number1=A.makeClassA(PlasmidName,j,startAngle,arcAngle,name);
number1.print();



String PlasmidName1="end";
int[] j1={1,2};
int[] startAngle1={3,4};
int[] arcAngle1={5,6};
String[] name1={"end","."};
A number2=A.makeClassA(PlasmidName1,j1,startAngle1,arcAngle1,name1);
number2.print();

}
public void print(){
System.out.println(PlasmidName);
}
static String PlasmidName;
static int[] j;
static int[] startAngle;
static int[] arcAngle;
static String[] name;
}

你看看行不 ,思想是利用方法工厂。
yi10000 2006-12-12
  • 打赏
  • 举报
回复
用java克隆的方法就解决你的问题了
fool_leave 2006-12-12
  • 打赏
  • 举报
回复
发现你全是static属性
写了一个将子类static属性转换到父类的方法。其实你可以照猫画虎,写个更通用的

用到了reflect,你可以去看看这个包


public static void putStaticValueToFather(String son)throws Exception{
Class sonClass=Class.forName(son);
Class fatherClass=sonClass.getSuperclass();
Field[] fs=sonClass.getDeclaredFields();
for(int i=0;i<fs.length;i++){
Field sonField=fs[i];
String fieldName=fs[i].getName();
Field fatherField=fatherClass.getField(fieldName);
if(fatherField!=null){
Object value=sonField.get(sonClass);
fatherField.set(fatherClass,value);
System.out.println(fieldName+"->\t"+value);
}else{
System.err.println("Field not found in father class."+fieldName);
}
}
}

public static void main(String[] args) throws Exception{
System.out.println(B.str);
putStaticValueToFather("A");
System.out.println(B.str);
}
}
class B{
public static String str="B";
}
class A extends B{
public static String str="A";
}
wunan320 2006-12-12
  • 打赏
  • 举报
回复
factory

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧