面向对象学习day15

本文详细介绍了Java中的Object类基础,包括其作用、构造器和常用方法如equals()与toString()的原理与重写规则。此外,还对比了==和equals()的区别,展示了基本数据类型和包装类在equals()中的行为。
摘要由CSDN通过智能技术生成

java面向对象学习day15

无论当下的境遇如何,这片大陆的星空下始终都有你的位置。

一、Object类

1、说明:

  • Object类是所有Java类的根父类

  • 如果在类的声明中未使用extends关键字指明其父类,则默认父类为java.lang.Object

  • Object类中的功能(属性、方法)就具有通用性

  • Object类只声明了一个空参的构造器

  • 方法:equals()、toString()、getClass()、hashCode()、clone()、finalize()、wait()、notify()、notifyAll()

2、面试题:

final、finally、finalize的区别?

二、equals

1、面试题: == 和 equals() 区别

1.1 “==”的使用:

  • 如果比较的是基本数据类型变量:比较的是两个变量保存的数据是否相等。(不一定要类型相同)

  • 如果比较的是引用数据类型变量:比较两个变量的地址值是否相等。即,两个引用是否指向同一个对象实体

  • “==”符号使用时,必须保证符号左右两边的变量类型一致。

1.2“equals()”的使用:

  • 是一个方法,而非运算符。因此,只适用于引用数据类型。

  • Object类中equals()源码中的定义:

    public boolean equals(Object obj) { ​ return (this == obj); ​ }

    注:Object类中定义的equals的作用是相同的:比较两个对象的地址值是否相同。

  • 像String、Date、File、包装类等都重写了Object类中的equals()方法。重写以后,比较的不是两个引用的地址是否相同。而是比较两个对象的“实体内容”是否相同。

  • 通常情况下,完美自定义的类如果使用equals()的话,通常是比较两个对象的”实体内容“是否相同。那么我们就需要对Object类中的equals()进行重写。重写的原则:比较两个对象的实体内容是否相同

1.3 重写equals()方法的原则

2、实例

 package Object.day15.Test;
 import Object.day15.Customer;
 import java.util.Date;
 public class EqualsTest {
     public static void main(String[] args) {
 ​
         //基本数据类型
         int i = 9;
         int j = 9;
         double d = 9.0;
         System.out.println(i == j);
         System.out.println(i == d);
 ​
         boolean b = true;
         //System.out.println(boolean == i);//编译报错
 ​
         char c = 9;
         System.out.println(i == c);//true
 ​
         //引用数据类型
         Customer cust1 = new Customer("jay",22);
         Customer cust2 = new Customer("jay",22);
         System.out.println(cust1 == cust2);//false
 ​
         String str1 = new String("nihaoya");
         String str2 = new String("nihaoya");
         System.out.println(str1 == str2);//false
 ​
         System.out.println(cust1.equals(cust2));//false
         System.out.println(str1.equals(str2));//true
 ​
         Date date1 = new Date(2342422424L);
         Date date2 = new Date(2342422424L);
         System.out.println(date1.equals(date2));//true
 ​
     }
 }

三、toString()

1、Object类中toString()的使用

  • 当我们输出一个对象的引用时,实际上就是调用当前对象的toString()

  • Object类中toString()定义的源码:

    public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }

  • 像String、Date、File、包装类等都重写了Object类中的toString()方法。使得在调用对象的toString()时,返回“实体内容”信息

  • 自定义类也可以重写toString()方法,当调用此方法时,返回对象的“实体内容”

四、包装类(Wrapper)

1、包装类的使用

  • java提供了8种基本数据类型对应的包装类,使得基本数据类型的变量具有类的特征。

  • 掌握的:基本数据类型、包装类、String之间的相互转换

2、概述

3、基本数据类型、包装类和String类间的转化

重点:

  • 基本数据类型和包装类之间可自动装箱与自动拆箱

  • 基本数据类型和包装类转化成String类利用String重载的 valueOf() 方法

  • String转化成基本数据类型和包装类利用包装类的parseXxx(String)方法

