抽象类的应用
假设有以下三类事物,每个事物有自己的操作
- 机器人(Robot):具备充电,工作两个基本操作
- 人类(Human):具备吃饭,工作,睡觉三个基本操作
- 猪(Pig):具备吃饭,睡觉两个基本属性。
现在要求实现对三种事物进行控制,也就是说可以对机器人,人类和猪控制它们自己所属的基本操作,三种操作就是:吃饭,睡觉,工作。
简单说来,相当于有一个按钮,一按“吃”,那么:机器充电,人类吃饭,猪吃饭。
首先明确有三个普通类:Robot,Human,Pig,由于三者的基本操作有共同性,也就是说具备抽象的共有行为,分别是:EAT,SLEEP,WORK.所以设计一个抽象类Action,里面具备三个抽象方法:eat(),sleep(),work().由于要求Action对三类事物控制同一行为,所以把操作定义成全局常量,用具体的数字表示行为,用一个分支结构区别不同操作。
1:抽象类设计
abstract class Action{
//定义三个表示吃睡工作的全局变量,尽量让两者之和不等于第三方,比如:控制:吃+睡
public static final int EAT=1;
public static final int SLEEP=3;
public static final int WORK=5;
//三个用于继承类实现的抽象方法
public abstract void eat();
public abstract void sleep();
public abstract void work();
//命令,分支选择
public void command(int flag) {
switch(flag) {
case EAT:
this.eat();
break;
case SLEEP:
this.sleep();
break;
case WORK:
this.work();
break;
}
}
}
2:三个类的继承
class Robot extends Action{//机器人类覆写抽象方法
public void eat() {
System.out.println("机器人在充电");
}
public void sleep() {
//由于机器人不用睡觉,不写,但必须有方法体。
}
public void work() {
System.out.println("机器人在工作");
}
}
class Human extends Action{
public void eat() {
System.out.println("人在吃饭");
}
public void sleep() {
System.out.println("人在睡觉");
}
public void work() {
System.out.println("人在工作");
}
}
class Pig extends Action{
public void eat() {
System.out.println("猪在吃猪食");
}
public void sleep() {
System.out.println("猪在睡大觉");
}
public void work() {
//只有方法体,猪不工作
}
}
3:主程序实现
public static void main(String[] args) {
fun(new Robot());
fun(new Human());
fun(new Pig());
}
//外部函数,调用各种命令,Action类标准:三个常量
public static void fun(Action act) {
act.command(Action.EAT);
act.command(Action.SLEEP);
act.command(Action.WORK);
}
程序具体实现:
abstract class Action{
//定义三个表示吃睡工作的全局变量,尽量让两者之和不等于第三方,比如:控制:吃+睡
public static final int EAT=1;
public static final int SLEEP=3;
public static final int WORK=5;
//三个用于继承类实现的抽象方法
public abstract void eat();
public abstract void sleep();
public abstract void work();
//命令,分支选择
public void command(int flag) {
switch(flag) {
case EAT:
this.eat();
break;
case SLEEP:
this.sleep();
break;
case WORK:
this.work();
break;
}
}
}
class Robot extends Action{//机器人类覆写抽象方法
public void eat() {
System.out.println("机器人在充电");
}
public void sleep() {
//由于机器人不用睡觉,不写,但必须有方法体。
}
public void work() {
System.out.println("机器人在工作");
}
}
class Human extends Action{
public void eat() {
System.out.println("人在吃饭");
}
public void sleep() {
System.out.println("人在睡觉");
}
public void work() {
System.out.println("人在工作");
}
}
class Pig extends Action{
public void eat() {
System.out.println("猪在吃猪食");
}
public void sleep() {
System.out.println("猪在睡大觉");
}
public void work() {
//只有方法体,猪不工作
}
}
public class transfer {
public static void main(String[] args) {
fun(new Robot());
fun(new Human());
fun(new Pig());
}
//外部函数,调用各种命令,Action类标准:三个常量
public static void fun(Action act) {
act.command(Action.EAT);
act.command(Action.SLEEP);
act.command(Action.WORK);
}
}
执行结果:
总结:
一般开发中使用继承,绝大部分只继承抽象类;抽象类强制了子类必须重载的抽象方法,而且子类可以自己定义函数,所以采用:抽象类 对象=new 子类()去实现调用。最后由于抽象类的单继承局限,开发中20%用到,80%会采用接口。
解释:子类丰富抽象类方法
abstract class Action{
public abstract void eat();
}
class Pig extends Action{
public void eat() {
System.out.println("猪在吃猪食");
}
public void sleep() {
System.out.println("猪在睡大觉");
}
public void work() {
//只有方法体,猪不工作
}
}
public class transfer {
public static void main(String[] args) {
fun(new Pig());
}
//外部函数,调用各种命令,Action类标准:三个常量
public static void fun(Action act) {
act.eat();
if(act instanceof Pig) {
Pig b=(Pig) act;
b.work();
}
}
}