public class Person{
......
}
等价于:
public class Person extends Onject{
......
}
| 方法名称 | 类型 | 描述 |
|---|---|---|
| public Object() | 构造 | 构造器 |
| public boolean euqals(Object obj) | 普通 | 对象比较 |
| public int hashCode() | 普通 | 取得Hash码 |
| public String toString() | 普通 | 对象打印时调用 |
1、equals()是一个方法,而非运算符
2、equals()只能适用于引用数据类型
3、Object类中equals()的定义:
public boolean equals(Object obj){
return(this==obj);
}
说明:Object类中的equals()方法和“==”的作用是相同的,都是比较量两对象的 地址值。
4、想String、Date、File,包装类等都重写了Object类中的euqals()方法,重写之后比较的不知两个引用的地址是否相同,而是比较两个对象的“实体内容”是否相同
5、通常情况下,使用equals()也通常比较的是两对象的“实体内容”。因此需要对Object()类中的equals()方法进行重写
比较两对象的实体内容是否相等

1.编写Order类,有int型的orderId,String型的orderName,相应的 getter()和setter()方法,两个参数的构造器,重写父类的equals()方法: public boolean equals(Object obj),并判断测试类中创建的两个对象是否相等。
- public class OrderTest {
- public static void main(String[] args) {
- Order order1=new Order(1001,"AA");
- Order order2=new Order(1001,"AB");
- System.out.println(order1.equals(order2));
-
- }
- }
-
- class Order{
- private int orderId;
- private String orderName;
-
- public Order(int orderId ,String orderName) {
- this.orderId=orderId;
- this.orderName=orderName;
- }
-
- public int getOrderId() {
- return orderId;
- }
- public void setOrderId(int orderId) {
- this.orderId = orderId;
- }
- public String getOrderName() {
- return orderName;
- }
- public void setOrderName(String orderName) {
- this.orderName = orderName;
- }
- @Override
- public boolean equals(Object obj) {
- if(this==obj) {
- return true;
- }
- if(obj instanceof Order) {
- Order order=(Order)obj;//强转
-
- return this.orderId==order.orderId&&this.orderName.equals(order.orderName);
- }
- return false;
- }
- }
运行结果如下:

1、toString()方法在Object类中定义,其返回值是String类型,返回类名和它的引用地址
public String toString{
return getClass.getname()+"@"+Intteger.toHexString(hashCode());
}
2、在进行String与其他类型数据的连接时,自定调用toString()方法
Date now =new Date;
System.out.println("now="+now);
相当于:
System.out.println("now="+now.toString());
3、当我们输出一个对象的引用时,实际上是调用了当前对象的yoString()方法
4、基本数据类型转换为String类型是,调用了包装类中的toString()方法
5、像String、Date、File、包装类都重写了Object类的toString()方法,使得对象再调用toString()方法时,返回的是“实体内容”信息
6、自定义类可以根据需要重写toString()方法,是的方法返回的是对象的“实体内容”
定义两个类,父类GeometricObject代表几何形状,子类Circle代表圆形。


写一个测试类,创建两个Circle对象,判断其颜色是否相等;利用equals方法判断其半径是否相等;利用 toString()方法输出其半径。
- public class ObjectTest {
- public static void main(String[] args) {
- Circle o1=new Circle(1.0);
- Circle o2=new Circle(2.0);
- System.out.println("两圆的颜色是否相等:"+o1.getColor().equals(o2.getColor()));
- System.out.println("两圆的半径是否相等:"+o1.equals(o2));
- System.out.println("圆1的半径为:"+o1.toString());
- System.out.println("圆2的半径为:"+o2.toString());
- }
- }
-
- class GeometricObject {
- private String color;
- private double weight;
-
- public GeometricObject() {
- color="white";
- weight=1.0;
- }
-
- /**
- * @param color:颜色
- * @param weight:重量
- */
- public GeometricObject(String color, double weight) {
- this.color = color;
- this.weight = weight;
- }
-
- public String getColor() {
- return color;
- }
-
- public void setColor(String color) {
- this.color = color;
- }
-
- public double getWeight() {
- return weight;
- }
-
- public void setWeight(double weight) {
- this.weight = weight;
- }
-
-
- }
- class Circle extends GeometricObject {
- private double radius;
- public Circle() {
- super();
-
- }
- public Circle(double radius) {
- super();
- this.radius=radius;
- }
- public Circle(double radius,String color,double weight) {
- super();
- this.radius=radius;
- }
- public double getRadius() {
- return radius;
- }
- public void setRadius(double radius) {
- this.radius = radius;
- }
- public double findArea() {
- return getRadius()*getRadius()*Math.PI;
- }
-
- /**
- * @Override
- *比较两圆半径是否相等
- */
- public boolean equals(Object obj) {
- if(this==obj) {
- return true;
- }
- if(obj instanceof Circle) {
- Circle cir=(Circle)obj;
- return cir.radius==this.radius;
- }
- return false;
-
- }
- /**
- * @Override
- * 输出圆的半径
- */
- public String toString() {
- return "圆的半径是:"+this.radius;
-
- }
- }
运行结果如下:
1、Junit进行单元测试要求:
2、此类声明单元测试的方法: 方法的权限public ,没有返回值,没有形参
3、此单元测试方法上需要声明注释:@Test;并在单元测试中导入:import org.junit.Test;
4、声明单元测试方法后,就可以在方法体内测试相关代码
针对八种基本数据类型定义响应相应的引用类型——包装类(封装类)
有了类的特点,就可以调用类中的方法,java才是真正的面向对象
| 基本数据类型 | 包装类 |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| boolean | Boolean |
| char | Character |
int i=500;
Integer t =new Intrger(i);
Float f=new Float("4.56");
boolean b=bObj.booleanValue();
JDK1.5后,支持自动装箱、自动拆箱。但类型必须匹配。
int i=new Integer("12");
Float f =Float.parseFloat("12.1");
String fstr=String.valueOf(2.34f);
String str=5+“”;
- public void method1() {
- Integer i = new Integer(1);
- Integer j = new Integer(1);
- System.out.println(i == j);//会输出false(==比较的是地址值)
- Integer m = 1;
- Integer n = 1;//自动装箱
- System.out.println(m == n);//true
- Integer x = 128;
- Integer y = 128;//相当于new了一个Integer对象
- System.out.println(x == y);//false
- }
- /*
- **Integer内部定义了IntegerCache结构,IntegerCache中定义了一个
- **Integer[],保存了从-128~127,如果使用自动装箱的方式,给Integer赋值,
- **变量范围在-128~127范围内时,可以直接使用数组中的元素,不用再去new了
- */
Integer内部定义了IntegerCache结构,IntegerCache中定义了一个Integer[],保存了从-128~127,如果使用自动装箱的方式,给Integer赋值,变量范围在-128~127范围内时,可以直接使用数组中的元素,不用再去new了