实例:

 
package Object.day15;
 import org.junit.Test;
 public class WrapperTest {
 ​
 //String类型 --->基本数据类型、包装类型:调用包装类的parseXxx(String s)
     @Test
     public void test5(){
         String str1 = "123";
         int num2 = Integer.parseInt(str1);
         System.out.println(num2 + 1);
 ​
         String str2 = "true";
         boolean b1 = Boolean.parseBoolean(str2);
         System.out.println(b1);
 ​
         String str3 = "true1";//不是标准的true都是false
         boolean b2 = Boolean.parseBoolean(str3);
         System.out.println(b2);
     }
 ​
 //基本数据类型、包装类 --->String类型:调用String重载的valueOf()
     @Test
     public void test4(){
 ​
         int num1 = 10;
         //方式1:连接运算
         String str1 = num1 + "";
         //方式2:调用String的valueOf(xxx)
         float f1 = 12.3f;
         String str2 = String.valueOf(f1);
         System.out.println(str2);
         Double d1 = new Double(12.3);
         String str3 = String.valueOf(d1);
         System.out.println(str3);
 ​
     }
 ​
     //JDK 5.0新特性:自动装箱与自动拆箱
     @Test
     public void test3(){
         int num1 = 10;
         method(num1);
     }
 ​
     public void method(Object obj){
         System.out.println(obj);
     }
 ​
 //包装类 ---> 基本数据类型 : 调用xxxValue()
     @Test
     public void test2(){
         Integer in1 = new Integer(12);
         int i1 = in1.intValue();
         System.out.println(i1 + 1);
 ​
         Float f1 = new Float(12.3);
         float f2 = f1.floatValue();
         System.out.println(f2);
 ​
     }
 ​
 //基本数据类型 ---> 包装类 : 调用包装类的构造器
     @Test
     public void test1(){
 ​
         int num1 = 10;
         Integer in1 = new Integer(num1);
         System.out.println(in1.toString());
 ​
         Integer in2 = new Integer("12345");
         System.out.println(in2.toString());
 ​
 //        报异常(里面必须为int类型)
 //        Integer in3 = new Integer("12345abc");
 //        System.out.println(in3.toString());
 ​
         Float f1 = new Float(12.3);
         Float f2 = new Float("12.4");
         System.out.println(f1);
         System.out.println(f2);
 ​
         Boolean b1 = new Boolean(true);
         Boolean b2 = new Boolean("TrUe");
         System.out.println(b2);
         Boolean b3 = new Boolean("true123");
         System.out.println(b3);
 ​
         Order1 order1 = new Order1();
         System.out.println(order1.isMale);
         System.out.println(order1.isFemale);
 ​
     }
 ​
 }
 ​
 class Order1{
     boolean isMale;
     Boolean isFemale;
 }

3、包装类相关面试题

题一:下面两者的输出分别什么?

 
@Test
 public void test1(){
     Object o1 = true ? new Integer(1) : new Double(2.0);
     System.out.println(o1);
 }
 ​
 @Test
 public void test2(){
     Object o2;
     if(true)
         o2 = new Integer(1);
     else
         o2 = new Double(2.0);
     System.out.println(o2);
 }

result:

1.0

1

analyze:

三元运算符的右边两个选项的类型必须相同。而上述例子中左边为int类型,而右边为double类型。故会先将左边的int类型自动转化为double,即1.0。转化完成后再执行三元运算符。故最终输出的结果为1.0。(坑爹呀)

题二:下面程序输出的结果为?

 Integer i = new Integer(1);
 Integer j = new Integer(1);
 System.out.println(i == j);//true
 ​
 Integer m = 1;
 Integer n = 1;
 System.out.println(m == n);
 ​
 Integer x = 128;
 Integer y = 128;
 System.out.println(x == y);

result:

