第五章 面向对象(中)
Java 面向对象学习的三条主线:
Java 类及类的成员:属性,方法,构造器,代码块,内部类
面向对象的三大特征:封装性,继承性,多态性,(抽象性)
其他关键字:this,super,static,final,abstract,interface,import等
5.7 Object类下:toString( )方法的使用
5.7.1 Object类中toString( )方法的简述
当我们输出一个对象的引用时,实际上是调用当前对象的toString( )方法
toString( )方法在Obejct类中定义,其返回值是String类型,返回类名和它的引用地址。
在进行String与其它类型数据的连接操作时,自动调用toString( )方法
可以根据需要在用户自定义类型中重写toString( )方法
基本类型数据转换为String类型时,调用了对应包装类的toString( )方法
5.7.2 Object类中toString( )方法的定义
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
5.7.3 Object类中toString( )方法的重写
像String,Date,File,包装类等都重写了Object类中的toString( )方法。使得在调用对象的toString( )时,返回“实体内容”信息。自定义类也可以重写toString( )方法,,当调用此方法时,返回对象的“实体内容”。
//toString方法的重写
//return返回值内容可以根据需求自定义返回值内容
//当前Order 为 类名
//当前orderId orderName 为属性名
@Override
public String toString() {
return "Oder{" +
"orderId=" + orderId +
", orderName='" + orderName + '\'' +
'}';
}
5.7.4 Object类中toString( )方法的使用实例
//父类 GeometricObject
public class GeometricObject {
private String color;
private double weight;
public GeometricObject() {
super();
this.color = "white";
this.weight = 1.0;
}
public GeometricObject(String color, double weight) {
super();
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;
}
}
//子类 Circle
import java.util.Objects;
public class Circle extends GeometricObject{
private double radius;
public Circle() {
super();
radius = 1.0;
}
public Circle(double radius) {
super();
this.radius = radius;
}
public Circle(String color, double weight, double radius) {
super(color, weight);
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double findArea(){
return Math.PI*radius*radius;
}
//重写equals()方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Circle circle = (Circle) o;
return Double.compare(circle.radius, radius) == 0;
}
//重写toString()方法
@Override
public String toString() {
return "Circle{" +
"radius=" + radius +
'}';
}
}
//测试类
public class CircleTest {
public static void main(String[] args) {
Circle c1 = new Circle(2.3);
Circle c2 = new Circle("white",2.3,2.3);
System.out.println("颜色是否相等:"+c1.getColor().equals(c2.getColor()));
//颜色是否相等:true
System.out.println("半径是否相等:"+c1.equals(c2));//半径是否相等:true
System.out.println(c1);//Circle{radius=2.3}
System.out.println(c2.toString());//Circle{radius=2.3}
}
}
5.8 Java中的JUnit单元测试
5.8.1 Java中的JUnit单元测试的使用步骤
①.创建Java类,进行单元测试(此时的Java类的要求:此类必须是public(公共类),此类提供公共的无参的构造器)
②.此类中声明单元测试方法:方法的权限是public,没有返回值,没有形参
③.单元测试方法上需要声明注解:@Test,并在单元测试类中导入:import org.junit.Test;
④.声明好单元测试以后,就可以在方法体内测试相关代码
⑤.每个测试类都可以独立运行,进行测试方法体内的需求
5.8.2 Java中的JUnit单元测试的使用实例
//利用Java单元测试方法进行便捷测试代码
import org.junit.Test;
public class CircleTest {
Circle c1 = new Circle(2.3);
Circle c2 = new Circle("white",2.3,2.3);
//单元测试的使用
@Test
public void CircleEquals(){
System.out.println("颜色是否相等:"+c1.getColor().equals(c2.getColor()));//颜色是否相等:true
System.out.println("半径是否相等:"+c1.equals(c2));//半径是否相等:true
}
//单元测试的使用
@Test
public void CircleToString(){
System.out.println(c1);//Circle{radius=2.3}
System.out.println(c2.toString());//Circle{radius=2.3}
}
}
5.9 包装类(Wrapper)的使用
5.9.1 包装类的简述
针对八种基本数据类型定义相应的引用类-包装类(封装类),使得基本数据类型的变量具有类的特征
有了类的特点,就可以调用类中的方法,Java才是真正的面向对象
5.9.2 基本数据类型,包装类与String类间的转换
①.基本数据类型转换为 ----> 包装类 :调用包装类的构造器
@Test
public void demo01() {
int num = 10;
Integer integer = new Integer(num);
System.out.println(integer.toString());
Boolean ble = false;
Boolean aBoolean = new Boolean(ble);
System.out.println(aBoolean.toString());
}
②.包装类转换为 ----> 基本数据类型 :调用包装类的xxxValue
@Test
public void demo02() {
Integer integer = new Integer(10);
int i = integer.intValue();
System.out.println(i);
Boolean aBoolean = new Boolean(false);
boolean b = aBoolean.booleanValue();
System.out.println(b);
}
③. Java5.0新特性:自动装箱与自动拆箱
//③.包装类的自动装箱和自动拆箱
@Test
public void demo03() {
//自动装箱 基本数据类型 ---> 包装类对象
int num1 = 10;
Integer integer1 = num1; //自动装箱
System.out.println(integer1.toString());
boolean ble1 = false;
Boolean aBoolean1 = ble1; //自动装箱
System.out.println(aBoolean1.toString());
//自动拆箱 包装类对象 ---> 基本数据类型
Integer integer2 = 10;
int num2 = integer2;
System.out.println(num2); //自动拆箱
Boolean aBoolean2 = false;
boolean ble2 = aBoolean2;
System.out.println(ble2); //自动拆箱
}
④. 基本数据类型,包装类 ----> String类型
//④.基本数据类型,包装类 ----> String类型 :调用String重载的ValueOf(Xxx xxx)
@Test
public void demo04() {
int num1 = 10;
//方式一:连接运算
String str1 = num1 + " 数字";
System.out.println(str1);
//方式二:调用String重载的ValueOf(Xxx xxx)
int num2 = 10;
String str2 = String.valueOf(num2);
System.out.println(str2+" "+str2.getClass());
}
5.9.1 包装类的使用实例应用
//如下两个题目输出结果相同吗?各是什么:
@Test
public void demo05() {
Object o1 = true ? new Integer(1) : new Double(2.0);
System.out.println(o1);//1.0
Object o2;
if (true)
o2 = new Integer(1);
else
o2 = new Double(2.0);
System.out.println(o2);//1
}
//面试题②
@Test
public void demo06() {
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i == j);//false 对象的引用地址值比较
/*Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[],保存了从-128~127范围的整数。
如果我们使用自动装箱的方式,给Integer赋值的范围在 -128~127范围内时,可以直接使用数组中的元素,不用new,提高了效率。
但如果赋值超过范围,则等于new了一个新的Integer对象*/
Integer m = 1;
Integer n = 1;
System.out.println(m == n);//true 数组内数值的比较
Integer x = 128;//等于new了一个新的Integer对象
Integer y = 128;//等于new了一个新的Integer对象
System.out.println(x == y);//false 对象的引用地址值比较
}