try763738799
lauqasim
2016-01-20 11:46
采纳率: 0%
浏览 1.7k

java程序在eclipse运行下出错,但是在cmd可以正常运行,求大神!

源代码:
class Person{
private String name;
private 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;
}

}

class Student extends Person {
private String school;
Student(String name,
String school) {
this(name,"beijing", school);
}
Student(String n,String l,String school) {
super(n,l);
this.school = school;
}
public String info() {
return super.info()+
" school: "+school;
}
}

class Teacher extends Person{
private String capital;

Teacher(String name,String capital){
    this(name,"beijing",capital);
}

Teacher(String n,String l,String capital){
    super(n,l);
    this.capital = capital;
}

public String info(){
    return super.info()+"location"+capital;
}

}

public class TestTeacher {
public static void main(String[] args) {
Person p1 = new Person("A");
Person p2 = new Person("B","shanghai");
Student s1 = new Student("C","S1");
Student s2 = new Student("C","shanghai","S2");
System.out.println(p1.info());
System.out.println(p2.info());
System.out.println(s1.info());
System.out.println(s2.info());

    Teacher t1 = new Teacher("D","Professor");
    System.out.println(t1.info());
}

}

错误:
Exception in thread "main" java.lang.NoSuchMethodError: Person.(Ljava/lang/String;)Vat TestTeacher.main(TestTeacher.java:55)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

8条回答 默认 最新

  • caozhy

    person构造函数前面加上public

    点赞 评论
  • 91program
    91program 2016-01-20 12:14

    代码上确实没有看到明显的问题,在 Eclpise 调试时出错就单执行一个,看看出错的哪一行各个变量的值是否是你想要的吧。

    点赞 评论
  • wojiushiwo945you
    毕小宝 2016-01-20 12:26

    不是吧,我测试了你的代码,Eclipse和cmd都没有问题啊。我的环境是jdk1.8+Eclipse Mars Release (4.5.0)。
    你的前面三个类是一个Person类文件中,后面的TestTeacher类是一个文件。你是这样的吗?

    点赞 评论
  • try763738799
    lauqasim 2016-01-20 14:37

    我把所有的Person都改成Perso就运行出来了。

    点赞 评论
  • rui888
    兔子托尼啊 2016-01-21 01:20

    删除掉。重新试试。排除eclipse 自身的原因。

    点赞 评论
  • Royal_lr
    Royal_lr 2016-01-21 01:55

    这个十有八九是你的视图的问题

    点赞 评论
  • try763738799
    lauqasim 2016-01-21 02:10

    应该是我之前有一个Person.class,造成混淆啦,谢谢大家的解答。

    点赞 评论
  • qq_26946497
    谁用了我的英文名 2016-01-21 13:02

    的确问题不在代码,是文件名、包路径之类的问题

    点赞 评论

相关推荐