• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
superStar1213
博客园    首页    新随笔    联系   管理    订阅  订阅
三种泛型方式

1.泛型类

class A<T>{使用泛型}     在创建类对象的时候使用

public class MyClass<T> {

    private T t;

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }
    public static void main(String[] args) {
        
        //创建对象
        MyClass<String> mc = new MyClass<String>();
        mc.setT("a");
        System.out.println(mc.getT());
    }

}

 

2.泛型方法

public <T> void method(T a){使用泛型}   在方法调用的时候使用

package com.heima.fanxing;

public class MyClass<T> {

    private T t;

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }
    
    //泛型方法
    public <T> void method(T c){
        System.out.println(c);
    }
    public static void main(String[] args) {
        
        //创建对象
        MyClass<String> mc = new MyClass<String>();
        mc.method(1);
        mc.setT("a");
        System.out.println(mc.getT());
    }

}

 

3.泛型接口

interface B<T>

public interface MyInterface<T> {
    
    public  abstract  void method(T t);

}
public class MyInte implements MyInterface<String>{

    @Override
    public void method(String t) {
        
        
    }

}

 

posted on 2017-12-09 20:42  superStar1213  阅读(337)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3