Object类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Object类相关的知识,希望对你有一定的参考价值。
1 Object类的概述
- 类Object是类层次结构的根类。每个类都使用
Object
作为超类。所有对象(包括数组)都实现这个类的方法。 - Object类是不断抽取而来的。
2 equals()方法
public boolean equals(Object obj)
public boolean equals(Object obj) { return (this == obj); }
- 示例:调用Object类的equals()方法
package java011; /** * 2017/9/18 * 说明: */ public class Person { private String name; private int age; public Person(){ } public Person(String name,int age){ this.name = name; this.age = age; } }
package java011; /** * 2017/9/18 * 说明: */ public class Test { public static void main(String[] args) { Person p = new Person("张三",15); Person p2 = new Person("张三",15); System.out.println(p == p2);//false System.out.println(p.equals(p2));//false } }
- 示例:重写equals()方法
package java011; /** * 2017/9/18 * 说明: */ public class Person { private String name; private int age; public Person(){ } public Person(String name,int age){ this.name = name; this.age = age; } @Override public boolean equals(Object obj) { if(obj == null){ return false; } if(obj instanceof Person){ Person p = (Person) obj; if(!this.name.equals(p.name) && this.age!=p.age ){ return false; } } return true; } }
package java011; /** * 2017/9/18 * 说明: */ public class Test { public static void main(String[] args) { Person p = new Person("张三",15); Person p2 = new Person("张三",15); System.out.println(p == p2);//false System.out.println(p.equals(p2));//true } }
3 hashcode()方法
public int hashCode()
public native int hashCode();
package java011; /** * 2017/9/18 * 说明: */ public class Person { private String name; private int age; public Person(){ } public Person(String name,int age){ this.name = name; this.age = age; } @Override public boolean equals(Object obj) { if(obj == null){ return false; } if(obj instanceof Person){ Person p = (Person) obj; if(!this.name.equals(p.name) && this.age!=p.age ){ return false; } } return true; } }
package java011; /** * 2017/9/18 * 说明: */ public class Test { public static void main(String[] args) { Person p = new Person("张三",15); Person p2 = new Person("张三",15); System.out.println(p);//[email protected] System.out.println(p.hashCode());//460141958 System.out.println(Integer.toHexString(p.hashCode()));//1b6d3586 } }
4 getClass()方法
public final Class<?> getClass()
public final native Class<?> getClass();
- 示例:
package java011; /** * 2017/9/18 * 说明: */ public class Person { private String name; private int age; public Person(){ } public Person(String name,int age){ this.name = name; this.age = age; } @Override public boolean equals(Object obj) { if(obj == null){ return false; } if(obj instanceof Person){ Person p = (Person) obj; if(!this.name.equals(p.name) && this.age!=p.age ){ return false; } } return true; } }
package java011; /** * 2017/9/18 * 说明: */ public class Test { public static void main(String[] args) { Person p = new Person("张三",15); Class<?> clazz = p.getClass(); System.out.println(clazz);//class java011.Person System.out.println(clazz.getName());//java011.Person } }
5 toString()方法
public String toString()
public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }
- 示例:
package java011; /** * 2017/9/18 * 说明: */ public class Person { private String name; private int age; public Person(){ } public Person(String name,int age){ this.name = name; this.age = age; } @Override public boolean equals(Object obj) { if(obj == null){ return false; } if(obj instanceof Person){ Person p = (Person) obj; if(!this.name.equals(p.name) && this.age!=p.age ){ return false; } } return true; } }
package java011; /** * 2017/9/18 * 说明: */ public class Test { public static void main(String[] args) { Person p = new Person("张三",15); System.out.println(p);//[email protected] System.out.println(p.toString());//[email protected] } }
以上是关于Object类的主要内容,如果未能解决你的问题,请参考以下文章
如何通过单击片段内的线性布局从片段类开始新活动?下面是我的代码,但这不起作用