泛型类
package com.generic_class;
public class GenericDemo<T> {
private T val;
public void setVal(T val){
this.val = val;
}
public T getVal() { /*返回值为泛型形参T的成员方法*/
return val;
}
public static void main(String[] args) {
GenericDemo<String> demo = new GenericDemo<String>();
demo.setVal("123");
System.out.println(demo.getVal());
}
}
泛型接口及其实现类
package com.generic_interface;
public interface Addable<T> {
public void add();
}
package com.generic_interface;
public interface Getable<T> {
public T get(int i);
}
package com.generic_interface;
public class ArrayUtil<T> implements Addable<T>{
@Override
public void add() {
System.out.println("泛型接口的实现类");
}
}
package com.generic_interface;
public class ArrayUtil2<T> implements Addable<T>,Getable<T> {
@Override
public void add() {
System.out.println("多实现泛型接口add方法");
}
@Override
public T get(int i) {
System.out.println("多实现泛型接口get方法");
return null;
}
}
package com.generic_interface;
public class ArrayUtil3<T,S> implements Addable<T>,Getable<S>{
@Override
public S get(int i) {
System.out.println("多实现多泛型接口get方法");
return null;
}
@Override
public void add() {
System.out.println("多实现泛型接口add方法");
}
}
package com.generic_interface;
public class TestGenericInterface123 {
public static void main(String[] args) {
ArrayUtil<String> a1 = new ArrayUtil<String>();
ArrayUtil2<Integer> a2 = new ArrayUtil2<Integer>();
ArrayUtil3<Integer,String> a3 = new ArrayUtil3<Integer,String>();
ArrayUtil3 a3_2 = new ArrayUtil3();
System.out.println("泛型接口实现测试:实现类对象已经正常创建。");
}
}
泛型方法
package com.generic_method;
class Demo{
public <T> T get(T arg){
System.out.println("泛型方法");
return arg;
}
}
public class GenericMethodTest {
public static void main(String[] args) {
Demo demo = new Demo();
String getString = demo.get("泛型方法实参String");
Integer integer = demo.get(123);
System.out.println(getString);
System.out.println(integer);
}
}
泛型实验–礼品盒
package com.generic_wildcard;
public class Gift {
protected String name = "礼物名称";
public Gift(){
System.out.println("礼物!");
}
}
package com.generic_wildcard;
public class BirthdayGift extends Gift{
public BirthdayGift(){
this.name = "生日礼物!";
System.out.println("生日礼物!");
}
}
package com.generic_wildcard;
public class LoverGift extends Gift {
public LoverGift(){
this.name = "情人礼物!";
System.out.println("情人礼物!");
}
}
package com.generic_wildcard;
public class Box <T extends Gift>{
private T gift;
public Box(T gift){
this.gift = gift;
System.out.println("礼物盒");
}
}
package com.generic_wildcard;
public class BoxUtil {
public void open(Box<?> x){
System.out.println("打开"+ x +"盒");
}
public static void main(String[] args) {
System.out.println("父类构造方法输出:“礼物!”");
Gift gift = new Gift();
System.out.println();
System.out.println("父类构造方法输出:“礼物!”且子类构造方法输出:“生日礼物!”");
BirthdayGift bg = new BirthdayGift();
System.out.println();
System.out.println("父类礼物:");
Box<Gift> box = new Box<Gift>(gift);
System.out.println("生日礼物:");
Box<BirthdayGift> box2 = new Box<BirthdayGift>(bg);
BoxUtil bop = new BoxUtil();
System.out.println();
bop.open(box);
bop.open(box2);
}
}
输出结果:
父类构造方法输出:“礼物!”
礼物!
父类构造方法输出:“礼物!”且子类构造方法输出:“生日礼物!”
礼物!
生日礼物!
父类礼物:
礼物盒。
生日礼物:
礼物盒。
打开com.generic_wildcard.Box@1b6d3586盒
打开com.generic_wildcard.Box@4554617c盒