analyze:

Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[ ],保存了从-128~127范围的整数。如果我们使用自动装箱的方式,给Integer赋值的范围在-128~127范围内时,直接使用了数组中的元素,不用再去new了。若超出这个范围,则需去new对象。目的:提高效率

Examples

test1:

topic:

 

code:

GeometricObject:

 
package Object.day14.Test;
 ​
 public class GeometricObject {
 ​
     protected String color;
     protected double weight;
 ​
     public GeometricObject(){
 ​
     }
     protected GeometricObject(String color,double weight){
         this.color = color;
         this.weight = weight;
     }
 ​
     public void setColor(String color){
         this.color = color;
     }
     public String getColor(){
         return color;
     }
 ​
     public void setWeight(double weight){
         this.weight = weight;
     }
     public double getWeight(){
         return weight;
     }
 ​
     public double findArea(){
         return 0.0;
     }
 ​
 }

Circle:

 
package Object.day14.Test;
 ​
 public class Circle extends GeometricObject{
 ​
     private double radius;
 ​
     public Circle(double radius,String color,double weight){
         super(color,weight);
         this.radius = radius;
     }
 ​
     public void setRadius(double radius){
         this.radius = radius;
     }
     public double getRadius(){
         return radius;
     }
 ​
     public double findArea(){
         return Math.PI * radius * radius;
     }
 }

MyRectangle:

 
package Object.day14.Test;
 ​
 public class MyRectangle extends GeometricObject{
 ​
     private double width;
     private double height;
 ​
     public MyRectangle(double width,double height,String color,double weight){
         super(color,weight);
         this.width = width;
         this.height = height;
     }
 ​
     public void setWidth(double width){
         this.width = width;
     }
     public double getWidth(){
         return width;
     }
 ​
     public void setHeight(double height){
         this.height = height;
     }
     public double getHeight(){
         return height;
     }
 ​
     public double findArea(){
         return width * height;
     }
 ​
 }

GeometricTest:

 package Object.day14.Test;
 ​
 public class GeometricTest {
 ​
     public static void main(String[] args) {
         GeometricTest test = new GeometricTest();
         Circle c1 = new Circle(2.0,"red",3.0);
         test.displayGeometricObject(c1);
         Circle c2 = new Circle(3.0,"blue",7.0);
         test.displayGeometricObject(c2);
 ​
         System.out.println(test.equalsArea(c1, c2));
     }
 ​
         public boolean equalsArea(GeometricObject o1,GeometricObject o2){
             return o1.findArea() == o2.findArea();
         }
 ​
         public void displayGeometricObject(GeometricObject o3){
             System.out.println("面积为:" + o3.findArea());
         }
 ​
 ​
 ​
 }

result:

test2:

code:

package Object.day15.Test.test1;

public class test {
    public static void main(String[] args) {
        Base1 base = new Sub1();
        base.add(1,2,3);
        Sub1 s = (Sub1)base;
        s.add(1,2,3);
    }
}

class Base1{
    public void add(int a,int... arr){
        System.out.println("base1");
    }
}

class Sub1 extends Base1{
    public void add(int a,int[] arr){
        System.out.println("sub1");
    }

    public void add(int a,int b,int c){
        System.out.println("sub2");
    }
}

result:

analyze:

首先子类Sbu1继承于父类Base1。而利用多态性,创建了base对象。在调用add方法时,发现子类重写了add方法,子类将父类的add方法覆盖。所以base调用的为子类重写的方法。即第一个输出语句为“sub1”。

而后面又将base强制转型成了Sub1类型,此时调用add方法时,便优先考虑形参为三个的后者方法。即第二个输出语句为“sub2”

        

        今天也是看到几句很棒的话:

        1、平凡的好处,在于身边处处都是你的参照物。处处都是理解你的人

        2、无论当下的境遇如何,这片大陆的星空下始终有你的位置

        3、旋涡无法击碎的磐石,也终将会在时光的冲刷下磨损。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值