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

19 篇文章 0 订阅

Java设计一个学生类Student,其属性有name(姓名)、age(年龄)和degree(学位)。 由类派生出本科生类Undergraduate和研究生类Graduate, Undergraduate类增加属specialty(专业),研究生类增加属性direction(研究方向)。 每个类都有show()函数,用于新出属性信息。注意: (1)所有的输人均为英文,不含中文。(2)编写测试类Test进行测三分别声 明两个类的对象,并调用show()函数。

package pack2;//p93 7 
class Student1{
    private String name;
    private int age;
    private String degree;
    public Student1(String name, int age, String degree) {
        super();//Java的规定:子类继承父类,子类的构造方法必须调用
                //super()即父类的构造方法,而且必须放在构造方法的第一行
        this.name = name;
        this.age = age;
        this.degree = degree;
    }
    public Student1() {
        super();
    }
    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 void show(){
        System.out.println("姓名:" + this.getName() + " 年龄:" + this.getAge() + " 学位:" + this.getDegree() );
    }
}
 
class Undergraduate extends Student1{ //本科类
    private String specialty;//专业
    public String getSpecialty() {
        return specialty;
    }
    public void setSpecialty(String specialty) {
        this.specialty = specialty;
    }
    public Undergraduate(String name, int age, String degree, String specialty) {
        super(name, age, degree);
        this.specialty = specialty;
    }
     
    public void show(){
        System.out.println("姓名:" + this.getName() + "  年龄:" + this.getAge() + "  学位:" + this.getDegree() + "  专业:" + this.getSpecialty());
    }
}
 
class Graduate extends Student1{ //研究生类
    private String direction;
    public String getDirection() {
        return direction;
    }
    public void setDirection(String direction) {
        this.direction = direction;
    }
    public Graduate(String name, int age, String degree, String direction) {
        super(name, age, degree);
        this.direction = direction;
    }

    public void show(){
        System.out.println("姓名:" + this.getName() + "  年龄:" + this.getAge() + "  学位:" + this.getDegree() + "  研究方向:" + this.getDirection());
    }
}

public class student {
    public static void main(String[] args) {
        Undergraduate stu1=new Undergraduate("吴飞",21,"本科","网络工程");
        Graduate stu2=new Graduate("吴优秀",24,"硕士","软件工程");
        stu1.show();
        stu2.show();
    	
    }
}

在这里插入图片描述

  • 26
    点赞
  • 118
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值