一、equals==
public class equals {
//把光标放在equals方法,直接输入ctrl+b--查看jdk源码
public static void main(String[] args) {
Person person1 =new Person("jack",18,'男');
//System.out.println(person1.getAge());
Person person2 =new Person("jack",18,'男');
System.out.println(person1.equals(person2));//false->object的equals默认判断两个对象是否相等..改写之后false->true
//System.out.println(person1==person2);//false
//判断两个Person对象的内容是否相等,如果两个Person对象的各个属性值都一样,则返回true,反之false--equals的重写
}
}
class Person{//extends object
private String name;
private int age;
private char gender;
//重写object的equals方法
public boolean equals(Object obj) {
//判断如果比较的两个对象是同一个对象,则直接返回true
if (this == obj) {
return true;
}
if (obj instanceof Person){
//向下转型
Person p = (Person)obj;
return this.name.equals(p.name) && this.age==p.age && this.gender==p.gender;
}
return false;
}
public Person(String name, int age, char gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
}
Person p1 = new Person();
p1.name = "hspedu";
Person p2 = new Person();
p2.name= "hspedu";
System.out.println(p1==p2);//false(==可以用在引用类型or基本数据类型,如果是引用类型判断的是两个对象是否指向同一个对象)
System.out.println(p1.name.equals(q2.name));//true(p1.name是字符串,字符串把equals方法重写(object的equal判断的是类是否相同),此时比较但是内容)
System.out.println(p1.equals(p2));//false(此时没有重写,这里的equals就是object的equals,判断的是两个对象是否相等)
String s1 = new String("asdf");
String s2 = new String("asdf");
System.out.println(s1.equals(s2));//true(此时为字符串,equals被重写,此时判断的是内容)
System.out.println(s1==s2);//false(String是引用类型,判断的是是否指向同一个地址)
int it = 65;
float fl = 65.of;
System.out.println("65和'A'是否相等?"+(it == fl));//true(==判断的类型为数值时是在判断内容)
char ch1 = 'A';
char ch2 = 12;
System.out.println("65和'A'是否相等?"+(it == ch1));//true(ASCII编码)
System.out.println("12和ch2是否相等?"+(12 == ch2));//true
String str1 = new String("hello");
String str2 = new String("hello");
System.out.println("str1和str2是否相等?"+(str1 == str2));//false
System.out.println("str1是否equals str2?"+(str1.equals(str2)));//true
System.out.println("hello"==new java.sql.Date());//false,文件的类型不相同
二、hashcode
public class equals {
public static void main(String[] args) {
AA aa = new AA();
AA aa2 = new AA();
AA aa3 = aa2;
//指向不同的对象,hashcode不同
System.out.println(aa.hashCode());//460141958
System.out.println(aa2.hashCode());//1163157884
//指向相同的对象,hashcode一样
System.out.println(aa3.hashCode());//1163157884
}
}
class AA{
}
三、toString
public class toString {
public static void main(String[] args) {
Monster monster =new Monster("kity","cooking",1800);
System.out.println(monster.toString());//example.Monster@1b6d3586
}
}
class Monster {
private String name;
private String job;
private double salary;
public Monster(String name, String job, double salary) {
this.name = name;
this.job = job;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
//重写toString方法,打印对象或拼接对象
//快捷键即可,zlt+insert->toString
@Override
public String toString() {
return "Monster{" +
"name='" + name + '\'' +
", job='" + job + '\'' +
", salary=" + salary +
'}';
}
}
三、finalize
public class finalize {
public static void main(String[] args) {
Car car = new Car("宝马");
car = null;//car对象是一个垃圾,垃圾回收器就会回收(销毁)对象,在销毁对象前,会调用该对象的finalize方法
//可以在finalize中写自己的业务逻辑代码(比如释放资源)
}
}
class Car {
private String name;
public Car(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
protected void finalize() throws Throwable {
//super.finalize();
//重写
System.out.println("我们销毁了"+name);
}
}
标签:JAVA,name,子类,equals,println,out,public,原码,String From: https://www.cnblogs.com/maxzz/p/16897601.html