`
jxqc_job
  • 浏览: 529 次
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
java泛型编程 java泛型编程
1. Animal.java
package com.pojo;

public class Animal {
	public String name;
	public Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}


2. Cat.java
package com.pojo;

public class Cat extends Animal{
	private Integer fourFoot;

	public Integer getFourFoot() {
		return fourFoot;
	}

	public void setFourFoot(Integer fourFoot) {
		this.fourFoot = fourFoot;
	}
}

3. Bird.java
package com.pojo;

public class Bird extends Animal{
	private Integer twoFoot;

	public Integer getTwoFoot() {
		return twoFoot;
	}

	public void setTwoFoot(Integer twoFoot) {
		this.twoFoot = twoFoot;
	}
}

4. Demo.java
package com.test;

import java.util.ArrayList;
import java.util.List;

import com.pojo.Animal;
import com.pojo.Bird;
import com.pojo.Cat;
import com.pojo.Person;
import com.pojo.Worker;

public class Demo {
	private List list = new ArrayList();
	/**
	  * 定义泛型方法
	  */
	public <T> String getStr(T s){
		if(s instanceof String){
			return "this is a string : "+s.toString();
		}else{
			return "this is not a string : "+s.toString();
		}
	}
	
	public <T> List<T> getTMap(List<T> ld , List<? extends T> ls){
		for(T t : ls){
			ld.add(t);
		}
		return ld;
	}
	
	public <T> void saveObj(T o){
		list.add(o);
	}
	
	
	public static void main(String[] args) {
		List<String> ls = new ArrayList<String>();
//		List<Object> lo = ls; //error
		
		Demo d = new Demo();
		String s = d.getStr(1);
		System.out.println(s);
		String s2 = d.getStr("hello kity");
		System.out.println(s2);
		
		
		Cat cat_1 = new Cat();
		cat_1.setName("kit猫");
		cat_1.setAge(2);
		cat_1.setFourFoot(4);
		
		Cat cat_2 = new Cat();
		cat_2.setName("cool猫");
		cat_2.setAge(1);
		cat_2.setFourFoot(4);
		
		List<Cat> cat = new ArrayList<Cat>();
		cat.add(cat_1);
		cat.add(cat_2);
		List<Animal> ans = d.getTMap(new ArrayList<Animal>(), cat);
		d.showMessage(ans);
		
		
		Bird b_1 = new Bird();
		b_1.setName("喜鹊");
		b_1.setAge(2);
		b_1.setTwoFoot(2);
		
		Bird b_2 = new Bird();
		b_2.setName("黄莺");
		b_2.setAge(1);
		b_2.setTwoFoot(2);
		
		List<Bird> bs = new ArrayList<Bird>();
		bs.add(b_1);
		bs.add(b_2);
		List<Animal> lb = d.getTMap(new ArrayList<Animal>(), bs);
		d.showMessage(lb);
		
		
		Cat c = new Cat();
		c.setName("coffee猫");
		c.setAge(1);
		c.setFourFoot(4);
		d.saveObj(c);
		
		Bird b = new Bird();
		b.setName("麻雀");
		b.setAge(1);
		b.setTwoFoot(2);
		d.saveObj(b);
		System.out.println("***************************************");
		d.showMessage(d.list);
	}
	
	public <T> void showMessage(List<T> lb){
		for(T t : lb){
			if(t instanceof Animal){
				Integer footNumber = 0;
				Animal a = (Animal)t;
				if(a instanceof Bird)
					footNumber = 2;
				else if(a instanceof Cat)
					footNumber = 4;
				System.out.println(a.getName()+", "+a.getAge()+", "+(footNumber == 0 ? "":footNumber)+"条腿");
			}
		}
	}
}

Global site tag (gtag.js) - Google Analytics