继承与构造函数

问题描述:设计一个学生类Student,其属性有:姓名(name)、年龄(age)、学历(education),由Student类派生出本科生类Undergraduate和研究生类Graduate,本科生类增加属性:专业(specialty),研究生类增加属性:研究方向(direction)。每个类都有构造方法和用于输出属性信息的show()方法,在测试类TestExtends中测试输出

package testExtends;
    /**        
     * Title: TestExtends.java    
     * Description:练习类的继承、构造方法的使用,并测试结果
     * @author LiHang      
     * @created 2017年10月14日 下午4:48:39
     * gitAddress:https://github.com/lh75756/homework.git    
     */
public class TestExtends {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student s1=new Student("LiMing",20,"大学");
		s1.show();
		System.out.println("");
		Undergraduate u1=new Undergraduate("WangXing",21,"大学","软件工程");
		u1.show();
		System.out.println();
		Graduate g1=new Graduate("ZhangLing",20,"研究生","软件工程");
		g1.show();
	    }
	}
class Student{
	static private String name;
	static private int age;
	static private String education;
	public Student(String a,int b,String c){
		this.name=a;
		this.age=b;
		this.education=c;
	}
    static public String getName() {
		return name;
	}
	static public int getAge() {
		return age;
	}
	static public String getEducation() {
		return education;
	}
	public void show()
	{
		System.out.println("学生的姓名为:"+name);
		System.out.println("学生的年龄为:"+age);
		System.out.println("学生的学历为:"+education);
	}
}
class Undergraduate extends Student{
	private String specialty;
	public Undergraduate(String a,int b,String c,String d){
		super(a,b,c);
		this.specialty=d;
	}
	public void show() {
		super.show();
		System.out.println("大学生的专业为:"+specialty);
	}
}
class Graduate extends Student{
	private String direction;
	public Graduate(String a,int b,String c,String e){
		super(a,b,c);
		this.direction=e;
	}
	public void show() {
        super.show();
		System.out.println("研究生的研究方向为:"+direction);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值