class a<F,G>
{
void make(F x,G y){
x.put1();
y.put1();
}
}
class b
{
public void put1(){
System.out.println("我是B类");
}
}class c
{
public void put1(){
System.out.println("我是C类");
}
}class ex519
{
public static void main(String args[])
{
a<b,c> mm=new a<b,c>();
b xx=new b();
c yy=new c();
mm.make(xx,yy);
}
}
{
void make(F x,G y){
x.put1();
y.put1();
}
}
class b
{
public void put1(){
System.out.println("我是B类");
}
}class c
{
public void put1(){
System.out.println("我是C类");
}
}class ex519
{
public static void main(String args[])
{
a<b,c> mm=new a<b,c>();
b xx=new b();
c yy=new c();
mm.make(xx,yy);
}
}
a<b,c> mm=new a<b,c>(); //这两个定义的类型相同吗?
class a<F,G>
{
void make(F x,G y){
x.put1();
y.put1();
}
}
这个里面指定的f,g泛型,类是不可能知道它指向什么东西,当然也不可能知道f,g里面会有putl();这个方法,这里在这里运用显然是错误的。
class a<F,G>
{
void make(F x,G y){
x.put1();
y.put1();
}
}
该成
class a<F,G>
{
void make(F x,G y){
((b)x).put1();
((c)y).put1();
}
}但是你觉得这样的话,用泛型就无任何意义,直接传b,c要好得多。
至于泛型的意义,你可以参考集合类的实现代码。
{
public void put1();
}class A<F extends SomeInterface, G extends SomeInterface>
{
void make(F x,G y)
{
x.put1();
y.put1();
}
}class B implements SomeInterface
{
public void put1()
{
System.out.println("我是B类");
}
}class C implements SomeInterface
{
public void put1()
{
System.out.println("我是B类");
}
}public class ex519
{public static void main(String[] args)
{
A<B, C> mm=new A<B, C>();
B xx=new B();
C yy=new C();
mm.make(xx,yy);
}}
把公共部分接口化。
void put1();
}class A<F extends Put1, G extends Put1> {
void make(F x, G y) {
x.put1();
y.put1();
}
}class B implements Put1{
public void put1() {
System.out.println("我是B类");
}
}class C implements Put1{
public void put1() {
System.out.println("我是C类");
}
}public class ex519 {
public static void main(String args[]) {
A<B, C> mm = new A<B, C>();
B xx = new B();
C yy = new C();
mm.make(xx, yy);
}
}