java 泛型接口 泛型方法

浅浅的花香味﹌ 2022-06-15 07:12 463阅读 0赞

1 java 泛型接口:

Center

package Inteface;
interface GneInter{
public void say();
}
class Gin implements GneInter{
private String info;
public Gin(String info)
{
this.info = info;
}
public String getInfo() {
return info;
}

public void setInfo(String info) {
this.info = info;
}

public void say()
{

}
}
public class GrenericalDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub
Gin g = new Gin(“泛型接口执行了”);
System.out.println(g.getInfo());
}

}

案例结果:

泛型接口执行了

2 泛型方法;

Center 1

package Inteface;
class Gener
{
public T tell(T t){
return t;
}
}
public class GenericDemo06 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Gener gener = new Gener();
System.out.println(gener.tell(“Hello”));//泛型方法数据类型根据我们的数据我们所需的数据类型 进行自己设定
System.out.println(gener.tell(10));//泛型方法数据类型根据我们的数据我们所需的数据类型
}

}

案例结果:

Hello

泛型方法:

package Inteface;

public class GenericDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub
String arr[] = {“www”,”Hello”};
tell(arr);
}
public static void tell(T arr[]) {
// TODO Auto-generated method stub
for(int i = 0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}

案例结果:

www
Hello

10

发表评论

表情:
评论列表 (有 0 条评论,463人围观)

还没有评论,来说两句吧...

相关阅读