任何一个类默认继承Object。如果就算没有直接继承,最终也会间接继承。
查找Object常用方法:
一、在JDK包下查找源代码进行查阅。
二、查阅java的类库的帮助文档。
注:帮助文档下载:https://www.oracle.com/java/technologies/downloads中选择相应的版本Docunmentation Download按钮下载
API:应用程序编程接口。每个JDK的类库就是一个javase的API,每个API会配置一套API帮助文档。
常用的几个方法
protected Object clone() //负责对象克隆
int hashCode() //获取对象哈希值的一个方法
boolean equals(Object obj) //判断两个对象是否相等
String toString() //将对象转换成字符串形式
protected void finalize() //垃圾回收器负责调用的方法
public String to String(){
return this.getClass().getName() + "@" + Integer.toHexString(hashCode());
}
public class Test {
public static void main(String[] args) {
MyTime t1 = new MyTime(2001,6,1);
System.out.println(t1.toString()); //MyTime{year=2001, month=6, day=1}
//在没有重写toString()方法,输出打印为test12.MyTime@4554617c
//@后面带的是16进制的地址。
}
}
class MyTime{
int year;
int month;
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 "MyTime{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
}
public boolean equals(Object obj){
return (this == obj);
}
public class test12 {
public static void main(String[] args) {
MyTime t1 = new MyTime(2001,6,21);
MyTime t2 = new MyTime(2001,6,21);
MyTime t3 = new MyTime(2001,8,21);
System.out.println(t1.equals(t2)); //true
//如果不重写,t1==t2判断为两个对象地址,结果为false。
System.out.println(t1.equals(t3)); //false
}
}
class MyTime{
int year;
int month;
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对象,再执行
MyTime my = (MyTime) obj;
if (this.year == my.year && this.month == my.month && this.day == my.day) {
return true;
} else {
return false;
}
}else {
return false;
}
}
}
public class Test {
public static void main(String[] args) {
String s1 = new String("ice");
String s2 = new String("coffee");
System.out.println(s1==s2); //这里" == "是比较s1和s2的引用地址,所以输出为false
System.out.println(s1.equals(s2)); //String类已经重写了equals方法,输出为true
System.out.println(s1.toString()); //String类已经重写了toString()方法,输出为ice
System.out.println(s2); //默认输出为String的字符串,输出为coffee
}
}
综上:
java中基本数据类型使用" == "来判断是否相等。
java中所有的引用数据类型统一使用equals()方法来判断是否相等。(注意:String类型是引用数据类型)
public class Test {
public static void main(String[] args) {
Student s1 = new Student(111,"中大南方");
Student s2 = new Student(111,"中大南方");
System.out.println(s1 == s2); //这里" == "是比较s1和s2的引用地址,所以输出为:false
System.out.println(s1.equals(s2)); //按照重写的equals方法,输出为:true
System.out.println(s1.toString()); //按照重写的toString方法,输出为:学号:111学校中大南方
}
}
class Student{
//学号
int no;
//所在学校
String school;
public Student(){}
public Student(int no,String school){
this.no = no;
this.school = school;
}
//重写toString方法
public String toString(){
return "学号:"+no + "学校" + school;
}
//重写equals方法
public boolean equals(Object obj){
if(obj==null || !(obj instanceof Student) ){
return false;
}else{
Student s = (Student) obj;
return (this.no == s.no && this.school.equals(s.school));
}
}
}
需求:有一个用户类,有用户名属性和地址属性,地址属性是一个类。
重写相应的equals()方法。
public class test14 {
public static void main(String[] args) {
User u1 = new User("zhangsan",new Address("ice","coffee","1111"));
User u2 = new User("zhangsan",new Address("ice","coffee","1111"));
User u3 = new User("zhangan",new Address("ice","coffee","1111"));
User u4 = new User("zhangsan",new Address("ice","coffee2","1111"));
System.out.println(u1.equals(u2)); //测试Address中equals()相等可用性:true
System.out.println(u1.equals(u3)); //测试User中equals()相等可用性:false
System.out.println(u1.equals(u4)); //测试Address中equals()不相等可用性:false
}
}
//一个用户类
class User{
//用户名
String name;
//住址
Address addr;
public User(){}
public User(String name, Address addr) {
this.name = name;
this.addr = addr;
}
//重写User中的equals()方法
public boolean equals(Object obj) {
if(obj == null || !(obj instanceof User)){
return false;
}else{
User u = (User)obj;
return (this.name.equals(u.name) && this.addr.equals(u.addr)); //这里还需要重写Address的equals()方法
}
}
}
//一个地址类
class Address{
//城市
String city;
//街道
String street;
//邮编
String zipoode;
public Address(){}
public Address(String city,String street,String zipoode){
this.city = city;
this.street =street;
this.zipoode = zipoode;
}
//重写Address中的equals()方法
public boolean equals(Object obj) {
if(obj==null || !(obj instanceof Address)){
return false;
}else{
Address a = (Address) obj;
return (this.city.equals(a.city) && this.street.equals(a.street) && this.zipoode.equals(a.zipoode));
}
}
}
protected void finalize() throws Throwable{ }
public native int hashCode();
//native关键字为底层调用C++程序
public class test15 {
public static void main(String[] args) {
Object o = new Object();
int hashCodeValue = o.hashCode();
System.out.println(hashCodeValue);//1163157884,这串数字就是o对象所在的内存地址的一个int值
}
}
——本章节为个人学习笔记。学习视频为动力节点Java零基础教程视频:动力节点—JAVA零基础教程视频