继承的练习

1、父类

package com.wyq.study;

public class Employer {
	private String name =  "sdf";
	int age;
	public void setName(String name){
		this.name = name;
	}
	public String getName(){
		return name;
	}
//	public void setAge(int age){
//		this.age = age;
//	}
//	public int getAge(){
//		return age;
//	}
	public Employer(){
		super();
		System.out.println("无参构造"+this.name+"\t"+this.age);
	}
	public Employer(String name,int age){
		super();
		this.name = name;
		this.age = age;
		System.out.println("employer的带参构造"+this.name+"\t"+age);
	}

}

2、子类

package com.wyq.study;

public class Employee extends Employer{
	
	private String department;
	public void setDepartment(String department){
		this.department = department;
	}
	public String getDepartment(){
		return department;
	}
	public Employee(){
		super();
		System.out.println("这里是子类的无参构造");
	}
	public Employee(String name,int age,String department){
		super(name,age);
		this.department = department;
	}
	public void show(){
		System.out.println(super.getName()+"\t"+super.age);
	}
}

3、测试类

package com.wyq.study;

public class TestEmp {
	public static void main(String[] args) {
		Employee e = new Employee("李四",12,"财务部");
		e.show();
		System.out.println(e+"\t"+e.age+"\t"+e.getName()+"\t"+e.getDepartment());
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值