05-面向对象编程(中)

05—面向对象编程(中)

1. OPP特征二:继承性

image-20210815151705107

  • 为什么要有继承?

    • 多个类中存在相同属性和行为时,将这些内容抽取到一个单独一个类中,那么多个类无需再定义这些属性和行为,只要继承那个类即可。
  • 此处的多个类称为子类(派生类),单独的这个类称为父类(基类或超类)。可以理解为:“子类 is a 父类”

  • 类继承语法规则:

    class Subclass extends SuperClass{ }

屏幕截图 2021-08-15 151823

  • 作用:

    • 继承的出现减少了代码冗余,提高了代码的复用性。
    • 继承的出现,更有利于功能的扩展。
    • 继承的出现让类与类之间产生了关系,提供了多态的前提。
  • 注意:不要仅为了获取其他类中某个功能而去继承

  • 子类继承了父类,就继承了父类的方法和属性。

  • 在子类中,可以使用父类中定义的方法和属性,也可以创建新的数据和方法。

  • 在Java 中,继承的关键字用的是“extends”,即子类不是父类的子集, 而是对父类的“扩展”。

  • 关于继承的规则:

    • 子类不能直接访问父类中私有的(private)的成员变量和方法。

    image-20210815152152978

  • Java只支持单继承和多层继承,不允许多重继承

    • 一个子类只能有一个父类

    • 一个父类可以派生出多个子类

      1
      2
      class SubDemo extends Demo{ } //ok
      class SubDemo extends Demo1,Demo2...//error
    • 多层继承image-20210815152555906image-20210815152332229

    • 多重继承(×)

      image-20210815152406598

