java中main方法的public和static可以互换位置吗?

今天在使用main方法的时候在考虑public和static可以进行互换位置吗?经过尝试是可以的
在这里插入图片描述
但是他究竟是为什么呢?哪位大神可以给我解答一下

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java 的枚举可以用作策略模式的策略类。策略模式的目的是定义一系列算法,将每个算法都封装起来,并且使它们之间可以互换。枚举可以用作策略模式的策略类,因为枚举常量是唯一的,并且可以很方便地用于选择特定的算法。 例如,我们可以定义一个枚举类型来表示不同的排序算法: ``` public enum SortType { BUBBLE_SORT, QUICK_SORT, MERGE_SORT } ``` 然后,我们可以定义一个接口来表示排序算法的策略: ``` public interface SortStrategy { void sort(int[] array); } ``` 接下来,我们可以为每个枚举值实现一个具体的策略: ``` public class BubbleSortStrategy implements SortStrategy { @Override public void sort(int[] array) { // 实现冒泡排序算法 } } public class QuickSortStrategy implements SortStrategy { @Override public void sort(int[] array) { // 实现快速排序算法 } } public class MergeSortStrategy implements SortStrategy { @Override public void sort(int[] array) { // 实现归并排序算法 } } ``` 最后,我们可以在客户端代码使用枚举和策略模式来选择特定的排序算法: ``` public class SortClient { public static void main(String[] args) { SortType sortType = SortType.QUICK_SORT; SortStrategy sortStrategy = null; switch (sortType) { case BUBBLE_SORT: sortStrategy = new BubbleSortStrategy(); break; case QUICK_SORT: sortStrategy = new QuickSortStrategy(); break; case MERGE_SORT: sortStrategy = new MergeSortStrategy(); break; } int[] array = {5, 3, 1, 4, 2}; sortStrategy.sort(array); } } ``` 在这个示例,我们使用枚举来选择排序算法,并使用策略模式来封装算法的实现。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值