《黑马程序员》 抽象类之模板设计模式

public class ModleModuleDemo2 {

	/**
	 * 抽象类之模板设计模式 练习 例:
	 * 学生和工人都可以说话 只是它们说话的内容不同
	 *  我们需要使用抽象类来设计这个类,
	 * 说话是一个功能,我们可以将说话的具体的 
	 * 内容交给工人和学习去具体的实现
	 */
	public static void main(String[] args) {
		Students students=new Students("李成", 22,99.5f);
		students.say();
		Workers workers=new Workers("李国强", 34, 5000);
		workers.say();
	}

}

abstract class Person2 {
	// 人有年龄,姓名
	private String name;
	private int age;

	public Person2(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void say() {
		// 调用抽象方法
		getContent();
	}

	public abstract void getContent();
}

class Students extends Person2 {
	// 分数
	private float score;

	public Students(String name, int age, float score) {
		super(name, age);
		this.score = score;
	}

	// 因为成员是私有的。我们又是继承的所以只能通过super.get方法来访问父类中的私有成员
	@Override
	public void getContent() {
		System.out.println("姓名:" + super.getName() + "; 年龄:" + super.getAge()
				+ "; 分数:" + score);
	}

}

class Workers extends Person2 {
	private double salary;

	public Workers(String name, int age, double salary) {
		super(name, age);
		this.salary = salary;
	}

	@Override
	public void getContent() {
		System.out.println("姓名:" + super.getName() + "; 年龄:" + super.getAge()
				+ "; 工资:" + salary);
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值