范型

作者: 我该忘了我自己w_u | 来源:发表于2017-01-13 16:12 被阅读15次

范型就是不指定类型,用的时候在指定类型
<T,K>
使用场景

public static class Point<T> {   
 private T x;   
 private T y;  
 public T getX() {  
      return x; 
   } 
 public T getY() {  
      return y;    
   }    
 public void setX(T x) {   
      this.x = x;
   }  
 public void setY(T y) {    
      this.y = y;
   }
}
Point<String>p = new Point<String>();
p.setX("jingdu :109");
p.setY("weidu: 110");
System.out.println("x="+ p.getX()+" y="+p.getY());

用在构造函数中

class Con<T>{  
  public T value;  
  public Con (T value){    
    this.value = value;  
  }  
  public T getValue(){     
   return value;  
  }   
  public void setValue(T value){   
     this.value = value;    
  }
}

Con con = new Con<String>("构造方法中使用范性") ;
System.out.println(con.getValue());


class  Gen<K,T>{
    private  T take;
    private  K key;
    public T getTake() {
        return take;
    }
    public void setTake(T take) {
        this.take = take;
    }
    public K getKey() {
        return key;
    }
    public void setKey(K key) {
        this.key = key;
    }
}

Gen<String,Integer>g = new Gen<String, Integer>();
g.setTake(10);
g.setKey("jjjjjj");
System.out.println(g.getTake()+g.getKey());



class Info<T>{
    private T key;
    public T getKey() {
        return key;
    }
    public void setKey(T key) {
        this.key = key;
    }
    @Override
    public String toString() {
        return this.getKey().toString();
    }
}

public static void tell(Info<?> i){
    System.out.println(i);
}
Info<String>i = new Info<String>();
i.setKey("jjjjj");
tell(i);


interface GenInter<T>{
    public void say();
}
class Gin implements GenInter<String>{
    private String info;
    public Gin(String info) {
        this.info = info;
    }
    public String getInfo() {
        return info;
    }
    public void setInfo(String info) {
        this.info = info;
    }
    @Override
    public void say() {
    }

Gin g = new Gin("jkdjdkja");
System.out.println(g.getInfo());

class Gener{
    public <T>T tell(T t){
        return t;
    }
}
Gener g = new Gener();
String str = g.tell("kjlj;");
System.out.println(g.tell(str));
int i = g.tell(1);
System.out.println(g.tell(i));



public static <T>void tell(T arr[]){
    for (int i = 0; i <arr.length; i++){
        System.out.println(arr[i]);
    }
}
String arr[]={"23","23432","ddf"};
tell(arr);

相关文章

  • CoreJava笔记 - 范型程序设计(2)

    范型代码和Java虚拟机 关键知识:类型擦除Java的范型是编译器层次的范型,而在Java虚拟机上并没有范型类。在...

  • 范型

    <:上界 >:下界 <%视图定界 一定要传隐式转换函数 T隐式转换成order[T][T<%M]关系意味着...

  • 范型

    范型就是不指定类型,用的时候在指定类型使用场景 用在构造函数中

  • 范型

    泛型是jdk1.5使用的新特性。 泛型的好处:1. 将运行时的异常提前至了编译时。2. 避免了无谓的强制类型转换 ...

  • 范型

  • Objective-C 范型

    系统库范型 Objective C支持轻量级的范型。在编写自定义的范型类之前,我们先来看看Cocoa Touch的...

  • CoreJava笔记 - 范型程序设计(5)

    反射与范型 由于类型擦除,反射无法得到关于范型类型参数的信息。 范型的Class类在Java的反射库中,Class...

  • Flutter 类方法和基础语法(II) - 范型

    范型限制

  • Generic 范型 - type parameter

    范型简介 从JDK 5.0开始,范型作为一种新的扩展被引入到了java语言中。 有了范型,我们可以对类型(type...

  • Rust范型(1) - 范型基础

    简介 C/C++、Rust都属于强类型语言,在定义变量或者是传参时,必须明确指定数据的数据类型。明确指定类型对于程...

网友评论

      本文标题:范型

      本文链接:https://www.haomeiwen.com/subject/fjssbttx.html