Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1707004543王红亮 #40

Open
hlwang98 opened this issue Nov 25, 2017 · 0 comments
Open

1707004543王红亮 #40

hlwang98 opened this issue Nov 25, 2017 · 0 comments

Comments

@hlwang98
Copy link

import java.util.Scanner;
public class BaseSort {
            public void sort(int []a){
                System.out.println("排序算法");
            }
        }
//选择排序
 class SelectSort extends BaseSort{
    @Override
    public void sort (int a[]){
         for (int i = 0; i < 10; i++) {
            for (int j = i; j < 10; j++) {
               if(a[j] < a[i])
               {
                   a[i] =a[i] ^a[j];
                   a[j] =a[i] ^a[j];
                   a[i] =a[i] ^a[j];
              }
            }
          }
         for (int j = 0; j < 10; j++) {
         System.out.print(a[j]+" ");
         }
        }
    }

//插入排序
 class InsertSort extends BaseSort{
        }
//快速排序
 class QuickSort extends BaseSort{
        }
//策略类
 class Factory{
        private BaseSort sort;
            //依赖注入
                public void setSort(BaseSort sort){
                    this.sort = sort;
                }
                public void doSort(int []a){
                    sort.sort(a);
        }
        }
 class Test{
                public static void main(String[] args) {
                //自己用键盘输入 int a[10]
                    int []a=new int[10];
                    Scanner s = new Scanner(System.in);
                    for (int i = 0; i < 10 ; i++) {
                        a[i] = s.nextInt();
                    }
                Factory factory = new Factory();
                BaseSort select_sort = new SelectSort();
                factory.setSort(select_sort);
                factory.doSort(a) ;
                }
        }

```java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant