java 泛型例子_java中的泛型的一些常见例子

/**

* @author Rollen-Holt 使用泛型

*/

class hello {

hello(){

}

public T getName(){

return name;

}

public void setName(T name){

this.name = name;

}

public V getAge(){

return age;

}

public void setAge(V age){

this.age = age;

}

private T name;

private V age;

public static void main(String[] args){

hello he = new hello();

he.setAge(10);

he.setName("Rollen Holt");

System.out.println(he.getName() + " " + he.getAge());

}

}

/**

* @author Rollen-Holt 泛型类的构造方法定义

*/

class hello {

hello(T name, V age){

this.age = age;

this.name = name;

}

public T getName(){

return name;

}

public V getAge(){

return age;

}

private T name;

private V age;

public static void main(String[] args){

hello he=new hello("Rollen",12);

System.out.println(he.getName()+" "+he.getAge());

}

}

/**

* @author Rollen-Holt 使用通配符

*/

class info {

info(T name){

this.name = name;

}

private T name;

}

class hello{

public static void function(info> temp){

System.out.println("内容: "+temp);

}

public static void main(String[] args){

info demo=new info("Rollen");

function(demo);

}

}

/**

* @author Rollen-Holt 泛型上限

*/

class info{

info(T age){

this.age=age;

}

private T age;

}

class hello{

public static void function(info extends Number> temp){

System.out.println("内容"+ temp);

}

public static void main(String[] args){

info demo=new info(1);

function(demo);

}

}

/**

* @author Rollen-Holt 泛型下限

*/

class info{

info(T age){

this.age=age;

}

private T age;

}

class hello{

public static void function(info super String> temp){

System.out.println("内容"+ temp);

}

public static void main(String[] args){

// 此处只能使用String 或者Object

info demo=new info("Rollen");

function(demo);

}

}

/**

* @author Rollen-Holt 泛型和子类继承的限制

*/

class info{

}

class hello{

public static void main(String[] args){

info demo1=new info();

info demo2=new info();

//demo2=demo1; 此处错误

}

}

/**

* 上面的例子说明,一个类的子类可以通过多态性被其父类实例化

* 但是在泛型操作中,子类的泛型类型是无法被其父类的泛型类型实例化的。

*/

如果允许上面的条语句的话,会出现:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

Type mismatch: cannot convert from info to info

at hello.main(hello.java:12)

泛型接口的两种实现:

/**

* @author Rollen-Holt 泛型接口的实现1

*/

interface info {

public void say();

}

// 直接在子类之后声明泛型

class hello implements info{

public static void main(String[] args){

info demo = new hello();

demo.say();

}

public void say(){

System.out.println("hello");

}

}

/**

* @author Rollen-Holt 泛型接口的实现2

*/

interface info {

public void say();

}

// 在子类实现的接口中明确给出泛型类型

class hello implements info{

public static void main(String[] args){

info demo = new hello();

demo.say();

}

public void say(){

System.out.println("hello");

}

}

/**

* @author Rollen-Holt 使用泛型通一传入参数的类型

*/

class info {

private T var;

public T getVar(){

return var;

}

public void setVar(T var){

this.var = var;

}

public String toString(){

return this.var.toString();

}

}

class hello{

public static void main(String[] args){

info demo1=new info();

info demo2=new info();

demo1.setVar("Rollen");

demo2.setVar("Holt");

printAdd(demo1, demo2);

}

// 此处传递的都是同一个String类型的

public static void printAdd(info demo1, info demo2){

System.out.println(demo1.getVar()+""+demo2.getVar());

}

}

否则的话如下所示:出现错误:

/**

* @author Rollen-Holt 使用泛型通一传入参数的类型

*/

class info {

private T var;

public T getVar(){

return var;

}

public void setVar(T var){

this.var = var;

}

public String toString(){

return this.var.toString();

}

}

class hello{

public static void main(String[] args){

info demo1=new info();

info demo2=new info();

demo1.setVar("Rollen");

demo2.setVar(30);

//此处调用错误

printAdd(demo1, demo2);

}

// 此处传递的都是同一个String类型的

public static void printAdd(info demo1, info demo2){

System.out.println(demo1.getVar()+""+demo2.getVar());

}

}

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

The method printAdd(info, info) in the type hello is not applicable for the arguments (info, info)

at hello.main(hello.java:26)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java泛型是一种强类型机制,它可以让你在编译时检查类型错误,从而提高代码的安全性和可读性。在使用泛型时,我们经常会遇到父类和子类的泛型转换问题。 首先,我们需要明确一点:子类泛型不能转换成父类泛型。这是因为Java泛型是不协变的。例如,如果有一个类A和它的子类B,那么List<A>和List<B>之间是不存在继承关系的。 下面我们来看一个例子: ```java public class Animal { //... } public class Dog extends Animal { //... } public class Test { public static void main(String[] args) { List<Animal> list1 = new ArrayList<>(); List<Dog> list2 = new ArrayList<>(); list1 = list2; // 编译错误 } } ``` 在这个例子,我们定义了Animal类和它的子类Dog。然后我们定义了两个List,分别是List<Animal>和List<Dog>。如果将List<Dog>赋值给List<Animal>,会出现编译错误。这是因为List<Animal>和List<Dog>之间不存在继承关系。 那么,如果我们想要让子类泛型转换成父类泛型,应该怎么办呢?这时我们可以使用通配符来解决问题。通配符可以表示任意类型,包括父类和子类。例如,我们可以将List<Dog>赋值给List<? extends Animal>,这样就可以实现子类泛型转换成父类泛型了。 下面我们来看一个使用通配符的例子: ```java public class Animal { //... } public class Dog extends Animal { //... } public class Test { public static void main(String[] args) { List<Animal> list1 = new ArrayList<>(); List<Dog> list2 = new ArrayList<>(); list1 = list2; // 编译错误 List<? extends Animal> list3 = new ArrayList<>(); list3 = list2; // 正确 } } ``` 在这个例子,我们定义了List<? extends Animal>来表示任意继承自Animal的类型。然后我们将List<Dog>赋值给List<? extends Animal>,这样就可以实现子类泛型转换成父类泛型了。 总结一下,Java泛型是不协变的,子类泛型不能转换成父类泛型。如果需要实现子类泛型转换成父类泛型,可以使用通配符来解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值