JAva汎用操作

1903 ワード

public class FanXintTest<T> {

    private T frunt;
    
    public FanXintTest(T frunt){
        this.frunt=frunt;
    }
    
    public T getFrunt(){
        return frunt;
    }
    
    public static void main(String[] args) {
        Frunt frunt=new Frunt();
        frunt.name="  ";
        frunt.weight="1kg";
        FanXintTest<Frunt> fxt=new FanXintTest<Frunt>(frunt);
        Frunt frunt2=fxt.getFrunt();
        System.out.println(frunt2.name);
        Apple apple=new Apple();
        apple.name="  ";
        apple.color="red";
        FanXintTest<Apple> afxt=new FanXintTest<Apple>(apple);
        System.out.println(afxt.getFrunt().name);
      }
}
class Frunt{
    public String name;
    public String weight;
}
class Apple extends Frunt{
    public String color;
}

二:メタクラスライブラリ
package com.flyjaky.fanxing;
public class FanXingTest2 {
    public static void main(String[] args) {
        ChuiZi ChuiZi=new ChuiZi();
        Jutou jutou=new Jutou();
        GongJuBao<ChuiZi,Jutou> gongJuBao=new GongJuBao<ChuiZi,Jutou>(ChuiZi, jutou);
         System.out.println("            ?");
         System.out.println(gongJuBao.jutou.name);
         System.out.println(gongJuBao.chuizi.name);
         
         System.out.println("
?");          gongJuBao.jutou.juju();          gongJuBao.chuizi.bangbang();     } } class GongJuBao<A,B>{     public final A chuizi;     public final B jutou;     public GongJuBao(A chuizi,B jutou){         this.chuizi=chuizi;         this.jutou=jutou;     } } class ChuiZi{     String name=" ";     public void bangbang(){         System.out.println(" !");     } } class Jutou{     String name=" ";     public void juju(){         System.out.println(" !");     } }