Java学习笔记——Java语言基础(十一)(Object类)
程序员文章站
2024-03-14 09:04:22
...
一、Object类
1.1 API
1.API(Application Programming Interface)应用程序编程接口
2.Java API:Java提供的可以直接使用的类,这些类将底层的实现封装起来。
1.2 Object
1.Object类:类层次结构的根类,所有类都直接或者间接的继承Object类
2.构造方法:public Object() 子类的构造方法默认访问的是父类的无参构造方法
1.3 Object的hashCode()方法
1.返回对象的哈希吗值,不同对象的hashCode()一般来说不会相同
1.4 Object类的getClass()方法
1.通过getClass()方法,获取对象的真实类的全名称
hashCode()和getClass()的代码如下:
public class Test03 {
public static void main(String[] args) {
//Object的hashCode()方法
Object obj1 = new Object();
System.out.println(obj1);//aaa@qq.com
int o1 = obj1.hashCode();
System.out.println(o1);//356573597
Object obj2 = new Object();
System.out.println(obj2);//aaa@qq.com
int o2 = obj2.hashCode();
System.out.println(o2);//1735600054
//Object的getClass()方法
Object obj3 = new Object();
System.out.println(obj3);//aaa@qq.com
Object obj4 = new Object();
System.out.println(obj4);//aaa@qq.com
//比较地址值
System.out.println(obj3==obj4);//false
Class C3 = obj3.getClass();
Class C4 = obj4.getClass();
//比较类名
System.out.println(C3);//class java.lang.Object
System.out.println(C4);//class java.lang.Object
System.out.println(C3==C4);//true
}
}
1.5 Object类的toString()方法
1.返回该对象的字符串
public class Test04 {
public static void main(String[] args) {
Object obj = new Object();
String s = obj.toString();
//打印地址值
System.out.println(s);//aaa@qq.com
/*没有重写toString方法
Student student1 = new Student();
String s1 = student1.toString();
System.out.println(s1);//aaa@qq.com
*/
//重写toString方法
Student student = new Student("张三", 23);
String s1 = student.toString();
System.out.println(s1);//Student{name='张三', age=23}
}
}
class Student{
String name;
int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
//重写toString方法
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
1.6 Object类的equals()方法
1.指示其他某个对象是否与调用方法的对象相等
2.默认情况下比较的是对象的引用是否相同
3.由于比较引用没有意义,一般建议重写equal()方法。
public class Test05 {
public static void main(String[] args) {
//Object类的equals方法
Object obj1 = new Object();
Object obj2 = new Object();
System.out.println(obj1.equals(obj2));//false
System.out.println(obj1==obj2);//fasle
//Student1重写equals方法
Student1 s1 = new Student1("张三", 23);
Student1 s2 = new Student1("张三", 24);
Student1 s3 = new Student1("张三", 23);
boolean b1 = s1.equals(s2);
boolean b2 = s1.equals(s3);
System.out.println(b1);//false
System.out.println(b2);//true
}
}
class Student1{
String name;
int age;
public Student1() {
}
public Student1(String name, int age) {
this.name = name;
this.age = age;
}
//重写eauals方法,比较的是Student里面的成员变量是否相等
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student1 student1 = (Student1) o;
return age == student1.age &&
Objects.equals(name, student1.name);
}
}
1.7 Object类的clone()方法
1.clone()的权限修饰符是受保护的,在用的时候,让该类重写该方法,并把该方法的权限修饰符改为public
2.对象的浅克隆:浅克隆和深克隆,使用clone方法的克隆为浅克隆
3.如果一个对象需要调用clone方法进行克隆,那么该对象所属的类必须实现Cloneable接口。
4.Cloneable接口并没有任何的抽象方法,这种没有任何抽象的接口时一种标记接口
public class Test01 {
public static void main(String[] args) throws CloneNotSupportedException {
Student s1 = new Student("学生A", 23);
System.out.println(s1);//Student{name='学生', age=23}
//克隆一个Object类
//CloneNotSupportedException抛出异常
Object obj = s1.clone();
//向下转型
Student s2= (Student) obj;
s2.age=30;
s2.name="学生B";
System.out.println(s2);//Student{name='学生B', age=30}
}
}
class Student implements Cloneable{
String name;
int age;
//重写toString方法
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
当一个方法的参数包含引用类型的时候,clone方法的使用如下:
public class Test02 {
public static void main(String[] args) throws CloneNotSupportedException {
DogFood dogfood = new DogFood("骨头");
Dog dog1 = new Dog("一号狗", 3, dogfood);
dog1.dogFood.name="火腿";
System.out.println(dog1.dogFood.name);//火腿
//克隆Dog的dog1对象
Dog dog2 = (Dog) dog1.clone();
dog2.dogFood.name="狗粮";
System.out.println(dog2.dogFood.name);//狗粮
System.out.println(dog1.dogFood.name);//狗粮
}
}
class DogFood{
String name;
public DogFood(String name){
this.name=name;
}
}
class Dog implements Cloneable {
String name;
int age;
DogFood dogFood;
public Dog(String name, int age, DogFood dogFood) {
this.name = name;
this.age = age;
this.dogFood = dogFood;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
内存图