接口与实验:实验3
接口:
package mypackage;
public interface DogState {
public abstract void showstate();
}
接口实现:
package mypackage;
public class SoftlyState implements DogState {
@Override
public void showstate() {
// TODO 自动生成的方法存根
System.out.println("听主人的命令");
}
}
package mypackage;
public class MeetEnemyState implements DogState {
@Override
public void showstate() {
// TODO 自动生成的方法存根
System.out.println("狂叫,并冲向曲狠咬敌人");
}
}
package mypackage;
public class MeetFriendState implements DogState {
@Override
public void showstate() {
// TODO 自动生成的方法存根
System.out.println("摇动尾巴,表示欢迎");
}
}
package mypackage;
public class MeetAnotherDog implements DogState {
@Override
public void showstate() {
// TODO 自动生成的方法存根
System.out.println("嬉戏");
}
}
类:
package mypackage;
public class Dog{
public void show(DogState dog) {
dog.showstate();
}
}
主类:
package main_class;
import mypackage.*;
public class Main {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Dog k=new Dog();
k.show(new SoftlyState());
k.show(new MeetEnemyState());
k.show(new MeetFriendState());
k.show(new MeetAnotherDog());
}
}
posted on 2017-11-24 16:15 MACHINE_001 阅读(125) 评论(0) 编辑 收藏 举报