原创

Java中的面向对象编程概念

温馨提示:
本文最后更新于 2022年11月16日,已超过 529 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

1.概述

在本文中,我们将研究Java中的面向对象编程(OOP)概念。我们将讨论 类、对象、抽象、封装、继承和多态性.

2.类别

类是所有对象的起点,我们可以将它们视为创建对象的模板。类通常包含成员字段、成员方法和特殊的构造函数方法。

我们将使用构造函数创建类的对象:

public class Car {
 
    // member fields
    private String type;
    private String model;
    private String color;
    private int speed;
 
    // constructor
    public Car(String type, String model, String color) {
        this.type = type;
        this.model = model;
        this.color = color;
    }
     
    // member methods
    public int increaseSpeed(int increment) {
        this.speed = this.speed + increment;
        return this.speed;
    }
     
    // ...
}

请注意,一个类可能有多个构造函数。我们可以在类章节查看

3.对象

对象是从类创建的,称为类的实例。我们使用类的构造函数从类中创建对象:

Car veyron = new Car("Bugatti", "Veyron", "crimson");
Car corvette = new Car("Chevrolet", "Corvette", "black");

在这里,我们创建了类的两个实例 Car 在我们的 对象章节

4.摘要

抽象是隐藏实现的复杂性并公开更简单的接口。

如果我们考虑一台典型的计算机,我们只能看到外部接口,这是与它交互最重要的,而内部芯片和电路对用户来说是隐藏的。

在OOP中,抽象意味着隐藏程序的复杂实现细节,只公开使用实现所需的API。在Java中,我们通过使用接口和抽象类来实现抽象。

我们可以阅读关于 抽象类接口 的更多文章。

5.封装

封装是对API的使用者隐藏对象的状态或内部表示以及提供绑定到对象的公共可访问方法以进行读写访问。这允许隐藏特定信息并控制对内部实现的访问。

例如,类中的成员字段对其他类是隐藏的,可以使用成员方法访问它们。一种方法是将所有数据字段 private 并且只能通过使用 public 成员方法:

public class Car {

    // ...
    private int speed;

    public int getSpeed() {
        return color;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }
    // ...
}

这里,字段speed使用private修饰符修饰access,并且只能使用public getSpeed() 和 setSpeed()方法。我们可以在我们的 访问修饰符 章节

6.继承

继承是一种机制,它允许一个类通过继承另一个类来获取所有属性。 我们称继承类为子类,将继承类称为超类或父类。

在Java中,我们通过扩展父类来实现这一点。因此,子类从父类获取所有属性:

public class Car extends Vehicle { 
    //...
}

当我们扩展一个类时,我们形成一个 IS-A关系. 这个 Car 是A Vehicle.因此,它具有 Vehicle的属性及方法.

我们可以问这个问题, 为什么我们需要继承? 为了回答这个问题,让我们考虑一个制造不同类型车辆的车辆制造商,例如汽车、公共汽车、电车和卡车。

为了简化工作,我们可以将所有车辆类型的通用特性和属性绑定到一个模块中(在Java中是一个类)。我们可以让各个类型继承和重用这些属性:

public class Vehicle {
    private int wheels;
    private String model;
    public void start() {
        // the process of starting the vehicle
    }
    
    public void stop() {
        // process to stop the vehicle
    }
    
    public void honk() { 
        // produces a default honk 
    }

}

车辆类 Car现在将从父级继承 Vehicle 类别:

public class Car extends Vehicle {
    private int numberOfGears;

    public void openDoors() {
        // process to open the doors
    }
}

Java支持单继承和多级继承。这意味着一个类不能直接从多个类扩展,但它可以使用层次结构:

public class ArmoredCar extends Car {
    private boolean bulletProofWindows;
    
    public void remoteStartCar() {
        // this vehicle can be started by using a remote control
    }
}

这里 ArmogedCar 延伸 Car, Car 延伸 Vehicle, 所以 ArmogedCar 从两者继承属性 Car 和 Vehicle.

当我们从父类继承时,开发人员也可以重写父类的方法实现。 这被称为 方法覆盖.

在我们上面的示例中 Vehicle 类,有 honk()方法这个Car类扩展Vehicle类可以重写此方法并以其想要的方式实现:

public class Car extends Vehicle {  
    //...

    @Override
    public void honk() { 
        // produces car-specific honk 
    }
 }

请注意,这也称为运行时多态性,如下一节所述。我们可以在我们的 Java继承inheritance 和 composition 学习。

7.多态性

多态性 是OOP语言根据输入类型不同地处理数据的能力。在Java中,这可以是具有不同方法签名并执行不同功能的相同方法名:

public class TextFile extends GenericFile {
    //...
 
    public String read() {
        return this.getContent()
          .toString();
    }
 
    public String read(int limit) {
        return this.getContent()
          .toString()
          .substring(0, limit);
    }
 
    public String read(int start, int stop) {
        return this.getContent()
          .toString()
          .substring(start, stop);
    }
}

在这个例子中,我们可以看到方法 read()具有三种不同的形式,具有不同的功能。 这种类型的多态性是静态或编译时多态性,也称为 方法重载.

还有运行时或 动态多态性,其中子类重写父类的方法:

public class GenericFile {
    private String name;
 
    //...
 
    public String getFileInfo() {
        return "Generic File Impl";
    }
}

子类可以扩展GenericFile类并重写getFileInfo()方法:

public class ImageFile extends GenericFile {
    private int height;
    private int width;
 
    //... getters and setters
     
    public String getFileInfo() {
        return "Image File Impl";
    }
}

在我们的 Java中的多态性 文章

8.结论

在本文中,我们了解了Java OOP的基本概念。

本文中的代码示例可用 关于Github.

正文到此结束
本文目录