Java抽象员工经理实例

程序员3个属性:姓名,年龄,工资
经理:包括上面三个属性,还有奖金属性
继承思想设计类,类中提供必要的方法进行属性访问

public class Test {
	public static void main(String [] args) {
		Coder c = new Coder("张三",18,5000);
		c.work();
	
		Manager m =  new Manager("张三",18,5000,500);
		m.work();
	}

}

abstract class Employee {
	private String name;					//姓名
	private int age;						//年龄
	private double salary;					//奖金
	
	public Employee() {}					//空参构造
	
	public Employee(String name,int age, double salary) {			//有参构造
		this.name = name;
		this.age = age;
		this.salary = salary;
		
	}
	
	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 void setSalary(double salary) {
		this.salary = salary;
		
	}
	public double getSalary() {
		return salary;
		
	}
	
	public abstract void work();        			//抽象方法
}
	
class Coder extends Employee {
	public Coder() {}
	
	public Coder(String name,int age,double salary) {
		super(name,age,salary);								//初始化父类参数
	}
	
	public void work() {
		System.out.println("我的姓名是:" + super.getName() + ",我的年龄是:" + this.getAge() + ",我的工资是:" + this.getSalary());
	}
}

class Manager extends Employee {
	private double bonus;
	
	public Manager() {}
	
	public Manager(String name,int age,double salary,double bonus) {
		super(name,age,salary);				//父类属性用super
		this.bonus = bonus;					//当前属性用this,在此类用this代表当前引用对象赋值
	}
	
	public void work() {
		System.out.println("我的姓名是:" + this.getName() + ",我的年龄是:" + this.getAge() + ",我的工资是:" + this.getSalary() + ",我的奖金是:" + this.bonus);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值