欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

泛型的泛型

程序员文章站 2022-07-15 21:00:50
...
package com.yuxi.demo.generics;

public class MyGerneric <T>
{
    private T foo;

    public T getFoo()
    {
        return foo;
    }

    public void setFoo(T foo)
    {
        this.foo = foo;
    }

}
package com.yuxi.demo.generics;

public class TestGeneric <T,K>
{
	private MyGerneric<K> myGerneric;
	private T testFoo;
	
	public static void main(String[] args)
	{
		TestGeneric<String, MyGerneric<String>> testGeneric = new TestGeneric<String, MyGerneric<String>>();
		MyGerneric<MyGerneric<String>> myGernericOut = new MyGerneric<MyGerneric<String>> ();
		MyGerneric<String> myGernericIn = new MyGerneric<String>();
		myGernericIn.setFoo("I am dangerous");
		myGernericOut.setFoo(myGernericIn);
		testGeneric.setMyGerneric(myGernericOut);
		System.out.println(testGeneric.getMyGerneric().getFoo().getFoo());
		
	}
	public MyGerneric<K> getMyGerneric()
	{
		return myGerneric;
	}

	public void setMyGerneric(MyGerneric<K> myGerneric)
	{
		this.myGerneric = myGerneric;
	}

	public void setTestFoo(T testFoo)
	{
		this.testFoo = testFoo;
	}

	public T getTestFoo()
	{
		return testFoo;
	}

}