基本作用:
objiect类是所有类的父类。
任何一个类定义的时候如果没有明确定义了父类的话,默认父类是Object类。
class A extends Object{}
在整个java里面,类的继承关系一直存在的。例如:
String 类
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAf4AAABJCAIAAABTtdMLAAAKXElEQVR4nO2dPW7qQBDHuVMUKYegQBFXSBVZyBdwmwqQKCJxASQa3JGamjIu0qThIvsKe9ezuzNrLzFfb/8/veJlMeP9/Hs8aw8jBQAAIDFGt64AAACAawPpBwCA5ID0AwBAckD6AQAgOSD9AACQHJB+AABIDkg/AAAkB6QfAACSA9IPAADJAekHAIDkgPQDAEByQPoBACA5IP0AAJAckH4AAEgOSD8AACQHpB8AAJID0g8AAMkB6QcAgOSA9AMAQHJA+gEAIDkg/QAAkBzR0v+9nDy/TBYV91mZPb88Pb88vS6Zj7+XE7b8rAo81f9m5aD2g/W/gZ1oqsW07pnJopy/Tuff+oPv5eQ538WY2s1uUH8AwJU4x+svc0H6m4+zS0rGbkYU7SIMVf/L9gN3wvzpuemcajE1/9cfxUk/AOB/ZlTm2kl80Q5jrVdlZtzq2tE2Qlbmk0W1mznHG3zJk73g1oWfzhfhK4rt79df+Q7Yr+u/NO3K2juEyhRyjm2MZIfq79uRz1vmulF5NnMkmyvnqBZTVt/NSBlTO6s83+mzNPVvG0WsNYX5Tn9KGluRybPM+FkBALgnRqrRF6OMu5n5f5m1EZVqPmuln65tcrz+Fi+dXrkVhSizfnohe/38JadtS/tptSv1f8vcihqF6u/RUX9G+oXzkiO/lxN6SePLJfQl0O9JyevXop+VXlDI/0qZPz1PJ/U0aOtTzV/bK1l9OcmcHgUA3BuN9FvyZxRfln7+eP1nP+mvFvkZoZs46Wfr7zjC50p/V/0ZO9J5TYze0U2pPFgr85UeAR/7qu99xEg/vdPSFww6IvRyCwC4V2rpd93VLukXjtd/3rf0V4spqb9TedYOT6z0d51X1/OVbaBULtZu/kp8/5D0C3sAvPRbdzmQfgAeFR3wsTxEP0hiPe9hH/+ngA8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qM17R65G219oWbnvMCnyJ4yj0p9Teu3rmqj8jOypiYiM66pLvml9rWVbNRmupynyyWGbmePKDi+KvfXl5mEP2uePPSSIdmfyZpHXztvu4ZM7O8WYqu+X+Np3JJq29V/Z40U6718TtUDn25eTVcj3l5NXfy0nU9qCQ7Nq/8dclq2Md9jWbfsVB7Ve0/kTC+vVne97L94+GCmtoXnFJvyPnlW6U3iahXdT289tqoyWsXqf8IA4wvoH6xI1X7Hq02ms/DEr28O3tbsZOqD/F5O1d8+EeOefhTjfgA5KhR0BjeDoevgR96PeI6k3G9zI8ahzmakRLf+tKnx8lBA+K9zTX5ZGejQF9iOy9G4zvJfB/uAn4IJEDAAAkB6QfAACSA9IPAADJAekHAIDkgPQDAEByQPoBACA5IP0AAJAckH4AAEgOSD8AACQHpB8AAJID0g8AAMnxD8JnIJ1RajkaAAAAAElFTkSuQmCC" alt="" />
这时候一个好处就出现了,利用Object类可以接受一切类。
Object obj = new Book() ;//向上转型
Obect obj = “Hello” ; //向上转型
如果不知道参数类型的时候,使用Object是最好的选择
http://docs.oracle.com/javase/7/docs/api/
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkQAAADJCAIAAABNM7oiAAATaUlEQVR4nO3c3VMUZ6LH8f1zIi8DHEQBT9XW3u2FdVK1ImepvUtVLnbdK92SRHGCStDEOghKFKNRIdGqpMQT1hKzm8KXocTBTK1Z96wREAN6fENAzNYaU3vx7EW/PU/30zM9Az3y4PdTv0o13T39RlX/fLqH/EzE4crp5JXQhYMHTg8qK38z6Hzq7c/vWZO3Pz99ZFIIIcTkhbc32etrZwIA8LNYtppfmZ1ev2mPFbfMxOSFt52Z3qa0MwEAr714ymzyQtKtpQC1zL5JumMs/ae+SSYv3I40EwDwmoqnzIQYPHDIfiQoxO3PD8kDKX+ZHfjGme+OzO4dSbrruL2lnQkAQGxlJsQ3SeeR4Hq7ru4dSe5xnyiu32S33e3PD9nPGA+cftt+fqis6RShdiYAADGWGQAARUKZAQCMR5kBAI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69+eyr0R0IIIUWKoWWmtMv13m6vzC70WcO1dy+4K3tjOGfms3lnNWWmsnJ317eBIWDLwHX5SK61v/n7U/Kxjez5rzcZnBFCSNGzwspsz/qO1Py8XUJSSz2znjTacy70rXe34E3LAz55F6EjM011BeqNEEJIEbLCysybf7232y42O16ZnemQ37ftWW912IU+/8ArZHduTv2++nen1JnX2t/8VfvIq/6lEkLI65YVVmbeO7McZRbsLcqMEEKMjaFlFvYFEPk5YfbHjMpLtXcv+D9ypsPdlNKC8n55zEgIIcskJpZZtq/md/V2e08OlU9JZTavfAHEG5DJm5VHae7KEb4A4h+rEUIIiT9mllkhyfrnYgWGr+YTQsiyyEovM2mwVegfTWcNfzRNCCHLICu9zAghhLwGocwIIYQYH8qMEEKI8aHMCCGEGB/KjBBCiPFZ7mVGCCGERMnyLTMAAIqAMgMAGI8yAwAYjzIDABiPMgMAGI8yAwAYjzIDABiPMgMAGI8yAwAYjzIDABiPMgMAGI8yAwAYjzIDABiPMgMAGI8yAwAYjzIDABiPMgMAGG+xZUYIIYQshxReZgAAmIIyAwAYjzIDABiPMgMAGI8yAwAYjzIDABiPMgMAGI8yAwAYjzIDABiPMgMAGI8yAwAYjzIDABiPMgMAGI8yAwAYjzIDABiPMgMAGI8yAwAYjzIDABiPMgMAGI8yAwAYjzIDABiPMgMAGI8yAwAY79/+C2JoI8jCoQAAAABJRU5ErkJggg==" alt="" />
可以发现在Object类里面定义一个无参构造方法。
这是为什么呢??
因为Object类是所有类的父类,当对象实例化的时候,一定会默认调用父类的无参构造方法。
他还提供了很多的方法。这些方法很重要。
从严格意义上来讲(一般不遵守) ,任何一个简单Java类都应该覆写Object类的三个方法
1.toString()
2.equals(Object);对象比较方法
3.public int hashCode()
第一个方法:取得对象信息
toString() :
范例:
class Book extends Object {}
public class ObjectTest{
public static void main(String arg[]){
Book b = new Book() ;
String s = "hello" ;
System.out.println(b) ;
System.out.println(b.toString());
System.out.println(s) ;
}
}
观察发现输出的结果第一行和第二行一样。
这是因为输出对象的时候,会自动调用toString() 方法将对象变成字符串输出;
所以,以后在使用的时候要覆写 Object类 的toString() 方法;
class Book {
private String title ;
private double price;
public Book(String title ,double price){
this.title = title ;
this.price = price ;
}
public String toString(){
return "title" + this.title + " , price :" + this.price ;
}
}
public class ObjectTest{
public static void main(String arg[]){
Book b = new Book("Java",19.9) ;
String s = "hello" ;
System.out.println(b) ;
System.out.println(b.toString());
System.out.println(s) ;
}
}
此时就会重写了toString()方法。就可以直接输出对象。
第二个:对象比较
equalls() ;
public boolean equals(Object obj){
if(this == obj)
return true ;
if(obj == null)
return false ;
if(obj instanceof Book) //判断是否是Book类型的实例化
return false ;
Book book = (Book) obj ;
if(this.title.equals(book.title) && this.price == book.price)
return true ;
}