object类
1.Object类查找
第一种方法:去源代码当中。(但是这种方式比较麻烦,源代码也比较难)
第二种方法:去查阅java的类库的帮助文档
2.API
应用程序编程接口。( Application Program Interface)
整个JDK的类库就是一个jabase的API。
每一个API都会配置一套API帮助文档
SUN公司提前写好的这套类库就是API。(一般每一份API都对应一份API帮助文档。)
3.Object类常用方法(暂时)
protected object clone()∥/负责对象克隆的
int hashcode()//获取对象哈希值的一个方法
boolean equals( object ob])//判断两个对象是否相等
string tostring()∥/将对象转换成字符串形式
protected void finalize()//垃圾回收器负责调用的方法
4.解析Object类方法
一.toString方法
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
源代码上 tostring()方法的默认实现是:
类名@对象的内存地址转换为十六进制的形式
public class Test01 {
public static void main(String[] args) {
Mytime T=new Mytime(2001,07,30);
System.out.println(T);
System.out.println(T.toString());
}
}
class Mytime extends Object{
private int year;
private int month;
private int day;
public Mytime() {
}
public Mytime(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public String toString(){
return this.year+"/"+this.month+"/"+this.day;
}
}
2001/7/30
2001/7/30
重写 tostring()方法
这个 tostring()方法怎么重写呢?
越銜洁越好,可读性越强越好
向简洁的、详实的、易阅读的方向发展
二.equals方法
public boolean equals(Object obj) {
return (this == obj);
}
以后编程的过程当中,都要通过equals方法来判断两个对象是否相等equals方法是判断两个对象是否相等的我们需要研究一下 Object类给的这个默认的equals方法够不够用!!!
在 Object类中的equals方法当中,默认采用的是"==" 判断两个java对象是否相等。
而判断"= ="的是两个java对象的内存地址,我们应该判断两个java对象的内容是否相等。所以老祖宗的 equals方法不够用需要子类重写 equals。
public class Test02 {
public static void main(String[] args) {
Mytime m1=new Mytime(2001,7,30);
Mytime m2=new Mytime(2001,7,30);
System.out.println(m1==m2);
int a=80;
int b=80;
System.out.println(a==b);
System.out.println(m1.equals(m2));
}
}
class Mytime extends Object {
private int year;
private int month;
private int day;
public Mytime() {
}
public Mytime(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public boolean equals(Object obj){
if (obj instanceof Mytime){
Mytime m2=(Mytime)obj;
if (year==m2.year&&month==m2.month&&day==m2.day){
return true;
}
}
return false;
}
}
false
true
true
三.String类已重写equals和toString方法
public class Test03 {
public static void main(String[] args) {
String s1=new String("lupeng");
String s2=new String("lupeng");
System.out.println(s1.equals(s2));
System.out.println(s1);
System.out.println(s1.toString());
}
}
true
lupeng
lupeng
引用数据类型最好用equals方法特别是String类的,其他重写equals方法即可
public class Test05 {
public static void main(String[] args) {
User u1=new User("lupeng",new Address("wuhan","jiangxia","2001"));
User u2=new User("lupeng",new Address("wuhan","jiangxia","2001"));
User u3=new User("lupeg",new Address("wuhan","jiansffsadda","2001"));
System.out.println(u1.equals(u2));
System.out.println(u1.equals(u3));
}
}
class User{
String name;
Address addr;
public User(){
}
public User(String name,Address addr){
this.name=name;
this.addr=addr;
}
public boolean equals(Object obj){
if(obj==null||!(obj instanceof User)) return false;
if(this==obj) return true;
User u=(User)obj;
if(this.name.equals(u.name)&&this.addr.equals(u.addr)){
return true;
}
return false;
}
}
class Address{
String city;
String street;
String zipcode;
public Address() {
}
public Address(String city, String street, String zipcode) {
this.city = city;
this.street = street;
this.zipcode = zipcode;
}
public boolean equals(Object obj){
if (obj==null||!(obj instanceof Address)) return false;
if (this==obj) return true;
Address addr=(Address)obj;
if (this.city.equals(addr.city)&&this.street.equals(addr.street)&&this.zipcode.equals(addr.zipcode)){
return true;
}
return false;
}
}
true
false
看懂便可完全领悟
四.finalize()方法
①这个方法不需要程序员手动调用JVM的垃圾回收器负责调用,这个方法不像 equals tostring, equals和 tostring()方法是需要你写代码调用的
fina1ize()只需要重写,重写完将来自动会有程序来调用
②finalize()方法实际上是SUN公司为java程序员准备的一个时机,垃圾销毁时机如果希望在对象销毁时机执行一段代码的话,这段代码要写到 finalize()方法当中,与静态代码块类似
protected void finalize() throws Throwable { }
重写
public class FinalizeTest01 {
public static void main(String[] args) {
for (int i=0;i<1000;i++) {
Person p = new Person();
p=null;
if(i%2==0)
System.gc();
}
}
}
class Person{
public void finalize() throws Throwable {
System.out.println(this+"Person.finalize()");
}
}
Person@69195cfaPerson.finalize()
Person@3b287fd2Person.finalize()
Person@38249fdcPerson.finalize()
Person@7bdfbdd7Person.finalize()
Person@5802c0daPerson.finalize()
Person@202cf2e7Person.finalize()
Person@3c6a4341Person.finalize()
Person@3add701ePerson.finalize()