静态泛型
来源:互联网 发布:app数据接口 编辑:程序博客网 时间:2024/05/16 03:37
public class GenericDemo {
public static void main(String[] args) {
GenericClass<String> gen = new GenericClass<String>();
gen.println("abc");//修改后可以输出任意类型的数据
gen.println(123);
gen.println(true);
gen.println(10.25);
gen.println(new Dog());//重载的调用
gen.println(new Cat());
GenericClass.print("逗比");//静态的泛型方法调用
}
}
class GenericClass<T> {
/*
* public void println(T content) {//这样就直接吧程序写死了,不能再写其他类型的
* System.out.println(content); }
*/
// 泛型方法
@SuppressWarnings("hiding")
public <T> void println(T content) {
System.out.println(content);
}
// 泛型方法的重载
@SuppressWarnings("hiding")
public <T extends Animal> void println(T animal) {
animal.eat();
}
//静态泛型方法
public static <T> void print(T content) {
System.out.println(content);
}
}
//创建一个接口Animal
interface Animal {
public abstract void eat();
}
//创建一个Dog类并实现Animal这个接口的方法
class Dog implements Animal {
public void eat() {
System.out.println("啃骨头");
}
}
//创建一个Cat类并实现Animal这个接口的方法
class Cat implements Animal {
public void eat() {
System.out.println("吃鱼");
}
}
0 0
- 静态泛型
- 静态泛型方法
- Java静态泛型方法
- Java泛型静态方法调用问题
- 集合框架(静态方法泛型)
- java 之 静态泛型方法
- 泛型不能持有静态变量
- java静态方法,静态初始化块及静态变量的声明和初始化不能使用泛型的原因
- 静态
- 静态
- 静态
- 静态
- 静态
- 静态
- 静态
- 静态
- 静态
- JAVA 泛型方法 和 静态方法泛型
- type= image 表单重复提交问题
- UVALive 4670(AC自动机)
- 深入浅出node.js游戏服务器开发1——基础架构与框架介绍
- The Suspects(并查集)
- jar 打包命令详解
- 静态泛型
- MySQL联合查询
- TCP状态转换详解
- 总结一下最近的体会
- openwrt tftp 烧录openwrt-ramips-mt7628-mt7628-squashfs-sysupgrade.bin
- crc 校验
- 泛型的使用方法以及注意事项
- MySQL触发器
- On Demand Resources