Java 比较接口comparable与comparator区别解析
作者:奋斗中的小蜗牛 时间:2022-11-26 20:54:24
这篇文章主要介绍了Java 比较接口comparable与comparator区别解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
package test0;
import java.util.Comparator;
//限定修饰符为friend不能为public,一个java文件中只能有一个public类
/***
* java程序是从一个public类的main函数开始执行的,
*(其实是main线程),就像c程序是从main()函数开始执行一样。
* 只能有一个public类是为了给类装载器提供方便。
*/
interface MyComparable<T>{ //接口类--抽象类
public int compare(T agr0); //抽象方法--在子类中实现
}
class student implements Comparable<student> {
private String name="zhang";
private int num=12;
@Override
public String toString() {
return "名字为:" + name +","+ "学号为:" + num;
}
@Override
public int compareTo(student arg0) {
return this.num-arg0.num;
}
}
class teacher implements Comparator<student> {
private String name="zhang";
private int num=12;
@Override
public String toString() {
return "名字为:" + name +","+ "学号为:" + num;
}
@Override
public int compare(student arg0, student arg1) {
// TODO Auto-generated method stub
return 0;
}
}
public class Test {
public static void main(String[] args) {
teacher space=new teacher();
}
}
来源:https://www.cnblogs.com/z2529827226/p/11638527.html
标签:java,比较,接口,comparable,comparator
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JavaWeb如何实现禁用浏览器缓存
2021-09-13 01:27:45
C#读写文本文件的方法
2022-02-01 15:20:51
如何设计一个安全的API接口详解
2023-03-06 14:57:03
![](https://img.aspxhome.com/file/2023/8/67498_0s.png)
java mybatis框架配置详解
2023-11-25 08:09:16
c# 通过代码开启或关闭防火墙
2021-12-11 19:53:20
Android开发中线程池源码解析
2023-12-19 02:30:57
![](https://img.aspxhome.com/file/2023/5/139395_0s.jpg)
Java实现自动压缩文件并加密的方法示例
2023-05-08 03:33:18
.net与javascript脚本的交互方法总结
2021-09-11 09:21:32
WPF如何自定义TabControl控件样式示例详解
2021-09-22 07:27:34
![](https://img.aspxhome.com/file/2023/7/108827_0s.gif)
关于jdk9、jdk10、jdk11、jdk12、jdk13新特性说明
2021-07-19 17:06:34
![](https://img.aspxhome.com/file/2023/5/85415_0s.png)
使用Nexus搭建Maven私服教程的方法步骤
2023-03-05 18:56:58
![](https://img.aspxhome.com/file/2023/3/66203_0s.jpg)
详解Android Studio无法检测新版本问题解决
2021-08-01 08:45:05
![](https://img.aspxhome.com/file/2023/4/117114_0s.png)
unity 如何修改材质属性和更换shader
2023-02-22 12:42:44
![](https://img.aspxhome.com/file/2023/0/92950_0s.jpg)
java中ThreadLocal的基本原理
2022-03-28 01:13:44
![](https://img.aspxhome.com/file/2023/2/76502_0s.png)
C#线程中弹窗的制作方法
2023-08-14 03:25:32
![](https://img.aspxhome.com/file/2023/2/100122_0s.jpg)
用C#缩小照片上传到各种空间的具体方法
2022-03-28 02:29:18
使用C语言编写基于TCP协议的Socket通讯程序实例分享
2023-07-05 03:30:03
springboot从application.properties中注入list, map方式
2023-11-28 23:42:33
![](https://img.aspxhome.com/file/2023/9/60539_0s.jpg)
Java字符串拼接新方法 StringJoiner用法详解
2022-06-16 13:02:13
基于C#实现磁性吸附窗体
2022-09-04 07:04:51
![](https://img.aspxhome.com/file/2023/7/89787_0s.gif)