6-5 请完成父类 (10 分)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44547670/article/details/102756571

6-5 请完成父类 (10 分)

裁判测试程序样例中展示的是一段定义基类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 = "CUIT 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("4700X");
		zs.setName("Zhang San");
		zs.say();
		System.out.println(zs.getId() + " , " + zs.getName());
		
		Student ls = new Student("330106","Li Si","2018000007",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.
4700X , 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.

解答

People() {
	}

	People(String id, String name) {
		this.id = id;
		this.name = name;
	}

	
	public void setId(String id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void say() {
		// TODO Auto-generated method stub
		System.out.println("I'm a person. My name is " + this.name + ".");
	}

	public String getId() {
		return this.id;
	}
	public String getName() {
		return this.name;
	}

定义多个构造方法,根据子类构造方法参数个数和种类的不同继承不同的父类构造方法。

猜你喜欢

转载自blog.csdn.net/weixin_44547670/article/details/102756571