Java中程序中定义一个学生班和一个研究生班,这是从学生派生的并且是主要班级。 2)学生有两个字段:学生的ID和姓名,这是通过学生的构造函数初始化的。

本文展示了如何在Java中定义一个`Student`类和一个从`Student`派生的`Graduate`类。`Student`类包含学生ID和姓名属性,并重写了`toString()`方法以提供自定义输出。`Graduate`类添加了主管属性,并在构造函数中初始化。在主函数中,我们创建了一个`Graduate`实例并打印了相关信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义一个class Student和一个class Graduate,这是从继承Student的,并且是主类。

2)学生有两个字段:学生的ID和姓名,这是通过学生的构造函数初始化的。
3)覆盖toString方法,让print(一个实例)产生类似“学号:101姓名:David Bissell”的输出。
4)毕业生有一个领域:主管,可通过“毕业生”进行初始化

Define a Student class and a Graduate class which is derived from the Student and is the main class.
2) Student has two fields: student’s Id and name, which are initialized through Student’s constructor.
3) Override toString method, let print(an instance) produce an output like “学号:101 姓名:David Bissell”.
4) Graduate has one field: supervisor, which is initialized through Graduate’

class Student{
	int Id;
	String name;
	public Student(int Id,String name) {
		this.Id=Id;
		this.name=name;
	}
	public String toString() {
		return "name:  "+name+"Id:"+Id;
	}
}
public class Graduate extends Student{
	String supervisor;
     public Graduate(int Id, String name,String supervisor) {
		super(Id, name);
		this.supervisor=supervisor;
	}

public static void main(String[] args) {
		Graduate stu=new Graduate(101,"David Bissell","Jeff Trump");
		System.out.print(stu+" 导师:"+stu.supervisor);
		}

	}



运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值