-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Description
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) ;
}
}
```javaMetadata
Metadata
Assignees
Labels
No labels