日期:2014-05-20 浏览次数:20719 次
public class A { private String abc; private int a; private float ab; public String getAbc() { return abc; } public void setAbc(String abc) { this.abc = abc; } public int getA() { return a; } public void setA(int a) { this.a = a; } public float getAb() { return ab; } public void setAb(float ab) { this.ab = ab; } }
private String abc; private int a; private float ab; public String getAbc() { return abc; } public void setAbc(String abc) { this.abc = abc; } public int getA() { return a; } public void setA(int a) { this.a = a; } public float getAb() { return ab; } public void setAb(float ab) { this.ab = ab; }
public class C { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub List<A> alist = new ArrayList<A>(); List<B> blist = new ArrayList<B>(); A a1 = new A(); a1.setA(1); a1.setAb(1.1f); a1.setAbc("111"); A a2 = new A(); a2.setA(2); a2.setAb(1.2f); a2.setAbc("222"); A a3 = new A(); a3.setA(3); a3.setAb(1.3f); a3.setAbc("333"); alist.add(a1); alist.add(a2); alist.add(a3); for(A a:alist){ // 此处是否可以优化,或者其他方法。其中A.java 和B.java属性方法都一样的,就类名不一样。 B b = new B(); b.setA(a.getA()); b.setAb(a.getAb()); b.setAbc(a.getAbc()); blist.add(b); } for(B b:blist){ System.out.println(b.getA()); System.out.println(b.getAb()); System.out.println(b.getAbc()); } } }