泛型的使用

1.FanXingClass

public class FanXingClass<A> {
 
    private String name;
 
    private A a;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public A getA() {
        return a;
    }
 
    public void setA(A a) {
        this.a = a;
    }
 
    public void show(){
        System.out.println("name: " + name + " A:" + a);
    }
 
    public void set(A a){
        System.out.println("这是set方法中的A" + a);
    }
 
    public A get(){
        return a;
    }

}

 -------------------------------------------------------------------------------------------------------------------

2.FanXingDemo

import java.util.ArrayList;
import java.util.List;
 
/**
 * 当做一些集合的统一操作的时候,需要保证集合的类型是统一的,此时需要泛型来进行限制
 *      优点:
 *          1、数据安全
 *          2、获取数据时效率比较高
 *      给集合中的元素设置相同的类型就是泛型的基本需求
 *       使用:
 *          在定义对象的时候,通过<>中设置合理的类型来进行实现
 *  泛型的高阶应用:
 *      1、泛型类
 *          在定义类的时候在类名的后面添加<E,K,V,A,B>,起到占位的作用,类中的方法的返回值类型和属性的类型都可以使用
 *      2、泛型接口
 *          在定义接口的时候,在接口的名称后添加<E,K,V,A,B>,
 *          1、子类在进行实现的时候,可以不填写泛型的类型,此时在创建具体的子类对象的时候才决定使用什么类型
 *          2、子类在实现泛型接口的时候,只在实现父类的接口的时候指定父类的泛型类型即可,此时,测试方法中的泛型类型必须要跟子类保持一致
 *      3、泛型方法
 *          在定义方法的时候,指定方法的返回值和参数是自定义的占位符,可以是类名中的T,也可以是自定义的Q,只不过在使用Q的时候需要使用<
 *          Q>定义在返回值的前面
 *      4、泛型的上限(工作中不用)
 *          如果父类确定了,所有的子类都可以直接使用
 *      5、泛型的下限(工作中不用)
 *          如果子类确定了,子类的所有父类都可以直接传递参数使用
 *
 *
 */
public class FanXingDemo {
 
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("1");  //new Integer(1)
        list.add("123");  // new String("123")
        list.add("true");  //new Boolean(true)
//
//        //遍历集合
//        for (int i = 0; i < list.size(); i++) {
//            System.out.println(list.get(i));
//        }
//
//        System.out.println("***************************************");
//        for(String o : list){
            String str = (String)o;
//            System.out.println(o);
//        }
 
//        FanXingClass<String> fanXingClass = new FanXingClass<String>();
//        fanXingClass.setNum(1);
//        fanXingClass.setA("123");
//        fanXingClass.show();
//
//        FanXingClass<Integer> fanXingClass2 = new FanXingClass<Integer>();
//        fanXingClass2.setNum(2);
//        fanXingClass2.setA(1234);
//        fanXingClass2.show();
//
//        FanXingClass<Person> fanXingClass3 = new FanXingClass<Person>();
//        fanXingClass3.setNum(3);
//        fanXingClass3.setA(new Person("小白",18));
//        fanXingClass3.show();
//
//        FanXingClass<String> fanXingClass4 = new FanXingClass<String>();
//        fanXingClass4.setNum(4);
//        fanXingClass4.set("666");
//        fanXingClass4.setA("8888");
//        System.out.println(fanXingClass4.get());
//        fanXingClass4.show();
 
//        FanXingInterface fanXingInterface = new FanXingInterfaceImpl();
//        fanXingInterface.test2("666");
//        System.out.println(fanXingInterface.test());
 
        FanXingMethod<String> fanXingMethod = new FanXingMethod<String>();
        fanXingMethod.setT("666");
        fanXingMethod.show(123);
        fanXingMethod.show(true);
    }

}

---------------------------------------------------------------------------------------------------------------------------------

3.FanXingInterface

public interface FanXingInterface<B> {

public abstract void test(B b);

public abstract B test2();

}

---------------------------------------------------------------------------------------------------------------------------------

4.FanXingMethod

public class FanXingMethod<T> {
 
    private int id;
 
    private T t;
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public T getT() {
        return t;
    }
 
    public void setT(T t) {
        this.t = t;
    }
 
    public <Q> void show(Q q){
        System.out.println(t);
        System.out.println(q);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
泛型(Generics)是一种在编程语言中实现参数化类型的技术,可以让我们编写更加灵活和通用的代码。下面是一个泛型使用案例: 假设我们有一个需求,需要实现一个通用的栈(Stack)数据结构,可以存储任何类型的元素。我们可以使用泛型来实现这个通用的栈数据结构。以下是一个基于Java的示例代码: ```java public class Stack<T> { private ArrayList<T> items; public Stack() { items = new ArrayList<T>(); } public void push(T item) { items.add(item); } public T pop() { if (items.isEmpty()) { throw new RuntimeException("Stack is empty"); } return items.remove(items.size() - 1); } public boolean isEmpty() { return items.isEmpty(); } } ``` 在上面的代码中,我们使用了一个类型参数 `T`,它代表任何类型。我们在类的定义中使用了 `<T>` 来声明这个类是一个泛型类,它可以接受任何类型的元素。在类的内部,我们使用 `T` 来代表元素的类型。我们将元素存储在一个 `ArrayList<T>` 中,这个 `ArrayList` 可以存储任何类型的元素。 我们定义了三个方法:`push()`、`pop()` 和 `isEmpty()`。`push()` 方法用于将元素压入栈中,`pop()` 方法用于弹出栈顶元素,并从栈中移除它,`isEmpty()` 方法用于判断栈是否为空。 使用泛型,我们可以使用这个通用的栈数据结构来存储任何类型的元素,例如: ```java Stack<Integer> intStack = new Stack<Integer>(); intStack.push(1); intStack.push(2); intStack.push(3); intStack.pop(); // 返回 3 intStack.pop(); // 返回 2 Stack<String> strStack = new Stack<String>(); strStack.push("Hello"); strStack.push("World"); strStack.pop(); // 返回 "World" ``` 在上面的示例代码中,我们分别使用了 `Stack<Integer>` 和 `Stack<String>` 来存储整数和字符串类型的元素。由于使用泛型,这个通用的栈数据结构可以存储任何类型的元素。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Destiny-^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值