📄 state2.txt
字号:
// mz.cpp : Defines the entry point for the console application.
//
class Context;
class State{
void handlepush(Context c);
void handlepull(Context c);
void getcolor();
};
class BlueState:public State{
public:
void handlepush(Context c){
//根据push 方法"如果是blue 状态的切换到green" ;
c.setState(new GreenState());
}
void handlepull(Context c){
//根据pull 方法"如果是blue 状态的切换到red" ;
c.setState(new RedState());
}
void getcolor(){ return (Color.blue);}
};
class Context{
private:
State state; //我们将原来的 Color state 改成了新建的State
//setState 是用来改变state 的状态 使用setState 实现状态的切换
pulic:
void setState(State state){
this.state=state;
}
void push(){
//状态的切换的细节部分,在本例中是颜色的变化,已经封装在子类的handlepush 中实现,这里无需关心
state.handlepush(this);
//因为sample 要使用state 中的一个切换结果,使用getColor()
Sample sample=new Sample(state.getColor());
};
public void pull(){
state.handlepull(this);
Sample2 sample2=new Sample2(state.getColor());
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -