//HashCode方法,JAVA中的对象都有自身的HashCode码
class Person02{
private String name;
private int age;
public Person02(String name,int age){
this.name=name;
this.age=age;
}
public String toString(){
return "姓名:"+this.name+"年龄:"+this.age;
}
//复写equals 和 HashCode 方法
//HashCode 方更多的是表示对象 的地址
public boolean equals(Object obj){
return true;
}
public int hashCode(){
return this.name.hashCode()+this.age*1000;
}
}
public class AdvDemo02 {
public static void main(String[] args) {
Map m=new HashMap();
m.put(new Person02("zhansan",30),"zhansan");
m.put(new Person02("zhansan",20),"zhansan");
//如果不覆写equals 和 HashCode 方法的话是不能查找的得到 的
System.out.println(m.get(new Person02("zhansan",30)));
System.out.println(m.get(new Person02("zhansan",10)));
}
}
//对象序列化
//对象序列化是指把对象写入到流中,对象的反序列化是指从流中恢复对象
//其优点是:即使程序运行结束,对象仍然可以保存下来。必须实现Serializable接口
//一个类想实现序列号,必须实现Serializable接口
//如果某个属性不要求进行序列化,则用关键字transient。
class Person implements Serializable
{
private String name;
//private int age;
//如果某个属性不要求进行序列化
private transient int age;
public Person(String name,int age){
this.name = name;
this.age = age ;
}
public String toString(){
return this.name+" "+this.age ;
}
}
//Serializable接口
//此接口只是一个声明接口,表示实现此接口的类可以被实例化
//如果某个属性不要求进行序列化,则用关键字transient。
public class SerializableTest
{
public static void main(String args[])throws Exception{
Person p = new Person("liuwei",23);
//序列化。谁为ObjectOutputStream对象实例化就向哪里输出
ObjectOutputStream oos = null;
oos = new ObjectOutputStream(new FileOutputStream(new File("c://123.txt")));
oos.writeObject(p);
//反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("c://123.txt")));
Person p1 = (Person)ois.readObject();
System.out.println(p1);
ois.close();
oos.close();
}
}
//克隆方法的使用,必须实现Cloneable接口
//而且必须覆写Object类的clone方法
//protected Object clone()throws CloneNotSupportedException
//Cloneable也是一个标识接口。和类似于Serializable
class Person03 implements Cloneable{
public String name;
private int age;
public Person03(String name,int age){
this.name=name;
this.age=age;
}
public String toString(){
return "姓名:"+this.name+" 年龄:"+this.age;
}
//扩大父类的权限为公共的
@Override
public Object clone()
throws CloneNotSupportedException{
return super.clone();
}
}
public class AdvDemo03 {
public static void main(String[] args) throws Exception {
Person03 p=new Person03("zhansan",12);
Person03 p2=(Person03) p.clone();
p2.name="lisi";
System.out.println(p);
System.out.println(p2);
//System.out.println(p2.age);
}
}
//比较器:Array类是一个专门用于数组排序的,可以对任何数据进行排序
//对象排序的内容必须实现Comparable接口,覆写compareTo方法定义规则
//还可以用在TreeSet上实际上TreeSet也需要比较器的支持
class Person5 implements Comparable{
private String name;
private float score;
private int age;
public Person5(String name,int age,float score){
this.name=name;
this.age=age;
this.score = score ;
}
public String toString(){
return "姓名:"+this.name+" 年龄:"+this.age+"分数"+this.score;
}
@Override
public int compareTo(Object obj){
Person5 p=(Person5)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 class AdvDemo5 {
public static void main(String args[])
{
/*Person5 p[] = new Person5[5] ;
p[0] = new Person5("张三",20,96) ;
p[1] = new Person5("李四",19,96) ;
p[2] = new Person5("王五",19,97) ;
p[3] = new Person5("赵六",21,78) ;
p[4] = new Person5("孙七",20,80) ;
Arrays.sort(p) ;
for(int i=0;i<p.length;i++)
{
System.out.println(p[i]) ;
}*/
//如果把compareTo方法除掉,则会包类型转换异常java.lang.ClassCastException
Set s = new TreeSet() ;
s.add(new Person5("张三",20,96)) ;
s.add(new Person5("李四",19,96)) ;
s.add(new Person5("王五",19,97)) ;
s.add(new Person5("赵六",21,78)) ;
s.add(new Person5("孙七",20,80)) ;
Iterator iter = s.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
}
}
//Class类的用法:反射
class Person7
{
private String name ;
private int age ;
private float score ;
public Person7(String name,int age,float score)
{
this.name = name ;
this.age = age ;
this.score = score ;
}
public String toString()
{
return "姓名:"+this.name+",年龄:"+this.age+",成绩:"+this.score ;
}
};
public class AdvDemo7
{
public static void main(String args[]) throws Exception
{
Person7 p = null ;
// 通过Class实例化Person7对象
// p = (Person7)Class.forName("Person7").newInstance() ;
// 根源在于,程序实例化时需要使用构造方法
Class cl = Class.forName("Person7") ;
Object obj[] = {"张三",new Integer(30),new Float(89)} ;
Constructor c = cl.getConstructors()[0] ;
p = (Person7)c.newInstance(obj) ;
System.out.println(p) ;
}
};