fhhandcc 2015-10-24 08:42 采纳率: 100%
浏览 1619
已采纳

菜鸟求助子类toString()重写

class Person {
String name;
String location;

Person(String name) {
    this.name = name;
    location = "beijing";
}

Person(String name,String location) {
    this.name = name;
    this.location = location;   
}

public String info() {
    return 
    "name:"+name+
    "location:"+location;
}

public String toString() {
    return "I'm a human!";
}

public boolean equals(Object obj) {
    if(obj == null) return false;
    else{
        if(obj instanceof Person) {
            Person p = (Person)obj;
            if(p.name == this.name && p.location == this.location) {
                return true;
            }
        }
    }

    return false;
}

}

class Student extends Person {
String school;

Student(String name,String school) {
    this(name,school,"Xi'an");
}

Student(String n,String s,String l) {
    super(n,l);
    this.school = s;
}

public String info() {
    return super.info()+"school:"+school;
}   

public String toString {
    return "I'm a studet.";
}
public boolean equals(Object obj) {
    if(obj == null) return false;
    else{
        if(obj instanceof Student){
            Student s = (Student)obj;
            if(s.name == this.name&&s.location == this.location&&s.school == this.school) {
                return true;
            }
        }
    }
    return false;   
}

}

public class TestTSandEQ {
public static void main(String[] arg) {
Person p1 = new Person("A","beijing");
Person p2 = new Person("B","beijing");
Student s1 = new Student("A","XD","beijing");
Student s2 = new Student("B","XD");

    System.out.println(p1.info());
    System.out.println(p2.info());
    System.out.println(s1.info());
    System.out.println(s2.info());
    System.out.println("p1:"+p1.toString());
    System.out.println("s1:"+s1.toString());
}

}
Student类中toString()一直提示少一个;图片说明 目前用ultraedit

  • 写回答

2条回答 默认 最新

  • 「已注销」 2015-10-24 08:59
    关注

    public String toString {
        return "I'm a studet.";
    }
    
    

    变为

    public String toString() {
        return "I'm a studet.";
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥250 雷电模拟器内存穿透、寻基址和特征码的教学
  • ¥200 比特币ord程序wallet_constructor.rs文件支持一次性铸造1000个代币,并将它们分配到40个UTXO上(每个UTXO上分配25个代币),并设置找零地址
  • ¥15 关于Java的学习问题
  • ¥15 如何使用chatgpt完成文本分类任务?
  • ¥15 已知速度v关于位置s的等式,怎么转化为已知位置求速度v的等式
  • ¥15 我有个餐饮系统,用wampserver把环境配置好了,但是后端的网页却进去,是为什么,能不能帮远程一下?
  • ¥15 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"
  • ¥20 基于决策树的数字信号处理,2ask 2psk 2fsk的代码,检查下报错的原因
  • ¥15 wincc已组态的变量过多
  • ¥60 如图:直线与椭圆X轴平行,求直线与椭圆任意一点的相切坐标计算公式