本人菜鸟。。。。求解【泛型接口】

youareperfect1 2012-06-02 07:08:32
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();
}
运行之后!!!总是提示红色区域需要标示符。。小弟左思右想。。不解!!!求各位高手指点呀。。
...全文
124 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sai123q 2012-06-03
  • 打赏
  • 举报
回复
i.getInfo().toString()
这个也要放在System.out.println()里才有输出
wyx100 2012-06-02
  • 打赏
  • 举报
回复
执行的语句必须放在方法里。不管是静态的,非静态的,有名字的或没名字的
RDroid 2012-06-02
  • 打赏
  • 举报
回复
java语法里,执行的语句必须放在方法里。不管是静态的,非静态的,有名字的或没名字的。但是一定不能裸奔
Clown_悠哈 2012-06-02
  • 打赏
  • 举报
回复
晕aaaaaaaaa
youareperfect1 2012-06-02
  • 打赏
  • 举报
回复
啊、、、一语中的。。谢谢了。。
古布 2012-06-02
  • 打赏
  • 举报
回复
放在main方法里

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

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