这篇文章主要介绍Java中Object是什么意思,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
public class TestObject {public static void main(String[] args) {//实例化一个 StudentObject p = new Student("李明");Student p2 = (Student) p;Student p3 = new Student("张涛");Student p4 = new Student("李明");System.out.println(p);System.out.println(p == p2);System.out.println(p3);System.out.println(p.equals(p3));System.out.println(p == p4);System.out.println(p.equals(p4));System.out.println(p);System.out.println(p4);Person p5 = new Student("李明");System.out.println(p4.equals(p5));String[] p6 = new String[1];p6[0] = "李明";System.out.println(p4.equals(p6));}}interface Person {//什么人可以说话public void say();}class Student extends Object implements Person {private String name;public Student() {}public Student(String name) {this.name = name;}public String getName() {return this.name;}public void say() {System.out.println("学生会说英语");}public boolean equals(Object obj) {//如果地址都相等, 两个对象肯定相等if (this == obj) {return true;}//判断是否是 Student 对象实例if (obj instanceof Student) {//将obj向下转型成 StudentStudent p = (Student) obj;//如果内容相等返回trueif (this.name.equals(p.getName())) {return true;}}return false;}public int hashCode() {return this.name.hashCode();}public String toString() {return this.name;}}
以上是“Java中Object是什么意思”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!