Java之泛型编程

1.概念
   泛型就是参数化类型。泛型的好处是在编译的时候检查类型安全,并且所有的 强制转换都是自动和隐式的,提高代码的重用率。
2.案例
1)先看下面案例:
//不适用泛型编程  
Apple app0=new Apple();  
Apple app1=new Apple();  
   
List li = new ArrayList();  
li.add(app0);        //添加非需要类型时发现不了错误。  
li.add(app1);  
   
Apple appUsed=(Apple)li.get(0);  
   
//使用泛型编程如下  
Apple app0=new Apple();  
Apple app1=new Apple();  
   
List<Apple> li = new ArrayList<Apple>();  
li.add(app0);        //如果添加的对象类型错误,编译器即可发现。指定容器要持有的对象类型,用编译器来保证类型的正确性。  
li.add(app1);  
   
Apple appUsed=li.get(0);  
 
 
  使用泛型的优点:大型应用时能显著降低程序的复杂度;泛型为较大的优化带来可能: 可以在编译期发现该类错误,而且在取出元素时不需要再进行类型判断,从而提高了程序的运行时效率。 
2)泛型类
有两个类如下,要构造两个类的对象,并打印出各自的成员x。
public class StringFoo {  
  
    private String x;  
  
    public StringFoo(String x) {  
             this.x = x;  
    }  
  
  
    public String getX() {  
             return x;  
    }  
  
     public void setX(String x) {  
             this.x = x;  
    }  
 }  
  
 public class DoubleFoo {  
  
    private Double x;  
  
    public DoubleFoo(Double x) {  
             this.x = x;  
    }  
  
    public Double getX() {  
             return x;  
    }  
  
     public void setX(Double x) {  
             this.x = x;  
    }  
 }  
 
 
用泛型来实现
public class GenericsFoo<T> {  
  
  private T x;  
  
  public GenericsFoo(T x) {  
     this.x = x;  
  }  
  
   public T getX() {  
     return x;  
  }  
  
  public void setX(T x) {  
     this.x = x;  
  }  
}  
  
   
代码实现:  
  
 public class GenericsFooDemo {  
  
  public static void main(String args[]){  
  
     GenericsFoo<String> strFoo=new GenericsFoo<String>("Hello Generics!");  
     GenericsFoo<Double> douFoo=new GenericsFoo<Double>(new Double("33"));  
     GenericsFoo<Object> objFoo=new GenericsFoo<Object>(new Object());  
  
     System.out.println("strFoo.getX="+strFoo.getX());  
     System.out.println("douFoo.getX="+douFoo.getX());  
     System.out.println("objFoo.getX="+objFoo.getX());  
  }  
}  
 
3)泛型方法
是否拥有泛型方法,与其所在的类是否泛型没有关系。要定义泛型方法,只需将泛型参数列表置于返回值前。
public class ExampleA {  
  
  public <T> void f(T x) {  
     System.out.println(x.getClass().getName());  
  }  
  
  public static void main(String[] args) {  
  
     ExampleA ea = new ExampleA();  
  
     ea.f("ea ");  
     ea.f(10);  
     ea.f('a');  
  }  
}  
 
 
  使用泛型方法时,不必指明参数类型,编译器会自己找出具体的类型。泛型方法除了定义不同,调用就像普通方法一样。需要注意,一个static方法,无法访问泛型类的类型参数,所以,若要static方法需要使用泛型能力,必须使其成为泛型方法。www.2cto.com
 
4)限制泛型和通配符
class GenericsFoo<T extends Collection>,这样类中的泛型T只能是Collection接口的实现类,传入非Collection接口编译会出错。

class GenericsFoo<? extends Collection>,“?”代表未知类型(通配符),这个类型是实现Collection接口。<? extends 类型>表示这个类型是某个类型的子类型。


CSDN又抽风了,文章来自红黑联盟。。。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python是一种动态类型语言,不像Java或C++那样有严格的类型约束。因此,Python中的泛型编程可以理解为通用函数或类,可以接受不同类型的参数。 在Python中,泛型编程通常通过函数注解来实现。通过在函数参数或返回值上添加注解,可以指定参数和返回值的类型。这样做不仅可以提高代码可读性,也可以让IDE等工具提供更好的代码提示和类型检查。 以下是一个简单的例子,实现了一个通用的加法函数: ```python def add(a: 'T', b: 'T') -> 'T': return a + b ``` 其中,参数a和b的类型注解为'T',表示可以接受任何类型的参数。返回值的类型也注解为'T',表示返回值类型与参数类型相同。 使用时,可以传入任意类型的参数: ```python print(add(1, 2)) # 3 print(add('hello', ' world')) # 'hello world' print(add([1, 2, 3], [4, 5, 6])) # [1, 2, 3, 4, 5, 6] ``` 另外,Python中也可以通过继承泛型类来实现泛型编程。例如,使用typing模块中的List和Dict泛型类可以定义接受列表和字典类型参数的函数: ```python from typing import List, Dict def process_list(lst: List[int]) -> Dict[int, int]: result = {} for i in lst: if i not in result: result[i] = 1 else: result[i] += 1 return result ``` 以上是一个简单的例子,定义了一个接受整数列表并返回整数计数字典的函数。在定义参数和返回值时,使用了List和Dict泛型类来指定参数和返回值的类型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值