PTA 6-2 根据派生类写出基类(Java)

这篇博客探讨了如何根据给定的派生类来补充基类的代码,确保Java裁判测试程序能够正常运行。内容涉及函数接口定义和测试程序样例,强调在真实测试中数据可能变化,但应遵循样例中的方法调用格式。

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

6-2 根据派生类写出基类(Java)

裁判测试程序样例中展示的是一段定义基类People、派生类Student以及测试两个类的相关Java代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。

函数接口定义:

提示:
观察派生类代码和main方法中的测试代码,补全缺失的代码。

裁判测试程序样例:
注意:真正的测试程序中使用的数据可能与样例测试程序中不同,但仅按照样例中的格式调用相关方法(函数)。

class People{
	protected String id;
	protected String name;
	
	/** 你提交的代码将被嵌在这里(替换此行) **/
	
}

class Student extends People{
	protected String sid;
	protected int score;
	public Student() {
		name = "Pintia Student";
	}
	public Student(String id, String name, String sid, int score) {
		super(id, name);
		this.sid = sid;
		this.score = score;
	}
	public void say() {
		System.out.println("I'm a student. My name is " + this.name + ".");
	}

}
public class Main {
	public static void main(String[] args) {
		Student zs = new Student();
		zs.setId("370211X");
		zs.setName("Zhang San");
		zs.say();
		System.out.println(zs.getId() + " , " + zs.getName());
		
		Student ls = new Student("330106","Li Si","20183001007",98);
		ls.say();
		System.out.println(ls.getId() + " : " + ls.getName());
		
		People ww = new Student();
		ww.setName("Wang Wu");
		ww.say();
		
		People zl = new People("370202", "Zhao Liu");
		zl.say();
	}
}

输入样例:
在这里给出一组输入。例如:

(无)

输出样例:
在这里给出相应的输出。例如:

I'm a student. My name is Zhang San.
370211X , Zhang San
I'm a student. My name is Li Si.
330106 : Li Si
I'm a student. My name is Wang Wu.
I'm a person! My name is Zhao Liu.
//无参
	public People() {
		
	}
	//有参
	public People(String id,String name) {
		this.id = id;
		this.name = name;
	}
	//setter & getter
	public void setId(String id) {
		this.id = id;
	}
	
	public String getId() {
		return this.id;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return this.name ;
	}
	
	public void say() {
		System.out.println("I'm a person! My name is " + this.name +".");
	}
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值