[Java] 纯文本查看 复制代码 public class Test2 {
public static void main(String[] args) {
People a = new American("Jonny");
People b = new Chinese("李小龙");
People c = new Chinese("成龙");
People[] p = new People[]{a,b,c};
for (People people : p) {
System.out.println(p);
}
}
}
class People{
private String name;
public People(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public String speak(){
return "说话";
}
@Override
public String toString() {
return "不知道哪里人";
}
}
class Chinese extends People{
public Chinese(String name) {
super(name);
}
@Override
public String speak() {
return "我说中国话";
}
@Override
public String toString() {
return "我叫"+getName()+"我来自中国";
}
}
class American extends People{
public American(String name) {
super(name);
}
@Override
public String speak() {
return "I'm speak English";
}
@Override
public String toString() {
return "My name is "+getName()+",I'm from American";
}
}
如上所示,输出结果是:
[Lcom.exam.zsq.People;@15db9742
[Lcom.exam.zsq.People;@15db9742
[Lcom.exam.zsq.People;@15db9742
请问我重写的toString方法为什么不生效?
|