Java Comparable以及Colne接口的使用

Comparable

 举例:对集合ArrayList jobs中的提交时间排序(提交时间为属性:submit)进行排序。

              首先对于Job类实现Comparable接口:   public class Job implements Comparable

              实现compareTo方法。返回值为1、-1、0。分别为大于、小于等于;切换1、-1的位置为切换大小排序方式

ps:1、-1、0的模式比较与1、-1更为精确,因为后者不能比较小数。

    @Override
    public int compareTo(Object arg0) {
        // TODO Auto-generated method stub
        float Submit  = ((Job)arg0).submit;
        if(this.submit>Submit) {
            return 1;
        }else if((this.submit             return -1;
        }else {
            return 0;
        }
    }

后再main函数内使用:Collectios.sort(jobs);即可 、Collectios是List、Set、Map三种集合的父接口。

Colne

Java对象的克隆通过Cloneable接口实现:分为浅克隆和深克隆。其区别为浅克隆克隆属性,深度克隆克隆属性是对象的属性;

实质上是深度克隆是多次浅克隆(对于实例化对象A有对象属性B,B含有name属性)。那么深度克隆A即是一次浅克隆A的非对象属性,再在其中浅克隆B的属性。如下代码:

核心为:Student类的(深度)

public Object clone() throws CloneNotSupportedException{
    Student s = (Student) super.clone();               //克隆非对象属性
    s.t = (teacher) t.clone();                                  //克隆了对象属性
         return s;
    }
}

浅克隆:

public Object clone() throws CloneNotSupportedException{
         return super.clone();                     //仅克隆非对象属性
    }

Student类:

package clone;

public class Student implements Cloneable{
public String str = "尝试深度克隆";
public String name;
public int no;
private teacher t;

public teacher getT() {
	return t;
}

public void setT(teacher t) {
	this.t = t;
}

public Student(String name, int no) {
	super();
	this.name = name;
	this.no = no;
}

public String getStr() {
	return str;
}

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public int getNo() {
	return no;
}

public void setNo(int no) {
	this.no = no;
}



@Override
public String toString() {
	return "Student [str=" + str + ", name=" + name + ", no=" + no + ", t=" + t.getName()
			+ "]";
}

public void setStr(String str) {
	this.str = str;
}
public Object clone() throws CloneNotSupportedException{
	Student s = (Student) super.clone();
	s.t = (teacher) t.clone();
         return s;
    }
}

 

Teacher类:

package clone;

public class teacher implements Cloneable{
private String name;


public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}
public Object clone() throws CloneNotSupportedException{
         return super.clone();
    }
}

 

实例类:

package clone;

public class doit {
public static void main(String[] args) throws CloneNotSupportedException {
	Student s1 = new Student("lisi", 1);
	teacher t = new teacher();t.setName("王晓晓");
	System.out.println(t.getName());
	s1.setT(t);
	Student s2 = (Student) s1.clone();
	System.out.println("Before:");
	System.out.println("s1" + s1.toString());
	System.out.println("s2" + s2.toString());
	teacher t2 = s2.getT();
	t2.setName("李明明");
	s2.setT(t2);
	s2.setStr("s2的信息改变");
	s2.setNo(6);
	System.out.println("After:");
	System.out.println("s1" + s1.toString());
	System.out.println("s2" + s2.toString());
}
}

 

你可能感兴趣的:(JAVA学习笔记)