Java 从入门到进阶之路(十八)
在之前的文章我们介绍了一下 Java 中的正则表达式,本章我们来看一下 Java 中的 Object。
在日常生活中,任何事物我们都可以看做是一个对象,在编程中是同样的道理,在 Java 编程中其实更突出,因为 Java 就是一门面向对象的编程语言。
我们先来看下面的代码:
public class Main {
public static void main(String[] args) {
Person person = new Person();
// person.age = 1000; // 编译错误
// System.out.println(person.age); // 编译错误
person.setAge(1000);
System.out.println(person.getAge()); // person.setAge(10);
System.out.println(person.getAge()); // System.out.println(person.toString()); // Person@1b6d3586
}
} class Person {
private int age; public int getAge() {
return age;
} public void setAge(int age) {
if (age < 0 || age > 100) {
return;
}
this.age = age;
}
}
} public void setAge(int age) {
if (age < 0 || age > 100 ) {
return;
}
this.age = age;
}
}
在上面的代码中,我们定义了 get 和 set 方法来实现私有属性的获取和更改,起到了对私有属性的保护作用。
在上面的代码中,我们还写了一个 toString() 方法,但是我们并没有在 Person 类中定义该方法,这是因为当我们定义 Person 类的时候,系统会默认继承 Object 类,且 Object 类中有 toString() 方法,并且输出为一个 类名@地址,这个字符串没有什么实际意义。因此通常我们要使用一个类的 toString 方法时,就应当重写该方法,重写该方法后,返回的字符串没有严格的格式要求,将来可以根据需求而定,但是原则上该字符串应当包含当前对象的属性信息,只有当我们自定义的类需要重写该方法时,JAVA API 提供的类通常都已经重写了该方法。
下面我们将 toString() 方法进行重写:
public class Main {
public static void main(String[] args) {
Point point = new Point(1, 2);
String string = point.toString();
System.out.println(string); // (1,2)
}
} class Point {
private int x;
private int y; public int getX() {
return x;
} public void setX(int x) {
this.x = x;
} public int getY() {
return y;
} public void setY(int y) {
this.y = y;
} public Point() {
} public Point(int x, int y) {
this.x = x;
this.y = y;
} public String toString() {
return "(" + x + "," + y + ')';
}
}
在上面的代码中,我们定义了一个 Point 类,相当于二维坐标系上的一个点,我们通过重写 toString 方法实现了一个坐标点的位置。
在 Object 类中还有定义好的 equals 方法,意思是比较两个对象,如下:
public class Main {
public static void main(String[] args) {
Point point1 = new Point(1, 2);
Point point2 = new Point(1, 2);
System.out.println(point1 == point2); // false
System.out.println(point1.equals(point2)); // false
}
}
在上面的代码中,point1 == point2 其实比较的是两个类的引用地址,所以为 false,我们看一下 equals 方法的源码:
public boolean equals(Object obj) {
return (this == obj);
}
在上面的代码中,Object 类的 equals 方法其实也是相当于 == 的方法来进行比较,所以当我们使用 equals 方法时同样需要进行重写,他的作用是比较两个对象(当前对象与给定对象)内容是否一样,如下:
public class Main {
public static void main(String[] args) {
Point point1 = new Point(1, 2);
Point point2 = new Point(1, 2);
System.out.println(point1 == point2); // false
System.out.println(point1.equals(point2)); // true
}
} class Point {
private int x;
private int y; public int getX() {
return x;
} public void setX(int x) {
this.x = x;
} public int getY() {
return y;
} public void setY(int y) {
this.y = y;
} public Point() {
} public Point(int x, int y) {
this.x = x;
this.y = y;
} public String toString() {
return "(" + x + "," + y + ')';
} public boolean equals(Object object) {
if (object == null) {
return false;
}
if (object == this) {
return true;
}
if (object instanceof Point) {
Point point = (Point) object; // 强转为point类型
return this.x == point.x && this.y == point.y;
}
return false;
}
}
当我们重写 equals 方法后,就可以获取我们想要的结果了,即上面代码第 6 行结果输出为 true。同样的,只有自己定义的类需要重写,JAVA API 提供的类基本都重写了 equals。
最新文章
- 【Python】[面向对象高级编程] 使用__slots__,使用@property
- 【前端福利】用grunt搭建自动化的web前端开发环境-完整教程
- Java集合系列:-----------08HashMap的底层实现
- git merge 与 rebase 的区别
- Android_Intent_passValue(4)
- How to: Change icon in Inno Setup
- 我为什么放弃Go语言
- JS---DOM概述
- Qt多线程编程总结(一)(所有GUI对象都是线程不安全的)
- 8.2 sikuli 集成进eclipse 报错:Getting the VisionProxy.dll: Can not find dependent libraries...
- selenium-01 搭建环境
- CentOS7安装Postgresql
- WPF中在MVVM模式下,后台绑定ListCollectionView事件触发问题
- mysql数据库设计三范式
- dao层、service和action的运用和区别
- python基础入门之对文件的操作
- Ubuntu18.04搭建nodejs环境
- Fiddler(三)Fiddler 报错creation of the root certificate was not successful
- ASP.NET Web API 框架研究 Self Host模式下的消息处理管道
- 1、JUC--volatile 关键字-内存可见性