Java SE 面向对象编程的3个常用接口
作者:wwzzzzzzzzzzzzz? 时间:2023-01-14 06:42:32
1.Comparable
前言,想要排序Student.有代码:
import java.util.Arrays;
class Student {
public int age;
public String name;
public double score;
public Student(int age, String name, double score) {
this.age = age;
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
", score=" + score +
'}';
}
}
public class TestDemo {
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student(12,"niubi",99.9);
students[1] = new Student(20,"liuren",18.9);
students[2] = new Student(80,"laoren",50.9);
System.out.println(Arrays.toString(students));
Arrays.sort(students);
System.out.println(Arrays.toString(students));
}
}
此代码运行报错:
原因: 没有告诉要如何进行排序,是年龄还是姓名还是分数.没有告诉比较的规则
解决方式:
如果自定义的数据类型 进行大小比较 一定要实现可以比较的接口
import java.util.Arrays;
class Student implements Comparable<Student>{
public int age;
public String name;
public double score;
public Student(int age, String name, double score) {
this.age = age;
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
", score=" + score +
'}';
}
//谁调用这个方法 谁就是this
@Override
public int compareTo(Student o) {
//return this.age - o.age;//从小到大
return o.age - this.age;//从大到小
}
}
public class TestDemo {
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student(12,"niubi",99.9);
students[1] = new Student(6,"liuren",18.9);
students[2] = new Student(80,"laoren",50.9);
System.out.println("比较前 "+Arrays.toString(students));
Arrays.sort(students);//默认从小到大排序
System.out.println("比较后 "+Arrays.toString(students));
}
}
如果要 分数比较 和 姓名比较
//谁调用这个方法 谁就是this
@Override
public int compareTo(Student o) {
//return this.age - o.age;//从小到大
//return o.age - this.age;//从大到小
return (int) (this.score - o.score);//分数排序
return this.name.compareTo(o.name);//姓名排序
}
缺点: 这个接口对类的侵入性非常强.一旦写好了,不敢轻易改动.
如何降低对类的侵入性呢?
使用Comparator
2.Comparator 比较器
import java.util.Arrays;
import java.util.Comparator;
class Student1 {
public int age;
public String name;
public double score;
public Student1(int age, String name, double score) {
this.age = age;
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
", score=" + score +
'}';
}
}
class AgeComparator implements Comparator<Student1>{
@Override
public int compare(Student1 o1, Student1 o2) {
return o1.age - o2.age;
}
}
class ScoreComparator implements Comparator<Student1>{
@Override
public int compare(Student1 o1, Student1 o2) {
return (int) (o1.score - o2.score);
}
}
class NameComparator implements Comparator<Student1>{
@Override
public int compare(Student1 o1, Student1 o2) {
return o1.name.compareTo(o2.name);
}
}
public class TestDemo1 {
public static void main(String[] args) {
Student1[] students1 = new Student1[3];
students1[0] = new Student1(12,"niubi",99.9);
students1[1] = new Student1(6,"liuren",18.9);
students1[2] = new Student1(80,"laoren",50.9);
System.out.println("比较前 "+Arrays.toString(students1));
AgeComparator ageComparator = new AgeComparator();
Arrays.sort(students1,ageComparator);
System.out.println("比较后(按年龄) "+Arrays.toString(students1));
ScoreComparator scoreComparator = new ScoreComparator();
Arrays.sort(students1,scoreComparator);
System.out.println("比较后(按姓名) "+Arrays.toString(students1));
NameComparator nameComparator = new NameComparator();
Arrays.sort(students1,nameComparator);
System.out.println("比较后(按分数) "+Arrays.toString(students1));
}
}
运行结果:
优点:对类的侵入性非常弱.
3.Cloneable
面试问题:
你知道Cloneable接口吗?为啥这个接口是一个空接口?有啥作用?
空接口 -> 标志接口 -> 代表当前这个类是可以被克隆的.
class Person implements Cloneable{
public int age ;
public void eat(){
System.out.println("吃!");
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class TestDemo2 {
public static void main(String[] args) throws CloneNotSupportedException {
Person person = new Person();
person.age = 99;
Person person2 = (Person) person.clone();
System.out.println(person.age);
System.out.println(person2.age);
System.out.println("==========");
person2.age = 199;
System.out.println(person.age);
System.out.println(person2.age);
}
}
运行结果:
注意事项:
1.引用的对象要想被克隆,必须实现Cloneable接口.
2.必须重写克隆方法,并且声明异常.
来源:https://blog.csdn.net/wwzzzzzzzzzzzzz/article/details/121453238
标签: Java,SE,面向对象,编程,接口
0
投稿
猜你喜欢
C++数组指针和二维数组详情
2022-03-31 21:11:07
SpringMVC之AbstractAnnotationConfigDispatcherSer解读
2023-06-21 21:42:32
详解JAVA之运算符
2023-09-05 13:22:46
Java读取TXT文件内容的方法
2023-11-23 22:33:41
基于ThreadLocal 的用法及内存泄露(内存溢出)
2023-11-23 20:22:28
unity shader实现玻璃折射效果
2022-04-16 08:22:36
C语言实现学生信息管理系统
2023-05-24 12:08:50
Android自定义ActionBar实例
2022-04-28 01:33:29
Java汉字转拼音工具类完整代码实例
2021-07-09 21:32:18
C# 运用params修饰符来实现变长参数传递的方法
2023-07-21 22:27:55
Java本地缓存的实现代码
2022-02-22 11:21:04
SpringBoot 实现动态添加定时任务功能
2022-07-12 05:07:40
Spring Boot假死诊断实战记录
2022-02-11 17:03:38
C#中把任意类型的泛型集合转换成SQLXML数据格式的实例
2022-12-24 08:16:21
java property配置文件管理工具框架过程详解
2023-10-12 04:35:50
C# WinForm开发中使用XML配置文件实例
2022-09-07 23:04:03
Android CheckBox中设置padding无效解决办法
2022-09-07 01:35:48
跳一跳自动跳跃C#代码实现
2023-02-19 11:12:46
JAVA中Comparable接口和自定义比较器示例讲解
2023-11-20 22:16:32
Mybatis中resultMap的Colum和property属性详解
2023-09-16 11:17:33