哪位编程大神帮我看看这java程序是哪里不对劲呢,那个编译出的错误完全不会改啊

2025-05-19 05:52:51
推荐回答(1个)
回答1:

class Student {
String name;
int age;
String dgree;
public Student(String name, int age, String dgree) {
this.name = name;
this.age = age;
this.dgree = dgree; }
public Student(String name, int age) {
this.name = name;
this.age = age; }
public String getDetails() {
return "姓名:" + name + "\n年龄:" + age + "\nDgree:" + dgree;
}}
class Undergraduate extends Student {
private String specialty;
public Undergraduate(String name, int age, String dgree, String specialty) {
// super(name, age);父类构造函数参数是三个,不能调用;我估计你是写掉了个
super(name, age, dgree);
this.specialty = specialty;

}
public String getDetails() {
return "姓名:" + name + "\n年龄:" + age + "\n学位:本科" + "\n专业:" + specialty;
}
}
class Graduate extends Student {
private String studydirection;
public Graduate(String name, int age, String studydirection) {
super(name, age);// 同样的问题,你要想只要2个参数,可以再写个构造函数;或者直接用super调用父类成员函数;
this.studydirection = studydirection;
}

public String getDetails() {
return "姓名:" + name + "\n年龄:" + age + "\n学位:硕士" + "\n研究方向:" + studydirection;
}
}
public class TestOverriding {
public static void main(String[] args) {
Undergraduate u = new Undergraduate("王军", 23, "本科", "自动化");
Graduate g = new Graduate("刘君", 27, "网络安全技术");
System.out.println(u.getDetails());
System.out.println(g.getDetails());
}
}