标记接口
标记接口(Marker Interface),又称标签接口(Tag Interface)
仅代表一个标记 不包含任何方法
标记接口是用来判断某个类是否具有某种能力
Cloneable标记接口
此类实现了 Cloneable 接口,以指示 Object.clone 方法可以合法地对该类实例进行按字段复制
如果在没有实现 Cloneable 接口的实例上调用 Object 的 clone 方法, 则会导致抛出 CloneNotSupportedException 异常- // Cloneable源码:
- public interface Cloneable { }
- // 仅代表一个标记
- // 克隆的前提条件:
- // 被克隆的对象必须实现Cloneable接口
- // 必须重写clone方法
复制代码 基本使用- public class CloneableDemo {
- public static void main(String[] args) throws CloneNotSupportedException {
- // ArrayList类实现了Cloneable接口
- ArrayList<String> list = new ArrayList<String>();
- list.add("张三");
- list.add("李四");
- list.add("王五");
- ArrayList<String> listClone = (ArrayList<String>) list.clone();
- System.out.println(list == listClone);
- System.out.println(listClone);
- }
- }
复制代码 Clone案例:将一个学生的数据复制到另一个学生对象中,并且两个对象不受任何的影响.
传统方式:- public class CloneTest {
- public static void main(String[] args) {
- //传统方式:
- Student stu1 = new Student("张三", 12);
- //再次创建一个新的学生对象
- Student stu2 = new Student();
- //把stu1对象name的值取出来赋值给stu2对象的name
- stu2.setName(stu1.getName());
- //把stu1对象age的值取出来赋值给stu2对象的age
- stu2.setAge(stu1.getAge());
- System.out.println(stu1 == stu2);
- System.out.println(stu1);
- System.out.println(stu2);
- System.out.println("================");
- stu1.setName("李四");
- System.out.println(stu1);
- System.out.println(stu2);
- }
- }
- class Student {
- private String name;
- private int age;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public Student() {
- }
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
复制代码此时修改对象1的姓名与对象2的姓名无关
克隆方式(浅拷贝):- /*
- 实现Cloneable接口
- 重写clone方法 将访问权限改为public 并将返回值类型改为Student
- */
- class Student implements Cloneable{
- private String name;
- private int age;
- public Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
- @Override
- public Student clone() throws CloneNotSupportedException {
- return (Student) super.clone();
- }
- }
复制代码- class CloneTest01 {
- public static void main(String[] args) throws CloneNotSupportedException {
- //克隆方式
- //创建学生对象
- Student stu1 = new Student("张三",12);
- //通过克隆获得一个student对象
- Student stu2 = stu1.clone();
- System.out.println(stu1 == stu2);
- System.out.println(stu1);
- System.out.println(stu2);
- stu1.setName("李四");
- System.out.println(stu1);
- System.out.println(stu2);
- }
- }
复制代码 浅拷贝的局限性:基本数据类型(包括String)可以达到完全复制,引用数据类型则不可以;- class Student implements Cloneable{
- private String name;
- private int age;
- private Car car;
- @Override
- public Student clone() throws CloneNotSupportedException {
- return (Student) super.clone();
- }
- }
- class Car {
- private String brand;
- public Car() {
- }
- public Car(String brand) {
- this.brand = brand;
- }
- public String getBrand() {
- return brand;
- }
- public void setBrand(String brand) {
- this.brand = brand;
- }
- @Override
- public String toString() {
- return "Car{" +
- "brand='" + brand + '\'' +
- '}';
- }
- }
- class CloneTest02 {
- public static void main(String[] args) throws CloneNotSupportedException {
- //克隆方式
- //创建学生对象
- Student stu1 = new Student("张三",12,new Car("宝马"));
- //通过克隆获得一个student对象
- Student stu2 = stu1.clone();
- System.out.println(stu1 == stu2);
- System.out.println(stu1);
- System.out.println(stu2);
- stu1.setName("李四");
- //stu1获得了Car修改Car的品牌
- stu1.getCar().setBrand("奔驰");
- //浅拷贝的局限性 引用类型只是拷贝了地址值 修改一个对象的的属性 另一个也改变了
- System.out.println(stu1);
- System.out.println(stu2);
- }
- }
复制代码 使用深拷贝解决上述问题- //首先 Car类实现克隆接口 重写clone方法
- class Car implements Cloneable {
- private String brand;
- public Car() {
- }
- public Car(String brand) {
- this.brand = brand;
- }
- public String getBrand() {
- return brand;
- }
- public void setBrand(String brand) {
- this.brand = brand;
- }
- @Override
- public String toString() {
- return "Car{" +
- "brand='" + brand + '\'' +
- '}';
- }
- @Override
- protected Car clone() throws CloneNotSupportedException {
- return (Car) super.clone();
- }
- }
- //修改Student的clone方法
- class Student implements Cloneable {
- private String name;
- private int age;
- private Car car;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Student(String name, int age, Car car) {
- this.name = name;
- this.age = age;
- this.car = car;
- }
- public Car getCar() {
- return car;
- }
- public void setCar(Car car) {
- this.car = car;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public Student() {
- }
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", car=" + car +
- '}';
- }
- @Override
- public Student clone() throws CloneNotSupportedException {
- // return (Student) super.clone();
- Student student = (Student) super.clone();
- Car car = this.car.clone();
- student.setCar(car);
- return student;
- }
- }
复制代码 RandomAccess标记接口
List 实现所使用的标记接口,用来表明其支持快速(通常是固定时间)随机访问。此接口的主要目的是允许一般的算法更改其行为,从而在将其应用到随机或连续访问列表时能提供良好的性能
简单的来说,如果实现了这个接口,普通for循环的速度要优于增强for的速度.- public class ArrayList_Demo01 {
- public static void main(String[] args) {
- //创建ArrayList集合
- List<String> list = new ArrayList<String>();
- //添加100W条数据
- for (int i = 0; i < 1000000; i++) {
- list.add(i+"a");
- }
- //测试普通循环
- long startTime = System.currentTimeMillis();
- for (int i = 0; i < list.size(); i++) {
- //取出集合的每一个元素
- list.get(i);
- }
- long endTime = System.currentTimeMillis();
- System.out.println("普通循环遍历: "+(endTime-startTime));
- //测试迭代器遍历
- startTime = System.currentTimeMillis();
- //获取迭代器
- Iterator<String> it = list.iterator();
- while (it.hasNext()){
- //取出集合的元素
- it.next();
- }
- endTime = System.currentTimeMillis();
- System.out.println("迭代器遍历: "+(endTime-startTime));
- }
- }
复制代码 LinkedList没有实现此接口,测试:- ublic class LinkedList_Demo01 {
- public static void main(String[] args) {
- //创建LinkedList集合
- List<String> list = new LinkedList<String>();
- //添加10W条数据
- for (int i = 0; i < 100000; i++) {
- list.add(i+"b");
- }
- //测试普通遍历时间
- long startTime = System.currentTimeMillis();
- for (int i = 0; i < list.size(); i++) {
- //取出集合的每一个元素
- list.get(i);
- }
- long endTime = System.currentTimeMillis();
- System.out.println("普通遍历时间: "+(endTime-startTime));
- //测试迭代器
- startTime = System.currentTimeMillis();
- //获取迭代器
- Iterator<String> it = list.iterator();
- while (it.hasNext()){
- //取出集合的每一个元素
- it.next();
- }
- endTime = System.currentTimeMillis();
- System.out.println("顺序迭代器访问时间: "+(endTime-startTime));
- }
- }
复制代码 实际应用- public class Test {
- public static void main(String[] args) {
- //我们今后可能有的集合不是直接创建的 可能是别人传递的
- //我们看到的就是一个List接口
- //至于具体的实现类可能不清楚
- List<String> list = new ArrayList<>();
- //list = new LinkedList<>();
- //我们可以判断以下 这个list集合是否实现了RandomAccess接口
- //如果实现了 可以使用随机访问的方式来进行遍历
- //如果没实现 可以使用迭代器的方式来进行遍历 这样可以提高效率
- if(list instanceof RandomAccess){
- for (int i = 0; i < list.size(); i++) {
- }
- }else {
- for (String s : list) {
- }
- }
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |