JAVA常用类(1)
student类:
package com.tiedandan.java常用类.Object类.getclassmethod;
public class Student {
private int age =20;
private String name ="张铁蛋";
public Student() {
}
public Student(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void print(){
System.out.println("");
}
Appliaction测试类:
package com.tiedandan.java常用类.Object类.getclassmethod;
public class Application {
public static void main(String[] args) {
Student stu1 = new Student(1,"铁蛋蛋1");
Student stu2 = new Student(2,"铁蛋蛋2");
System.out.println("名字"+stu1.getName()+"年龄"+stu1.getAge());
System.out.println("名字"+stu2.getName()+"年龄"+stu2.getAge());
//用getclass方法判断stu1和stu2类型是否相同
Class class1 = stu1.getClass();
Class class2 = stu2.getClass();
if (class1==class2){
System.out.println("stu1和stu2类型相等");
}else {
System.out.println("stu1和stu2类型不相等");
}
System.out.println("==============================");
//用hashcode()判断两个对象地址是否一致
System.out.println(stu1.hashCode());
System.out.println(stu2.hashCode());
Student stu3 = stu1;
System.out.println(stu3.hashCode());
System.out.println("-------------------------");
//tostring方法,在Student类中重写toString方法
System.out.println(stu3.toString());
System.out.println("-----------equals方法----------");
//equals方法在object类中用于对比两个对象地址是否相同
//可以进行重写,用于比较两对象内容是否相同。
System.out.println("--------------finalize垃圾回收方法");
// 对于new Student()的垃圾对象进行回收
//正常的Student stu1 = new Student(1,"铁蛋蛋1");无法回收
new Student(3,"铁蛋蛋3");
new Student(4,"铁蛋蛋4");
System.gc();
System.out.println("回收垃圾");
}
}