设计一个学生类Student和它的一个子类Undergraduate

5、设计一个学生类Student和它的一个子类Undergraduate
1)Student类有name(姓名)、和age(年龄)属性,一个包含两个参数的构造方法,用于给name和age属性赋值,一个show()方法打印Student的属性信息
2)本科生类Undergraduate增加一个degree(学位)属性。有一个包含三个参数的构造方法,前两个参数用于给继承的name和age属性赋值,第三个参数给degree专业赋值,一个show()方法用于打印Undergraduate的属性信息
3)在测试类中分别创建Student对象和Undergraduate对象,调用它们的show()。

class Student {
	public String name;
	public int age;
	public Student(String name,int age){
		this.name=name;
		this.age=age;
	}
	public void show(){
		System.out.println("name: "+name+" age: "+age);
	}
}
class UnderGraduate extends Student{
	public String degree;
	public UnderGraduate(String name,int age,String degree){
		super(name, age);
		this.degree=degree;
	}
	public void show(){
		System.out.println("name: "+name+" age: "+age+" degree: "+degree);
	}
}
public class Test01{
	public static void main(String[] args) {
		Student student = new Student("zhangsan", 16);
		student.show();
		UnderGraduate underGraduate = new UnderGraduate("lisi", 20, "bechalor");
		underGraduate.show();
	}
}


你可能感兴趣的:(《Java基础入门》课后题)