日期:2014-05-20  浏览次数:20705 次

本人菜鸟。。。。求解【泛型接口】
interface Info //定义一个标识接口,该接口没有任何方法
{
}
class Contact implements Info //实现接口
{
private String address; //地址属性
private String telephone; //电话属性
private String zipcode; //邮政编码属性
public Contact(String address,String telephone,String zipcode) //构造方法
{
this.setAddress(address);
this.setTelephone(telephone);
this.setZipcode(zipcode);
}
public String getAddress() //获取地址属性
{
return address;
}
public void setAddress(String telephone) //设置地址属性
{
this.address=address;
}
public String getTelephone() //获取电话号码属性
{
return this.telephone;
}
public void setTelephone(String telephone) //设置电话号码属性
{
this.telephone=telephone;
}
public String getZipcode() //获取邮政编码
{
return this.zipcode;
}
public void setZipcode(String zipcode) //设置邮政编码属性
{
this.zipcode=zipcode;
}
public String toString() //覆写Object类的ToString()方法
{
  return "联系方式:"+"\n"+
"\t|-联系电话:"+this.telephone+"\n"+
"\t|-联系地址:"+this.address+"\n"+
"\t|-邮政编码:"+this.zipcode;
}
}
class Person <T extends Info> //设置泛型类并设置上限
{
private T info;
public Person(T info)
{
this.info=info;
}
public T getInfo() //获取信息
{
return info;
}
public void setInfo(T info) //设置信息
{
this.info=info;
}
public String toString() //覆写Object类的toString()方法
{
return this.info.toString();
}
}
public class GenericsDemo20
{
Person<Contact> i=null;
Contact a=new Contact("重庆电力高等专科学校","15909306894","52260");
i=new Person<Contact>(a);
i.getInfo().toString();
}
运行之后!!!总是提示红色区域需要标示符。。小弟左思右想。。不解!!!求各位高手指点呀。。

------解决方案--------------------
放在main方法里
Java code

Person<Contact> i=null;
Contact a=new Contact("重庆电力高等专科学校","15909306894","52260");
i=new Person<Contact>(a);
i.getInfo().toString();

------解决方案--------------------
java语法里,执行的语句必须放在方法里。不管是静态的,非静态的,有名字的或没名字的。但是一定不能裸奔
------解决方案--------------------
执行的语句必须放在方法里。不管是静态的,非静态的,有名字的或没名字的
------解决方案--------------------
i.getInfo().toString()
这个也要放在System.out.println()里才有输出