Java 简明教程
Java - OOPs (Object-Oriented Programming) Concepts
OOPs (Object-Oriented Programming System)
对象是指现实世界实体,例如手机、书、桌子、计算机、手表等。面向对象编程是一种使用类和对象来设计程序的方法或范例。通过提供一些概念,它简化了软件开发和维护。
Object means a real-world entity such as a mobile, book, table, computer, watch, etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies software development and maintenance by providing some concepts.
在本教程中,我们将了解面向对象编程系统 Java (OOPs) 的概念。
In this tutorial, we will learn about the concepts of Java (OOPs) object-oriented programming systems.
Java OOPs (Object-Oriented Programming) Concepts
1. Object
在面向对象编程中,对象是一个实体,它具有两个特征(状态和行为)。一些现实世界中的对象是书、手机、表、电脑等。一个对象是类型类的 variable ,它是一个面向对象编程系统的基本组件。一个类具有 methods 和 data members (属性),这些方法和数据成员是通过一个对象访问的。因此,一个对象是一个类的实例。
In object-oriented programming, an object is an entity that has two characteristics (states and behavior). Some of the real-world objects are book, mobile, table, computer, etc. An object is a variable of the type class, it is a basic component of an object-oriented programming system. A class has the methods and data members (attributes), these methods and data members are accessed through an object. Thus, an object is an instance of a class.
2. Class
在面向对象编程中,一个类是一个蓝图,从中创建单独的对象(或者我们可以说一个类是对象类型的 data type )。在 Java 中,所有内容都与类和对象相关。每个类都有其方法和属性,可以通过对象访问和操作。
In object-oriented programming, a class is a blueprint from which individual objects are created (or, we can say a class is a data type of an object type). In Java, everything is related to classes and objects. Each class has its methods and attributes that can be accessed and manipulated through the objects.
3. Inheritance
在面向对象编程中,继承是一个过程,通过该过程我们可以复用现有类的功能来创建新类。在继承的概念中,有基 (父) 类和派生 (子) 类这两个术语。当一个类从另一个类(基类)继承时,它(派生类)将获得基类的所有属性和行为。
In object-oriented programming, inheritance is a process by which we can reuse the functionalities of existing classes to new classes. In the concept of inheritance, there are two terms base (parent) class and derived (child) class. When a class is inherited from another class (base class), it (derived class) obtains all the properties and behaviors of the base class.
4. Polymorphism
术语“多态性”意味着“多种形式”。在面向对象编程中,当你想为单个实体创建具有相同名称的多重形式时,多态性就很有用。为了在 Java 中实现多态性,我们使用两种概念 method overloading 和 method overriding 。
The term "polymorphism" means "many forms". In object-oriented programming, polymorphism is useful when you want to create multiple forms with the same name of a single entity. To implement polymorphism in Java, we use two concepts method overloading and method overriding.
方法重载在同一个类中执行,其中我们有具有相同名称但参数不同的多个方法,而方法覆盖是通过继承执行,其中我们可以在父类和子类中具有多个同名方法。
The method overloading is performed in the same class where we have multiple methods with the same name but different parameters, whereas, the method overriding is performed by using the inheritance where we can have multiple methods with the same name in parent and child classes.
5. Abstraction
在面向对象编程中,抽象是一种隐藏内部细节并显示功能的技术。抽象类和接口用于在 Java 中实现抽象。
In object-oriented programming, an abstraction is a technique of hiding internal details and showing functionalities. The abstract classes and interfaces are used to achieve abstraction in Java.
抽象的一个现实世界示例是汽车,内部细节(例如发动机、启动汽车的过程、换挡的过程等)对用户隐藏,并且诸如启动按钮、齿轮、显示器、制动等特性会提供给用户。当我们对这些功能执行任何操作时,就会执行内部过程。
The real-world example of an abstraction is a Car, the internal details such as the engine, process of starting a car, process of shifting gears, etc. are hidden from the user, and features such as the start button, gears, display, break, etc are given to the user. When we perform any action on these features, the internal process works.
6. Encapsulation
在面向对象的方法中,封装是一个将数据成员(属性)和方法捆绑在一起的过程。封装限制了对重要数据的直接访问。封装概念的最佳示例是制作一个类,其中数据成员是私有的,而方法是公有的,可以通过对象访问。在这种情况下,只有方法能访问那些私有数据。
In an object-oriented approach, encapsulation is a process of binding the data members (attributes) and methods together. The encapsulation restricts direct access to important data. The best example of the encapsulation concept is making a class where the data members are private and methods are public to access through an object. In this case, only methods can access those private data.
Advantages of Java OOPs
在 Java 中使用面向对象的方法有以下好处:
The following are the advantages of using the OOPs in Java:
-
The implementations of OOPs concepts are easier.
-
The execution of the OOPs is faster than procedural-oriented programming.
-
OOPs provide code reusability so that a programmer can reuse an existing code.
-
OOPs help us to keep the important data hidden.