用Java在IntSort类中3个同名方法sort,实现排序。 sort(int a,int b); sort(int a,int b,int c); sort(int a[]);
时间: 2023-12-12 14:05:45 浏览: 25
可以在IntSort类中通过方法重载实现这三个同名的sort方法,具体代码如下:
```
public class IntSort {
// 定义两个参数的sort方法
public void sort(int a, int b) {
int[] arr = {a, b};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
// 定义三个参数的sort方法
public void sort(int a, int b, int c) {
int[] arr = {a, b, c};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
// 定义数组参数的sort方法
public void sort(int[] a) {
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
public static void main(String[] args) {
IntSort intSort = new IntSort();
intSort.sort(2, 1);
intSort.sort(3, 1, 2);
int[] arr = {5, 3, 2, 4, 1};
intSort.sort(arr);
}
}
```
以上代码中,通过方法的重载实现了三个同名的sort方法,分别接收不同的参数类型。在main方法中,分别传入两个参数、三个参数和数组参数进行测试。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)