package net.xxt;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/**
* 支持HashCode查找, 重写了equals和hashCode方法
* 支持对象克隆,实现了Cloneable接口,重写了clone方法
* 支持对象比较,实现了Comparable接口,重写了compareTo方法
* 支持直接打印对象,重写了toString方法。
* @author zhaoguoli
* @version v1.0 2009-07-09 0:30 创建
*/
class Person implements Cloneable, Comparable {
private String name;
private float score;
private int age;
public Person(String name, int age, float score) {
this.name = name;
this.age = age;
this.score = score;
}
public boolean equals(Object obj) {
return true;
}
/**
* 必须要重写equals方法才行
*/
public int hashCode() {
return this.name.hashCode() + this.age * 1000;
}
/**
* 实现Cloneable接口,protected --> public ,抛出异常,调用父类SUPER
*/
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
/**
* 实现Comparable接口,重写compareTo方法,返回值1,0,-1
* 覆写compareTo方法就是重建排序规则
*/
public int compareTo(Object obj) {
Person p = (Person) obj;
if (p.score > this.score) {
return 1;
} else if (p.score < this.score) {
return -1;
} else {
// 如果成绩相等则判断年龄
if (p.age > this.age) {
return 1;
} else if (p.age < this.age) {
return -1;
} else {
return 0;
}
}
}
/**
* 定义打印格式
*/
public String toString() {
return "姓名:" + this.name + ",年龄:" + this.age + ",成绩:" + this.score;
}
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;
}
};
/**
* java特性测试及汇总
* @author 赵国力
* @version v1.0 2009-07-09 0:36 创建
*/
public class JavaAdv {
/**
* 测试类
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// hashcode测试
System.out.println("hashCode方法使用测试");
Map m = new HashMap();
m.put(new Person("张三", 30, 30), "张三");
System.out.println(m.get(new Person("张三", 30, 30)));
System.out.println("------------------------------");
// clone测试
System.out.println("clone方法使用测试");
Person p1 = new Person("张三", 30, 30);
Person p2 = (Person) p1.clone();
p2.setName("李四");
System.out.println(p1);
System.out.println(p2);
System.out.println("------------------------------");
// 比较对象测试
System.out.println("compareTo方法使用测试");
Person p[] = new Person[5];
p[0] = new Person("张三", 20, 96);
p[1] = new Person("李四", 19, 96);
p[2] = new Person("王五", 19, 97);
p[3] = new Person("赵六", 21, 78);
p[4] = new Person("孙七", 20, 80);
Arrays.sort(p);
for (int i = 0; i < p.length; i++) {
System.out.println(p[i]);
}
System.out.println("------------------------------");
//比较对象测试2
System.out.println("compareTo方法使用测试");
Set s = new TreeSet();
s.add(new Person("张三", 20, 96));
s.add(new Person("李四", 19, 96));
s.add(new Person("王五", 19, 97));
s.add(new Person("赵六", 21, 78));
s.add(new Person("孙七", 20, 80));
Iterator iter = s.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
System.out.println("------------------------------");
}
}
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/**
* 支持HashCode查找, 重写了equals和hashCode方法
* 支持对象克隆,实现了Cloneable接口,重写了clone方法
* 支持对象比较,实现了Comparable接口,重写了compareTo方法
* 支持直接打印对象,重写了toString方法。
* @author zhaoguoli
* @version v1.0 2009-07-09 0:30 创建
*/
class Person implements Cloneable, Comparable {
private String name;
private float score;
private int age;
public Person(String name, int age, float score) {
this.name = name;
this.age = age;
this.score = score;
}
public boolean equals(Object obj) {
return true;
}
/**
* 必须要重写equals方法才行
*/
public int hashCode() {
return this.name.hashCode() + this.age * 1000;
}
/**
* 实现Cloneable接口,protected --> public ,抛出异常,调用父类SUPER
*/
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
/**
* 实现Comparable接口,重写compareTo方法,返回值1,0,-1
* 覆写compareTo方法就是重建排序规则
*/
public int compareTo(Object obj) {
Person p = (Person) obj;
if (p.score > this.score) {
return 1;
} else if (p.score < this.score) {
return -1;
} else {
// 如果成绩相等则判断年龄
if (p.age > this.age) {
return 1;
} else if (p.age < this.age) {
return -1;
} else {
return 0;
}
}
}
/**
* 定义打印格式
*/
public String toString() {
return "姓名:" + this.name + ",年龄:" + this.age + ",成绩:" + this.score;
}
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;
}
};
/**
* java特性测试及汇总
* @author 赵国力
* @version v1.0 2009-07-09 0:36 创建
*/
public class JavaAdv {
/**
* 测试类
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// hashcode测试
System.out.println("hashCode方法使用测试");
Map m = new HashMap();
m.put(new Person("张三", 30, 30), "张三");
System.out.println(m.get(new Person("张三", 30, 30)));
System.out.println("------------------------------");
// clone测试
System.out.println("clone方法使用测试");
Person p1 = new Person("张三", 30, 30);
Person p2 = (Person) p1.clone();
p2.setName("李四");
System.out.println(p1);
System.out.println(p2);
System.out.println("------------------------------");
// 比较对象测试
System.out.println("compareTo方法使用测试");
Person p[] = new Person[5];
p[0] = new Person("张三", 20, 96);
p[1] = new Person("李四", 19, 96);
p[2] = new Person("王五", 19, 97);
p[3] = new Person("赵六", 21, 78);
p[4] = new Person("孙七", 20, 80);
Arrays.sort(p);
for (int i = 0; i < p.length; i++) {
System.out.println(p[i]);
}
System.out.println("------------------------------");
//比较对象测试2
System.out.println("compareTo方法使用测试");
Set s = new TreeSet();
s.add(new Person("张三", 20, 96));
s.add(new Person("李四", 19, 96));
s.add(new Person("王五", 19, 97));
s.add(new Person("赵六", 21, 78));
s.add(new Person("孙七", 20, 80));
Iterator iter = s.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
System.out.println("------------------------------");
}
}