Java 面向对象 之 Object 类

<a href="http://www.verejava.com/?id=16992832835152">http://www.verejava.com/?id=16992832835152</a>

1/** 2 知识点: Object 类 3 4 注意: 5 1. Object 是所有类的根类, 也就是说所有类默认都继承Object 只是省略没有写 6 2. 当 println()输出一个对象的时候 自动调用了 Object的 toString() 方法 7 3. Object toString() 方法返回的是 : 类名+@+16进制的哈希码hashCode地址 8 哈希码 代表对象的内存地址,所以如果比较两个对象的引用相等==必须 哈希码相等 9 4. Object equals(Object obj) 方法比较的也是引用内部是以==实现的 10 5. String 对象的equals()方法怎么是比较内容了? 11 6. 如果要equals()比较两个对象的内容相等我们需要复写equals 方法 12 7. 当Object的子类复写了 toString() 方法, println()输出时调用子类复写的 toString() 方法 13 8. 如果复写了equals()方法,为了保证对象相等哈希码hashcode必须相等的特性, 14 我们也要复写hashCode()方法使保持对象的一致性 15 9. Object 可以接收任意引用数据类型(类,数组,接口) 16 17 回顾知识点: 18 1. 如果一个类定义了带参数的构造方法, 当用不带参数的构造方法调用时,java 19 虚拟机不会自动生成默认的构造方法,必须得手动显示定义不带参数的默认构造方法。 20*/ 21public class TestObject 22{ 23 public static void main(String[] args) 24 { 25 //实例化一个 Student 26 Object p=new Student("李明"); 27 Student p2=(Student)p; 28 Student p3=new Student("张涛"); 29 Student p4=new Student("李明"); 30 System.out.println(p); 31 System.out.println(p==p2); 32 System.out.println(p3); 33 System.out.println(p.equals(p3)); 34 System.out.println(p==p4); 35 System.out.println(p.equals(p4)); 36 System.out.println(p); 37 System.out.println(p4); 38 39 Person p5=new Student("李明"); 40 System.out.println(p4.equals(p5)); 41 42 String[] p6=new String[1]; 43 p6[0]="李明"; 44 System.out.println(p4.equals(p6)); 45 } 46} 47interface Person 48{ 49 /** 50 什么人可以说话 51 */ 52 public void say(); 53} 54class Student extends Object implements Person 55{ 56 private String name; 57 58 public Student() 59 { 60 61 } 62 63 public Student(String name) 64 { 65 this.name=name; 66 } 67 68 public String getName() 69 { 70 return this.name; 71 } 72 73 public void say() 74 { 75 System.out.println("学生会说英语"); 76 } 77 78 79 public boolean equals(Object obj) 80 { 81 //如果地址都相等, 两个对象肯定相等 82 if(this==obj) 83 { 84 return true; 85 } 86 //判断是否是 Student 对象实例 87 if(obj instanceof Student) 88 { 89 //将obj向下转型成 Student 90 Student p=(Student)obj; 91 //如果内容相等返回true 92 if(this.name.equals(p.getName())) 93 { 94 return true; 95 } 96 } 97 return false; 98 } 99 100 public int hashCode() 101 { 102 return this.name.hashCode(); 103 } 104 105 public String toString() 106 { 107 return this.name; 108 } 109 110} 111

<a href="http://www.verejava.com/?id=16992832835152">http://www.verejava.com/?id=16992832835152</a>

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Guava库学习:Guava中Objects实用工具类的学习

    链接地址:http://www.xx566.com/detail/128.html(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.xx566.com%2Fdetail%2F128.html)  Java中的Object类是所有Java类的超类(也就是祖先),所

4. Nginx模块

Nginx官方模块1.ngx\_http\_stub\_status\_modulehttp://nginx.org/en/docs/http/ngx\_http\_stub\_status\_module.html。(https://www.oschina.net/action/GoToLink?urlhttp%3A%2

P2P技术揭秘.P2P网络技术原理与典型系统开发

Modular.Java(2009.06)\.Craig.Walls.文字版.pdf:http://www.t00y.com/file/59501950(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.t00y.com%2Ffile%2F59501950)\More.E

Uber基于RNN的极端事件预测,解决交通问题

时间 2017061212:00:15  亿欧网(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.tuicool.com%2Fsites%2FAJj2Un)_原文_  http://www.iyiou.com/p/47628(https://www.oschina.n

Cubieboard 3(cubietruck) 安装 java 环境

Cubieboard3 刷机过程 请看 http://name5566.com/4398.html(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fname5566.com%2F4398.html)安装java环境下载 armjdkh