Package Penalty for com.aaa.test; / * * generic role: A: enhance the security of the program (in the collection of generic use) B: Transfer will run encounter to compile C: omitted strong turn trouble type D: to improve the reusability of code, the code that implements a common package * • generic method - the method in the generic definition of - format: method name return type (generic type public <generic type> .) * * / Import of java.util.ArrayList; Import java.util.List; public class Demo11 <E> { Private int ID; Private String name; / ** * @return The ID * / public int getId () { return the above mentioned id; } /** * @param id the id to set */ public void setId(int id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Demo11 [id=" + id + ", name=" + name + "]"; } public static void main(String[] args) { Demo11<Object> demo11 = new Demo11<>(); //使用泛型 添加数据快捷 List<Object>list=new ArrayList<>(); list.add(4654); list.add("哈哈"); System.out.println(list); } }
Second, the generic definition of the interface
Package com.aaa.test; / * * generic defined in the interface - is defined in the generic interface - Format: public interface interface name <... generic types. 1> public interface FanXingDingYiJieKou <E> { int the Add (); } * * generic implementation of the interface * * / public class Demo10 <E> the implements FanXingDingYiJieKou <E> { @Override public int the Add (E E) { // the TODO Auto-Generated Method Stub return 0 ; } public static void main (String [ ] args) { Demo10 <Object> = Dnew Demo10<>(); // 前父后子 FanXingDingYiJieKou<Object> f = new Demo10<>(); d.add(1); f.add(1); } }