疯狂java讲义(李刚)6.2

xiaoxiao2021-02-28  49

6.2处理对象

6.2.1打印对象和toString方法

class Person{ private String name; public Person(String name) { this.name=name; }}public class PrintObject{ public static void main(String[] args) { Person p=new Person("孙悟空"); System.out.println(p); }

}//输出Person@6d06d69c

Systm.out的println()方法只能在控制台输出字符串,而Person实例是一个内存中的对象,当使用该方法输出Person对象时,实际上输出的是Person对象的toString()方法的返回值。

也就是说:System.out.println(p);

                System.out.println(p.toString());是等价的

String pStr=p+"";

String pStr=p.toString()+"";结果也完全相同

toString()方法是一个非常特殊的方法,它是一个自我描述的方法。Object类提供的toString()方法总是返回该对象实现类的”类名@hashCode“值,这个返回值不能真正实现自我描述的功能,因此如果用户需要自定义能实现自我描述的功能,就必须重写Object类的toString()方法

class Apple{ private String color; private double weight; public Apple(){} public Apple(String color,double weight) { this.color=color; this.weight=weight; } public String toString() { //return"一个苹果,颜色是:"+color+",重量是:"+weight; return "Apple[color="+color+",weight="+weight+"]"; }}public class ToStringTest{ public static void main(String[] args) { Apple a=new Apple("红色",5.68); System.out.println(a); }

}

6.2.2==和equals方法

==:如果两个变量是基本类型变量,且都是数值类型(不一定要求数据类型严格相同),则只要两个变量的值相等,就将返回true,对于两个引用类型变量,只有它们指向同一个对象时,==判断才会返回true,==不可用于比较类型上没有父子关系的两个对象。

public class EqualTest{ public static void main(String[] args) { int it=65; float f1=65.0f; System.out.println("65和65.0f是否相等:"+(it==f1));//true char ch='A'; System.out.println("65和A是否相等:"+(it==ch));//true String str1=new String("hello"); String str2=new String("hello"); System.out.println("str1和str2是否相等:"+(str1==str2));//false System.out.println("str1和str2是否相等:"+(str1.equals//true //由于java.lang.String与EqualTest类没有继承关系,所以下面语句导致编译错误 //System.out.println("hello"==new EqualTest()); }

}

"hello"直接量和new String("hello")有什么区别:

"hello":JVM将会使用常量池来管理这些字符串

new String("hello"):JVM会先使用常量池来管理"hello"直接量,再调用String类的构造器来创建一个新的String对象,新创建的String对象被保存在堆内存中,换句话说,new String("hello")一共产生了两个字符串对象

常量池专门用于管理在编译时被确定并被保存在已编译的.class文件中的一些数据,它包括了关于类,方法,接口中的常量,还包括字符串常量

public class StringCompareTest{ public static void main(String[] args) { String s1="疯狂java"; String s2="疯狂"; String s3="java"; String s4="疯狂"+"java"; String s5="疯"+"狂"+"java"; String s6=s2+s3; String s7=new String("疯狂java"); System.out.println(s1==s4);//true System.out.println(s1==s5);//true System.out.println(s1==s6);//false System.out.println(s1==s7);//false }

}

重写equals()方法

class Person{ public boolean equals(Object obj) { return true; }}class Dog{}public class OverrideEqualsError{ public static void main(String[] args) { Person p=new Person(); System.out.println("Person对象是否equals Dog对象?"+p.equals(new Dog()));//true System.out.println("Person对象是否equals String对象?"+p.equals(new String("hello")));//true }

}

重写Person类的equals()方法

class Person{ private String name; private String idStr; public Person(){} public Person(String name,String idStr) { this.name=name; this.idStr=idStr; } //此处省略name和idStr的setter和getter方法 public boolean equals(Object obj) { if(this==obj) { return true; } if(obj!=null&&obj.getClass()==Person.class) { Person personObj=(Person)obj; if(this.getIdStr().equals(personObj.getIdStr())) { return true; } } return false; }}public class OverrideEqualsRight{ public static void main(String[] args) { Person p1=new Person("孙悟空","12343433433"); Person p2=new Person("孙行者","12343433433"); Person p3=new Person("孙悟饭","99933433"); System.out.println("p1和p2是否相等?"+p1.equals(p2));//true System.out.println("p2和p3是否相等?"+p2.equals(p3));//false }}

转载请注明原文地址: https://www.6miu.com/read-2621911.html

最新回复(0)