zoukankan      html  css  js  c++  java
  • 泛型

    泛型 构造方法中使用

    class Gener<T>{
    	private T value;
    
    	public T getValue() {
    		return value;
    	}
    
    	public void setValue(T value) {
    		this.value = value;
    	}
    	public String toString(){
    		return this.getValue().toString();
    	}
    }
    
    public class Demo01 {
    
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Gener<String> g = new Gener<String>();
    		g.setValue("Hello World");
    		say(g);
    	}
    	public static void say(Gener<?> g){
    		System.out.println(g.toString());
    	}
    
    }
    

      泛型 通配符的使用:

    使用泛型时,如果事先不知道是什么类型,可以使用?通配符

    class Gener<T>{
    	private T value;
    
    	public T getValue() {
    		return value;
    	}
    
    	public void setValue(T value) {
    		this.value = value;
    	}
    	public String toString(){
    		return this.getValue().toString();
    	}
    }
    
    public class Demo01 {
    
    	
    	public static void main(String[] args) {
    		
    		Gener<String> g = new Gener<String>();
    		g.setValue("Hello World");
    		say(g);
    	}
    	public static void say(Gener<?> g){
    		System.out.println(g.toString());
    	}
    
    }
    

      泛型接口

    interface Gener1<T> {
    	public void say();
    }
    
    class Gar<T> implements Gener1<T> {
    	private String value;
    
    	public Gar(String value) {
    		this.value = value;
    	}
    
    	public String getValue() {
    		return value;
    	}
    
    	public void setValue(String value) {
    		this.value = value;
    	}
    
    	@Override
    	public void say() {
    
    	}
    
    }
    
    public class Demo02 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Gar<String> g = new Gar<String>("Hello World");
    		System.out.println(g.getValue());
    	}
    
    }
    

      泛型方法:

    泛型方法中可以定义泛型参数,此时,参数的类型就是传入数据类型

    class Genera {
    	public <T> T say(T t) {
    		return t;
    	}
    }
    
    public class Demo03 {
    
    	public static void main(String[] args) {
    		Genera g = new Genera();
    		String result = g.say("Hello World");
    		int result1 = g.say(10);
    		System.out.println(result);
    		System.out.println(result1);
    	}
    
    }
    

      泛型数组

    public class Demo04 {
    
    	public static void main(String[] args) {
    		Integer arr[] = { 1, 2, 3, 4 };
    		d(arr);
    		String a[] = { "Hello", "World", "Gracy" };
    		d(a);
    	}
    
    	public static <T> void d(T arr[]) {
    		for (int i = 0; i < arr.length; i++) {
    			System.out.println(arr[i]);
    		}
    	}
    
    }
    

      

  • 相关阅读:
    [LeetCode]2. Add Two Numbers链表相加
    Integration between Dynamics 365 and Dynamics 365 Finance and Operation
    向视图列添加自定义图标和提示信息 -- PowerApps / Dynamics365
    Update the Power Apps portals solution
    Migrate portal configuration
    Use variable to setup related components visible
    Loyalty management on Retail of Dynamic 365
    Modern Fluent UI controls in Power Apps
    Change screen size and orientation of a canvas app in Power App
    Communication Plan for Power Platform
  • 原文地址:https://www.cnblogs.com/gracyandjohn/p/4602916.html
Copyright © 2011-2022 走看看