设计一个学生类Student,其数据成员有name(姓名)、age(年龄)和degree(学位)。由Student类派生出本科生类Undergraduate和研究生类Graduate,本科生类Unde

题目信息:

  1. 设计一个学生类Student,其数据成员有name(姓名)、age(年龄)和degree(学位)。由Student类派生出本科生类Undergraduate和研究生类Graduate,本科生类Undergraduate增加成员specialty(专业),研究生类增加成员direction(研究方向)。每个类都有show()方法,用于输出数据成员信息。最后请输出下列信息:
    在这里插入图片描述

参考答案:

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 ;
    }

}

猜你喜欢

转载自blog.csdn.net/guankunkunwd/article/details/121675778
今日推荐