public class StrategyClient { public static void main(String[] args) { PayAmountContext payAmountContext = new PayAmountContext(); // 使用直减策略 payAmountContext.setPayAmount(new DecreasePayAmountStrategy()); payAmountContext.payAmount(null); // 使用N元购策略 payAmountContext.setPayAmount(new NYuanPayAmountStrategy()); payAmountContext.payAmount(null); // 使用满减策略 payAmountContext.setPayAmount(new CouponPayAmountStrategy()); payAmountContext.payAmount(null); // 使用折扣策略 payAmountContext.setPayAmount(new DiscountPayAmountStrategy()); payAmountContext.payAmount(null); } }
时间: 2023-07-16 08:11:52 浏览: 34
这是一个策略模式的示例代码,用于演示如何使用不同的策略实现不同的行为。具体来说,该代码定义了一个支付金额的上下文类 PayAmountContext,它可以根据不同的策略实现不同的支付金额计算方式。在主函数中,我们可以看到使用了四种不同的策略:直减策略、N元购策略、满减策略和折扣策略,并且每种策略都被传递到 PayAmountContext 中进行计算。
相关问题
public class E { public static void main(String args[]) { ComputerFrame fr = new ComputerFrame(); //【1】窗口标题设为“计算” } }
在示例代码中,我们可以通过以下方式将窗口标题设为“计算”:
```java
public class E {
public static void main(String args[]) {
ComputerFrame fr = new ComputerFrame();
fr.setTitle("计算"); // 【1】设置窗口标题为“计算”
}
}
```
在这个示例中,我们创建了一个名为`fr`的`ComputerFrame`对象,并使用`fr.setTitle("计算")`的方式将窗口标题设为“计算”。需要注意的是,这个方法需要在创建完窗口对象之后调用。
class A { static int m; static void f0) f m = 20 ; //A ] class B extends A void f() f m = 222 ; } / /B 1/C class E f public static void main (String args[]) A. f();
这段代码有一些语法错误,我来帮你分析一下:
1. 在 class A 中,static int m; 后面的 f0) 应该是一个方法名,但是缺少了参数列表,应该改成 f0()。
2. 在 A 类的方法 f0() 中,应该是 f(m) = 20; 而不是 f m = 20;,因为 f 是一个方法名,需要用小括号包裹参数。
3. 在 B 类中,应该是 void f() 而不是 void f;
4. 在 B 类的方法 f() 中,应该是 A.f(m) = 222; 而不是 f m = 222;,因为 A.f(m) 才是调用 A 类的方法 f。
5. 在 class E 中,main 方法的命名应该是小写的 main,而不是大写的 MAIN。
6. 在 main 方法中,应该改为 A.f0(); 而不是 A.f();,因为 A 类中的方法名是 f0,而不是 f。
所以,修正后的代码应该是这样的:
```
class A {
static int m;
static void f0() {
f(m) = 20;
}
}
class B extends A {
void f() {
A.f(m) = 222;
}
}
class E {
public static void main(String[] args) {
A.f0();
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)