2. 方法的重写(override)

  • 定义:在子类中可以根据需要对从父类中继承来的方法进行改造,也称为方法的重置、覆盖。在程序执行时,子类的方法将覆盖父类的方法。

  • 要求:

    1. 子类重写的方法必须和父类被重写的方法具有相同的方法名称、参数列表
    2. 子类重写的方法的返回值类型不能大于父类被重写的方法的返回值类型
    3. 子类重写的方法使用的访问权限不能小于父类被重写的方法的访问权限
      1. 子类不能重写父类中声明为private权限的方法
    4. 子类方法抛出的异常不能大于父类被重写方法的异常
  • 注意: 子类与父类中同名同参数的方法必须同时声明为非static的(即为重写),或者同时声明为 static的(不是重写)。因为static方法是属于类的,子类无法覆盖父类的方法

  • 重写举例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    public class Person {
    public String name;
    public int age;
    public String getInfo() {
    return "Name: "+ name + "\n" +"age: "+ age;
    }
    }
    public class Student extends Person {
    public String school;
    public String getInfo() { //重写方法
    return "Name: "+ name + "\nage: "+ age + "\nschool: "+ school;
    }
    public static void main(String args[]){
    Student s1=new Student();
    s1.name="Bob";
    s1.age=20;
    s1.school="school2";
    System.out.println(s1.getInfo()); //Name:Bob age:20 school:school2
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Person p1=new Person();
    //调用Person类的getInfo()方法
    p1.getInfo();
    Student s1=new Student();
    //调用Student类的getInfo()方法
    s1.getInfo();
    /*
    这是一种“多态性”:同名的方法,用不同的对
    象来区分调用的是哪一个方法。
    */
    1
    2
    3
    4
    5
    6
    7
    class Parent {
    public void method1() {}
    }
    class Child extends Parent {
    //非法,子类中的method1()的访问权限private比被覆盖方法的访问权限public小
    private void method1() {}
    }

3. 四种访问权限修饰符

封装性的体现,需要权限修饰符来配合。

类的成员:可以用public、protected、 (缺省)、 private修饰

类:只可以用public和default(缺省)。

  1. public类可以在任意地方被访问。
  2. default类只可以被同一个包内部的类访问。
微信图片编辑_20210612235925

其中Hello_son是Hello的子类

image-20210612234446616image-20210613000339972

  • Java规定的4种权限(从小到大排列):private、缺省、protected、public

    • 修饰符 类内部 同一个包 不同包的子类 同一个工程
      private Yes
      缺省 Yes Yes
      protected Yes Yes Yes
      public Yes Yes Yes Yes

    对于class的权限修饰,只可以用public和缺省(default)

    • public类可以在任意地方被访问
    • default类只可以被同一个包的内部类访问
  • 4种权限可以用来修饰类及类的内部结构:属性、方法、构造器、内部类

    具体的,4种权限都可以用来修饰类的内部结构:属性、方法、构造器、内部类

    修饰类的话,只能使用:缺省、public

3.1在类为public时,对于属性和方法

在Test包下

在Hello.java文件里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package test;

public class Hello{
private int helloPrivate;
int helloDefault;
protected int helloProtected;
public int helloPublic;

private void methodPrivate(){
helloPrivate = 1; //即使最小的权限也能在类内部使用
helloDefault = 2;
helloProtected = 3;
helloPublic = 4;
}
void methodDefault(){
helloPrivate = 1; //即使最小的权限也能在类内部使用
helloDefault = 2;
helloProtected = 3;
helloPublic = 4;
}
protected void methodProtected(){
helloPrivate = 1; //即使最小的权限也能在类内部使用
helloDefault = 2;
helloProtected = 3;
helloPublic = 4;
}
public void methodPublic(){
helloPrivate = 1; //即使最小的权限也能在类内部使用
helloDefault = 2;
helloProtected = 3;
helloPublic = 4;
}
}

在test_.java文件里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package test;

public class test_{
public static void main(String[] args){
Hello hello = new Hello();//通过类造对象,再去调用对象里的属性和方法

// hello.helloPrivate = 1;//声明为private的属性只能在类内部被调用
// The field Hello.helloPrivate is not visible (属性不可见)
hello.helloProtected = 2;//赋值成功
hello.helloDefault = 3;//赋值成功
hello.helloPublic = 4;//赋值成功

// hello.methodPrivate();//声明为private的方法只能在类内部被调用
// The method methodPrivate() from the type Hello is not visible
hello.methodProtected();//成功调用
hello.methodDefault();//成功调用
hello.methodPublic();//成功调用
}
}

在test2包下

在test2.java文件下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package test2;

import test.Hello; //将test包下的Hello导入

public class test2{
public static void main(String[] args){
Hello hello = new Hello();//通过类造对象,再去调用对象里的属性和方法

// hello.helloPrivate = 1;//声明为private的属性只能在类内部被调用
// The field Hello.helloPrivate is not visible (属性不可见)
// hello.helloDefault = 2;//声明为Default的属性只能在包内部被调用
// The field Hello.helloDefault is not visible (属性不可见)
// hello.helloProtected = 3;
hello.helloPublic = 3;//赋值成功

// hello.methodPrivate();//声明为private的方法只能在类内部被调用
// The method methodPrivate() from the type Hello is not visible
// hello.methodDefault();//声明为Default的方法只能在包内部被调用
// The method methodDefault() from the type Hello is not visible
// hello.methodProtected();
hello.methodPublic();//成功调用
}
}

在Hello_son.java文件下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package test2;

import test.Hello;

public class Hello_son extends Hello {
public void method(){
// helloPrivate = 1; //not visible
// helloDefault = 2; //not visible
helloProtected = 3;
helloPublic = 4;

// methodProivate(); //not visible
// methodDefault(); //not visible
methodProtected();
methodPublic();
}
}

3.2 在类为缺省时,对于属性和方法

在Test包下

在Hello.java文件里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Hello {
private int helloPrivate;
int helloDefault;
protected int helloProtected;
public int helloPublic;

private void methodPrivate(){
helloPrivate = 1; //即使最小的权限也能在类内部使用
helloDefault = 2;
helloProtected = 3;
helloPublic = 3;
}
void methodDefault(){
helloPrivate = 1; //即使最小的权限也能在类内部使用
helloDefault = 2;
helloProtected = 3;
helloPublic = 3;
}
void methodProtected(){
helloPrivate = 1; //即使最小的权限也能在类内部使用
helloDefault = 2;
helloProtected = 3;
helloPublic = 3;
}
public void methodPublic(){
helloPrivate = 1; //即使最小的权限也能在类内部使用
helloDefault = 2;
helloProtected = 3;
helloPublic = 3;
}
}

在test_.java文件里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class test_ {
public static void main(String[] args){
Hello hello = new Hello();//通过类造对象,再去调用对象里的属性和方法

// hello.helloPrivate = 1;//声明为private的属性只能在类内部被调用
// The field Hello.helloPrivate is not visible (属性不可见)
hello.helloProtected = 2;//赋值成功
hello.helloDefault = 3;//赋值成功
hello.helloPublic = 4;//赋值成功

// hello.methodPrivate();//声明为private的方法只能在类内部被调用
// The method methodPrivate() from the type Hello is not visible
hello.methodProtected();//成功调用
hello.methodDefault();//成功调用
hello.methodPublic();//成功调用
}
}

在test2包下

在test2.java文件下

1
2
3
4
5
6
7
8
9
10
package test2;

import test.Hello; //将test包下的Hello导入 报错

public class test2{
public static void main(String[] args){
Hello hello = new Hello();//报错,声明为缺省状态的类只能在包里面使用

}
}

在Hello_son.java文件下

1
2
3
4
5
6
7
8
9
package test2;

import test.Hello;//报错 The type test.Hello is not visible

public class Hello_son extends Hello {
//报错 Hello cannot be resolved to a type

}

4. 关键字:super

  • 在Java类中使用super来调用父类中的指定操作:

    • super可用于访问父类中定义的属性
    • super可用于调用父类中定义的成员方法
    • super可用于在子类构造器中调用父类的构造器
  • 注意:

    • 尤其当子父类出现同名成员时,可以用super表明调用的是父类中的成员
    • super的追溯不仅限于直接父类
    • super和this的用法相像,this代表本类对象的引用,super代表父类的内存空间的标识
  • 关键字super举例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    class Person {
    protected String name = "张三";
    protected int age;
    public String getInfo() {
    return "Name: " + name + "\nage: " + age;
    }
    }
    class Student extends Person {
    protected String name = "李四";
    private String school = "New Oriental";
    public String getSchool() {
    return school;
    }
    public String getInfo() {
    return super.getInfo() + "\nschool: " + school;
    }
    }
    public class StudentTest {
    public static void main(String[] args) {
    Student st = new Student();
    System.out.println(st.getInfo());
    }
    }

    Name: 张三
    age: 0
    school: New Oriental

4.1 调用父类的构造器
  • 子类中所有的构造器默认都会访问父类中空参的构造器

  • 当父类中没有空参数的构造器时,子类的构造器必须通过this(参数列表)或者super(参数列表)语句指定调用本类或者父类中相应的构造器。同时,只能”二选一” ,且必须放在构造器的首行

  • 如果子类构造器中既未显式调用父类或本类的构造器,且父类中又没有无参的构造器,则编译出错

  • 调用父类构造器举例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    public class Person {
    private String name;
    private int age;
    private Date birthDate;
    public Person(String name, int age, Date d) {
    this.name = name;
    this.age = age;
    this.birthDate = d;
    }
    public Person(String name, int age) {
    this(name, age, null);
    }
    public Person(String name, Date d) {
    this(name, 30, d);
    }
    public Person(String name) {
    this(name, 30);
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public class Student extends Person {
    private String school;
    public Student(String name, int age, String s) {
    super(name, age);
    school = s;
    }
    public Student(String name, String s) {
    super(name);
    school = s;
    }
    // 编译出错: no super(),系统将调用父类无参数的构造器。
    public Student(String s) {
    school = s;
    }
    }
4.2 this和super的区别
No. 区别点 this super
1 访问属性 访问本类中的属性,如果本类没有此属性则从父类中继续查找 直接访问父类中的属性
2 调用方法 访问本类中的方法,如果本类没有此方法则从父类中继续查找 直接访问父类中的方法
3 调用构造器 调用本类构造器,必须放在构造器的首行 调用父类构造器,必须 放在子类构造器的首行

super(…)和this(…)调用语句不能同时在一个构造器中出现

5. 子类实例化过程

image-20210815160431840

image-20210815160659235

Inked202108151607563_LI

举例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package Test;

/**
* @author Xiong
* @create 2021-08-15-15:50
*/
class Creature {
public Creature() {
System.out.println("Creature无参数的构造器");
}
}

class Animal extends Creature {
public Animal(String name) {
System.out.println("Animal带一个参数的构造器,该动物的name为" + name);
}

public Animal(String name, int age) {
this(name);
System.out.println("Animal带两个参数的构造器,其age为" + age);
}
}

public class Wolf extends Animal {
public Wolf() {
super("灰太狼", 3);
System.out.println("Wolf无参数的构造器");
}

public static void main(String[] args) {
new Wolf();
}
}


Creature无参数的构造器
Animal带一个参数的构造器,该动物的name为灰太狼
Animal带两个参数的构造器,其age为3
Wolf无参数的构造器

6. OOP特征三:多态性

  • 多态性,是面向对象中最重要的概念,在Java中的体现:

    对象的多态性:父类的引用指向子类的对象

    • 可以直接应用在抽象类和接口上
  • Java引用变量有两个类型:编译时类型和运行时类型。编译时类型由声明该变量时使用的类型决定,运行时类型由实际赋给该变量的对象决定。简 称:编译时,看左边;运行时,看右边。

    • 若编译时类型和运行时类型不一致,就出现了对象的多态性(Polymorphism)

    • 多态情况下, “看左边” :看的是父类的引用(父类中不具备子类特有的方法)

      ​ “看右边” :看的是子类的对象(实际运行的是子类重写父类的方法)

  • 对象的多态 —在Java中,子类的对象可以替代父类的对象使用

    • 一个变量只能有一种确定的数据类型

    • 一个引用类型变量可能指向(引用)多种不同类型的对象

      1
      2
      3
      Person p = new Student();
      Object o = new Person();//Object类型的变量o,指向Person类型的对象
      o = new Student(); //Object类型的变量o,指向Student类型的对象
    • 子类可看做是特殊的父类,所以父类类型的引用可以指向子类的对象:向上转型(upcasting)。

  • 一个引用类型变量如果声明为父类的类型,但实际引用的是子类对象,那么该变量就不能再访问子类中添加的属性和方法

    1
    2
    3
    4
    Student m = new Student();
    m.school = “pku”; //合法,Student类有school成员变量
    Person e = new Student();
    e.school = “pku”; //非法,Person类没有school成员变量

    属性是在编译时确定的,编译时e为Person类型,没有school成员变量,因而编译错误。

  • 方法声明的形参类型为父类类型,可以使用子类的对象作为实参调用该方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class Test {
    public void method(Person e) {
    // ……
    e.getInfo();
    }
    public static void main(Stirng args[]) {
    Test t = new Test();
    Student m = new Student();
    t.method(m); // 子类的对象m传送给父类类型的参数e
    }
    }
5.1 虚拟方法调用
  • 正常的方法调用

    1
    2
    3
    4
    Person e = new Person();
    e.getInfo();
    Student e = new Student();
    e.getInfo();
  • 虚拟方法调用(多态情况下)

    子类中定义了与父类同名同参数的方法,在多态情况下,将此时父类的方法称为虚拟方法,父类根据赋给它的不同子类对象,动态调用属于子类的该方法。这样的方法调用在编译期是无法确定的。

    1
    2
    Person e = new Student();
    e.getInfo(); //调用Student类的getInfo()方法
  • 编译时类型和运行时类型

    编译时e为Person类型,而方法的调用是在运行时确定的,所以调用的是Student类 的getInfo()方法。——动态绑定

5.2 方法的重载与重写

​ 从编译和运行的角度看:

​ 重载,是指允许存在多个同名方法,而这些方法的参数不同。编译器根据方法不同的参数表,对同名方法的名称做修饰。对于编译器而言,这些同名方法就成了不同的方法。它们的调用地址在编译期就绑定了。Java的重载是可以包括父类和子的,即子类可以重载父类的同名不同参数的方法。 所以:对于重载而言,在方法调用之前,编译器就已经确定了所要调用的方法, 这称为“早绑定”或“静态绑定”;

1
而对于多态,只有等到方法调用的那一刻,解释运行器才会确定所要调用的具体 方法,这称为“晚绑定”或“动态绑定”。 引用一句Bruce Eckel的话:“不要犯傻,如果它不是晚绑定,它就不是多态。”
5.3 instanceof操作符

x instanceof A:检验x是否为类A的对象,返回值为boolean型。

  • 要求x所属的类与类A必须是子类和父类的关系,否则编译错误。

  • 如果x属于类A的子类B,x instanceof A值也为true。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public class Person extends Object {…}
    public class Student extends Person {…}
    public class Graduate extends Person {…}
    -------------------------------------------------------------------
    public void method1(Person e) {
    if (e instanceof Person)
    // 处理Person类及其子类对象
    if (e instanceof Student)
    //处理Student类及其子类对象
    if (e instanceof Graduate)
    //处理Graduate类及其子类对象
    }
5.4 对象类型转换 (Casting )
  • 基本数据类型的Casting:

    • 自动类型转换:小的数据类型可以自动转换成大的数据类型

      如long g=20; double d=12.0f

    • 强制类型转换:可以把大的数据类型强制转换(casting)成小的数据类型

      如 float f=(float)12.0; int a=(int)1200L

  • 对Java对象的强制类型转换称为造型

    • 从子类到父类的类型转换可以自动进行
    • 从父类到子类的类型转换必须通过造型(强制类型转换)实现
    • 无继承关系的引用类型间的转换是非法的
    • 在造型前可以使用instanceof操作符测试一个对象的类型
  • image-20210815164314304

  • 继承成员变量和继承方法的区别

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    class Base {
    int count = 10;
    public void display() {
    System.out.println(this.count);
    }
    }
    class Sub extends Base {
    int count = 20;
    public void display() {
    System.out.println(this.count);
    }
    }
    public class FieldMethodTest {
    public static void main(String[] args){
    Sub s = new Sub();
    System.out.println(s.count);
    s.display();
    Base b = s;
    System.out.println(b == s);
    System.out.println(b.count);
    b.display();
    }
    }

    20
    20
    true
    10
    20

  • 子类继承父类

    • 若子类重写了父类方法,就意味着子类里定义的方法彻底覆盖了父类里的同名方法,系统将不可能把父类里的方法转移到子类中。
    • 对于实例变量则不存在这样的现象,即使子类里定义了与父类完全相同的实例变量,这个实例变量依然不可能覆盖父类中定义的实例变量。

7. Object类的使用

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

1
2
3
4
5
6
7
public class Person {
...
}
//等价于:
public class Person extends Object {
...
}
7.1 Object类中的主要结构
NO. 方法名称 类型 描述
1 public Object() 构造 构造器
2 public boolean equals(Object obj) 普通 对象比较
3 public int hashCode() 普通 取得Hash码
4 public String toString() 普通 对象打印时调用
7.2 ==操作符与equals方法
  • ==

    • 基本类型比较值:只要两个变量的值相等,即为true。

      • int a=5; if(a==6){…}
    • 引用类型比较引用(是否指向同一个对象):只有指向同一个对象时,==才返回true。

      Person p1=new Person();

      Person p2=new Person();

      if (p1==p2){…}

      • 用“ == ” 进行比较时,符号两边的数据类型必须兼容(可自动转换的基本数据类型除外),否则编译出错
  • equals():所有类都继承了Object,也就获得了equals()方法。还可以重写。如果该方法没有被重写过默认也是 == ;

    • 只能比较引用类型,其作用与“==”相同,比较是否指向同一个对象。
    • 格式:obj1.equals(obj2)
  • 特例:当用equals()方法进行比较时,对类File、String、Date及包装类 (Wrapper Class)来说,是比较类型及内容而不考虑引用的是否是同一个对 象;

    • 原因:在这些类中重写了Object类的equals()方法。

    当自定义使用equals()时,可以重写。用于比较两个对象的“内容”是否都相等

7.2.1 重写equals()方法的原则
  • 对称性:如果x.equals(y)返回是“true” ,那么y.equals(x)也应该返回是 “true”。
  • 自反性:x.equals(x)必须返回是“true”。
  • 传递性:如果x.equals(y)返回是“true” ,而且y.equals(z)返回是“true” , 那么z.equals(x)也应该返回是“true”。
  • 一致性:如果x.equals(y)返回是“true” ,只要x和y内容一直不变,不管你 重复x.equals(y)多少次,返回都是“true”。
  • 任何情况下,x.equals(null),永远返回是“false” ; x.equals(和x不同类型的对象)永远返回是“false”。
1
2
3
4
5
6
7
8
9
10
11
int it = 65;
float fl = 65.0f;
System.out.println(“6565.0f是否相等?” + (it == fl)); //true
char ch1 = 'A'; char ch2 = 12;
System.out.println("65和'A'是否相等?" + (it == ch1));//true
System.out.println(“12和ch2是否相等?" + (12 == ch2));//true
String str1 = new String("hello");
String str2 = new String("hello");
System.out.println("str1和str2是否相等?"+ (str1 == str2));//false
System.out.println("str1是否equals str2?"+(str1.equals(str2)));//true
System.out.println(“hello” == new java.util.Date()); //编译不通过
7.3 toString()方法

toString()方法在Object类中定义,其返回值是String类型,返回类名和它的引用地址。在进行String与其它类型数据的连接操作时,自动调用toString()方法

1
2
3
Date now=new Date();
System.out.println(“now=”+now); 相当于
System.out.println(“now=”+now.toString());

可以根据需要在用户自定义类型中重写toString()方法 如String 类重写了toString()方法,返回字符串的值。

1
2
s1=“hello”;
System.out.println(s1);//相当于System.out.println(s1.toString());

基本类型数据转换为String类型时,调用了对应包装类的toString()方法

int a=10; System.out.println(“a=”+a)

8. 包装类(Wrapper)的使用

针对八种基本数据类型定义相应的引用类型—包装类(封装类),有了类的特点,就可以调用类中的方法,Java才是真正的面向对象

image-20210815171252735

  • 基本数据类型包装成包装类的实例 —装箱

    • 通过包装类的构造器实现:

      int i = 500;

      Integer t = new Integer(i);

    • 还可以通过字符串参数构造包装类对象:

      Float f = new Float(“4.56”);

      Long l = new Long(“asdf”); //NumberFormatException

  • 获得包装类对象中包装的基本类型变量 —拆箱

    • 调用包装类的.xxxValue()方法:

      boolean b = bObj.booleanValue();

      JDK1.5之后,支持自动装箱,自动拆箱。但类型必须匹配。

  • 字符串转换成基本数据类型

    • 通过包装类的构造器实现:

      int i = new Integer(“12”);

    • 通过包装类的parseXxx(String s)静态方法:

      Float f = Float.parseFloat(“12.1”);

  • 基本数据类型转换成字符串

    • 调用字符串重载的valueOf()方法: String fstr = String.valueOf(2.34f);
    • 更直接的方式: String intStr = 5 + “”
  • 基本类型、包装类与String类间的转换

    image-20210815171942936