admin 管理员组

文章数量: 887021


2023年12月19日发(作者:此电脑管理快捷键)

public class Computer {

private CPU cpu;

private Memory memory;

private Disk disk;

public Computer(){

cpu = new CPU();

memory = new Memory();

disk = new Disk();

}

public void startup(){

n(“start the computer!”);

p();

p();

p();

n(“start computer finished!”);

}

public void shutdown(){

n(“begin to close the computer!”);

wn();

wn();

wn();

n(“computer closed!”);

}

}

User类如下:public class User {

public static void main(String[] args) {

Computer computer = new Computer();

p();

wn();

}

}

输出:

arrayInt[0] = nt(array[0]);

arrayInt[1] = nt(array[1]);

return arrayInt;

}

}

三个实现类:public class Plus extends AbstractCalculator implements ICalculator {

@Override

public int calculate(String exp) {

int arrayInt[] = split(exp,”+”);

return arrayInt[0]+arrayInt[1];

}

}

public class Minus extends AbstractCalculator implements ICalculator {

@Override

public int calculate(String exp) {

int arrayInt[] = split(exp,”-“);

return arrayInt[0]-arrayInt[1];

}

}

public class Multiply extends AbstractCalculator implements ICalculator {

@Override

public int calculate(String exp) {

int arrayInt[] = split(exp,”*”);

return arrayInt[0]*arrayInt[1];

}

}

简单的测试类:public class StrategyTest {

public static void main(String[] args) {

String exp = “2+8″;

ICalculator cal = new Plus();

int result = ate(exp);

n(result);

}

public MyIterator(Collection collection){

tion = collection;

}

@Override

public Object previous() {

if(pos > 0){

pos–;

}

return (pos);

}

@Override

public Object next() {

if(pos<()-1){

pos++;

}

return (pos);

}

@Override

public boolean hasNext() {

if(pos<()-1){

return true;

}else{

return false;

}

}

@Override

public Object first() {

pos = 0;

return (pos);

}

}

测试类:public class Test {

public static void main(String[] args) {

Collection collection = new MyCollection();

Iterator it = or();

public class MyCommand implements Command {

private Receiver receiver;

public MyCommand(Receiver receiver) {

er = receiver;

}

@Override

public void exe() {

();

}

}

public class Receiver {

public void action(){

n(“command received!”);

}

}

public class Invoker {

private Command command;

public Invoker(Command command) {

d = command;

}

public void action(){

();

}

}

public class Test {

public static void main(String[] args) {

Receiver receiver = new Receiver();

Command cmd = new MyCommand(receiver);

Invoker invoker = new Invoker(cmd);

();

}

}

}

}

public class Memento {

private String value;

public Memento(String value) {

= value;

}

public String getValue() {

return value;

}

public void setValue(String value) {

= value;

}

}

public class Storage {

private Memento memento;

public Storage(Memento memento) {

o = memento;

}

public Memento getMemento() {

return memento;

}

public void setMemento(Memento memento) {

o = memento;

}

}

测试类:public class Test {

public static void main(String[] args) {

// 创建原始类

Original origi = new Original(“egg”);

public void setValue(String value) {

= value;

}

public void method1(){

n(“execute the first opt!”);

}

public void method2(){

n(“execute the second opt!”);

}

}

package ;

/**

* 状态模式的切换类 2012-12-1

* @author erqing

*

*/

public class Context {

private State state;

public Context(State state) {

= state;

}

public State getState() {

return state;

}

public void setState(State state) {

= state;

}

public void method() {

if (ue().equals(“state1″)) {

1();

} else if (ue().equals(“state2″)) {

2();

}

private Mediator mediator;

public Mediator getMediator(){

return mediator;

}

public User(Mediator mediator) {

or = mediator;

}

public abstract void work();

}

public class User1 extends User {

public User1(Mediator mediator){

super(mediator);

}

@Override

public void work() {

n(“user1 exe!”);

}

}

public class User2 extends User {

public User2(Mediator mediator){

super(mediator);

}

@Override

public void work() {

n(“user2 exe!”);

}

}

测试类:public class Test {

public static void main(String[] args) {

Mediator mediator = new MyMediator();

Mediator();

l();

return num1;

}

public void setNum1(int num1) {

1 = num1;

}

public int getNum2() {

return num2;

}

public void setNum2(int num2) {

2 = num2;

}

}

public class Test {

public static void main(String[] args) {

// 计算9+2-8的值

int result = new Minus().interpret((new Context(new Plus()

.interpret(new Context(9, 2)), 8)));

n(result);

}

}

最后输出正确的结果:3。


本文标签: 状态 快捷键 原始 输出 模式