object:
object是所有类的祖先,即所有类都会直接或间接的继承object类;以下介绍object中的常见方法:
toString、equals、hashcode、clone、getClass、finalize
toString默认:返回字符串描述信息
equals默认:判断两对象地址是否相同
其他方法见以下链接:(这些方法经常被重写使用,以下代码有示例)
https://blog.csdn.net/lin49940/article/details/4677296
package wang;
class Student
{
int age;
String name;
Student(int age,String name)
{
this.age = age;
this.name = name;
}
void putS()
{
System.out.println(name+age+"岁了");
}
public String toString()//这里对toString进行了重写
{
return name+age+"岁了";
}
public boolean equals(Object arg0) {//这里对equals进行了重写
Student p = (Student)arg0;
if(this.age == p.age && this.name == p.name)
{
return true;
}
else
{
return false;
}
}
}
public class Test {
public static void main(String[] args) {
Student stu1 = new Student(17,"张良");
Student stu2 = new Student(17,"张良");
System.out.println(stu1.toString());
System.out.println(stu1.equals(stu2));
}
}