Topic information:
- Design a student class Student, whose data members are name (name), age (age) and degree (degree). The Undergraduate class and the graduate class Graduate are derived from the Student class. The undergraduate class Undergraduate adds a member specialty (professional), and the graduate class adds a member direction (research direction). Each class has a show() method for outputting data member information. Finally, please output the following information:
Reference answer:
public class Student_1202 {
String name;
int age;
String degree;
public String show () {
return
"姓名:" + name + '\'' +
", 年龄:" + age +
", 学位:" + degree + '\'' +
'}';
}
public Student_1202 (String name, int age, String degree) {
this.name = name;
this.age = age;
this.degree = degree;
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
public int getAge () {
return age;
}
public void setAge (int age) {
this.age = age;
}
public String getDegree () {
return degree;
}
public void setDegree (String degree) {
this.degree = degree;
}
public static void main (String[] args) {
Undergraduate zhangsan=new Undergraduate ("张三", 20,"本科","通信");
Undergraduate lisi=new Undergraduate ("李四",21,"本科","电子");
diection wangwu=new diection ("王五",25,"硕士","通信");
diection liuliu=new diection ("刘六",36,"博士","通信");
System.out.println (zhangsan.show ());
System.out.println (lisi.show ());
System.out.println (wangwu.show ());
System.out.println (liuliu.show ());
}
}
class Undergraduate extends Student_1202{
String sepcialty;
public Undergraduate (String name, int age, String degree, String sepcialty) {
super (name, age, degree);
this.sepcialty = sepcialty;
}
@Override
public String show () {
return
"姓名:" + this.name +" "+
" 年龄:" + this.age + " "+
" 学位:" + this.degree + " " +"专业:"+this.sepcialty ;
}
}
class diection extends Student_1202{
String sepcialty;
public diection(String name, int age, String degree, String sepcialty) {
super (name, age, degree);
this.sepcialty = sepcialty;
}
@Override
public String show () {
return
"姓名:" + this.name +" "+
" 年龄:" + this.age + " "+
" 学位:" + this.degree + " " +"研究方向:"+this.sepcialty ;
}
}