public final class> getClass(){ }
代码示例
public class Test {
public static void main(String[] args) {
Student jordan = new Student("jordan", 182);
Student susan = new Student("susan", 165);
Syytem.out.println(jordan.getClass());
Class class1 = jordan.getClass();
Class class2 = susan.getClass();
if (class1 == class2){
System.out.println("jordan和susan为同一类型");
}else System.out.println("jordan和susan不为同一类型");
}
}
//运行结果
class com.cclass.DemoGetclass.Student
jordan和susan为同一类型
Process finished with exit code 0
public int hashCode(){ }
代码示例
public class Test {
public static void main(String[] args) {
Student jordan = new Student("jordan", 182);
Student susan = new Student("susan", 165);
System.out.println(jordan.hashCode());
System.out.println(susan.hashCode());
}
}
//运行结果
21685669
2133927002
Process finished with exit code 0
public String toString(){ }
代码示例
public class Test {
public static void main(String[] args) {
Student jordan = new Student("jordan", 182);
Student susan = new Student("susan", 165);
System.out.println(jordan.toString());
System.out.println(susan.toString());
}
}
//运行结果
com.cclass.DemoGetclass.Student@14ae5a5
com.cclass.DemoGetclass.Student@7f31245a //16进制
Process finished with exit code 0
重写示例
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", height=" + height +
'}';
}
//运行结果
Student{name='jordan', height=182}
Student{name='susan', height=165}
Process finished with exit code 0
public boolean equals(Object obj){ }
代码示例
public class Test {
public static void main(String[] args) {
Student jordan = new Student("jordan", 182);
Student susan = new Student("susan", 165);
System.out.println(jordan.equals(susan));
}
}
//运行结果
false
Process finished with exit code 0
重写覆盖示例
@Override
public boolean equals(Object obj) {
if (this == obj){
return true;
}
if (this == null){
return false;
}
if (obj instanceof Student){
//强制转换 将object转为Student 大--->小
Student s = (Student) obj;
if (this.name.equals(s.getName()) && this.height == s.getHeight()){
return true;
}
}
return false;
}
//测试类如下
public class Test {
public static void main(String[] args) {
Student jordan = new Student("jordan", 182);
Student susan = new Student("susan", 165);
System.out.println(jordan.equals(susan));
Student susan1 = new Student("susan", 165);
System.out.println(susan1.equals(susan));
}
}
//输出结果
false
true
Process finished with exit code 0
代码示例
//重写finalize
@Override
protected void finalize() throws Throwable {
System.out.println(this.name+"被垃圾回收了");
}
//测试类
public class Test {
public static void main(String[] args) {
new Student("aaa",19);
new Student("bbb",19);
new Student("ccc",19);
System.gc();
}
}
//运行结果
ccc被垃圾回收了
bbb被垃圾回收了
aaa被垃圾回收了
Process finished with exit code 0
//Student类
package com.cclass.DemoGetclass;
public class Student {
private String name;
private int height;
public Student(String name, int height) {
this.name = name;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", height=" + height +
'}';
}
@Override
public boolean equals(Object obj) {
if (this == obj){
return true;
}
if (this == null){
return false;
}
if (obj instanceof Student){
//强制转换 将object转为Student 大--->小
Student s = (Student) obj;
if (this.name.equals(s.getName()) && this.height == s.getHeight()){
return true;
}
}
return false;
}
@Override
protected void finalize() throws Throwable {
System.out.println(this.name+"被垃圾回收了");
}
}
//Test 类
package com.cclass.DemoGetclass;
public class Test {
public static void main(String[] args) {
Student jordan = new Student("jordan", 182);
Student susan = new Student("susan", 165);
Class class1 = jordan.getClass(); //.getClass() 用于判断两个类是否为同一类型
Class class2 = susan.getClass();
if (class1 == class2){
System.out.println("jordan和susan为同一类型");
}else System.out.println("jordan和susan不为同一类型");
System.out.println("========================");
System.out.println(jordan.hashCode()); //.hashCode() 返回该对象的哈希码值
System.out.println(susan.hashCode());
System.out.println("========================");
System.out.println(jordan.toString());
System.out.println(susan.toString());
System.out.println("========================");
System.out.println(jordan.equals(susan));
Student susan1 = new Student("susan", 165);
System.out.println(susan1.equals(susan));
System.out.println("========================");
new Student("aaa",19);
new Student("bbb",19);
new Student("ccc",19);
System.gc();
}
}
//运行结果
jordan和susan为同一类型
========================
21685669
2133927002
========================
Student{name='jordan', height=182}
Student{name='susan', height=165}
========================
false
true
========================
ccc被垃圾回收了
bbb被垃圾回收了
aaa被垃圾回收了
Process finished with exit code 0