java callable接口和runnable,kernel oops_Java中的OOPS概念– OOPS概念示例

 2023-11-19 阅读 23 评论 0

摘要:kernel oopsObject-Oriented Programming Concepts are very important for programming. Without having an idea about OOPS concepts, you will not be able to design systems in the object-oriented programming model. 面向對象的編程概念對于編程非常重要。 如果不了解

kernel oops

Object-Oriented Programming Concepts are very important for programming. Without having an idea about OOPS concepts, you will not be able to design systems in the object-oriented programming model.

面向對象的編程概念對于編程非常重要。 如果不了解OOPS概念,您將無法在面向對象的編程模型中設計系統。

什么是面向對象的編程模型? (What is Object-Oriented Programming Model?)

The object-oriented programming model revolves around the concept of Objects.

java callable接口和runnable、 面向對象的編程模型圍繞對象的概念展開。

What is an Object?

什么是對象?

An object is an instance of a Class. It contains properties and functions. They are like real-world objects. For example, your car, house, laptop, etc. are all objects. They have some specific properties and methods to perform some action.

對象是類的實例。 它包含屬性和功能。 它們就像現實世界中的對象。 例如,您的汽車,房屋,筆記本電腦等都是對象。 它們具有一些特定的屬性和方法來執行某些操作。

java.lang.reflect.undeclared、What is a Class?

什么是課程?

The Class defines the blueprint of Objects. They define the properties and functionalities of the objects. For example, Laptop is a class and your laptop is an instance of it.

該類定義了對象的藍圖。 它們定義了對象的屬性和功能。 例如,筆記本電腦是一個類,而筆記本電腦是它的一個實例。

OOPS概念 (OOPS Concepts)

Core OOPS concepts are:

substring java。 OOPS的核心概念是:

  1. Abstraction

    抽象化
  2. Encapsulation

    封裝形式
  3. Polymorphism

    多態性
  4. Inheritance

    遺產
  5. Association

    協會
  6. Aggregation

    聚合
  7. Composition

    組成

Let’s look into these object-oriented programming concepts one by one. We will use Java programming language for code examples so that you know how to implement OOPS concepts in Java.

讓我們逐一研究這些面向對象的編程概念。 我們將使用Java編程語言作為代碼示例,以便您知道如何在Java中實現OOPS概念。

1.抽象 (1. Abstraction)

Abstraction is the concept of hiding the internal details and describing things in simple terms. For example, a method that adds two integers. The internal processing of the method is hidden from the outer world. There are many ways to achieve abstraction in object-oriented programmings, such as encapsulation and inheritance.

抽象是隱藏內部細節并用簡單術語描述事物的概念。 例如,一個將兩個整數相加的方法。 該方法的內部處理對于外部世界是隱藏的。 在面向對象的程序設計中有很多方法可以實現抽象,例如封裝和繼承。

java.lang.throwable,A Java program is also a great example of abstraction. Here java takes care of converting simple statements to machine language and hides the inner implementation details from the outer world.

Java程序也是抽象的一個很好的例子。 在這里,java負責將簡單的語句轉換為機器語言,并從外部隱藏了內部實現細節。

What is Abstraction in OOPS?什么是OOPS中的抽象?

2.封裝 (2. Encapsulation)

Encapsulation is the technique used to implement abstraction in object-oriented programming. Encapsulation is used for access restriction to class members and methods.

封裝是用于在面向對象的程序設計中實現抽象的技術。 封裝用于限制類成員和方法的訪問。

Access modifier keywords are used for encapsulation in object oriented programming. For example, encapsulation in java is achieved using private, protected and public keywords.

java.lang.OutOfMemoryError、 訪問修飾符關鍵字用于面向對象編程中的封裝。 例如,使用privateprotectedpublic關鍵字實現java中的封裝。

3.多態性 (3. Polymorphism)

Polymorphism is the concept where an object behaves differently in different situations. There are two types of polymorphism – compile time polymorphism and runtime polymorphism.

多態是在不同情況下對象行為不同的概念。 多態有兩種類型-編譯時多態和運行時多態。

Compile-time polymorphism is achieved by method overloading. For example, we can have a class as below.

通過方法重載可以實現編譯時多態。 例如,我們可以有一個如下的類。

public class Circle {public void draw(){System.out.println("Drwaing circle with default color Black and diameter 1 cm.");}public void draw(int diameter){System.out.println("Drwaing circle with default color Black and diameter"+diameter+" cm.");}public void draw(int diameter, String color){System.out.println("Drwaing circle with color"+color+" and diameter"+diameter+" cm.");}
}

java的stringbuffer,Here we have multiple draw methods but they have different behavior. This is a case of method overloading because all the methods name is same and arguments are different. Here compiler will be able to identify the method to invoke at compile-time, hence it’s called compile-time polymorphism.

這里我們有多種draw方法,但是它們具有不同的行為。 這是方法重載的一種情況,因為所有方法的名稱都相同,而參數則不同。 在這里,編譯器將能夠識別在編譯時要調用的方法,因此它被稱為編譯時多態性。

Runtime polymorphism is implemented when we have an “IS-A” relationship between objects. This is also called a method overriding because the subclass has to override the superclass method for runtime polymorphism.

當對象之間具有“ IS-A”關系時,將實現運行時多態。 這也稱為方法重寫,因為子類必須重寫超類方法以實現運行時多態。

If we are working in terms of the superclass, the actual implementation class is decided at runtime. The compiler is not able to decide which class method will be invoked. This decision is done at runtime, hence the name as runtime polymorphism or dynamic method dispatch.

如果我們在超類方面工作,則實際的實現類在運行時確定。 編譯器無法決定將調用哪個類方法。 該決定是在運行時完成的,因此命名為運行時多態或動態方法分派。

package com.journaldev.test;public interface Shape {public void draw();
}
package com.journaldev.test;public class Circle implements Shape{@Overridepublic void draw(){System.out.println("Drwaing circle");}}
package com.journaldev.test;public class Square implements Shape {@Overridepublic void draw() {System.out.println("Drawing Square");}}

Shape is the superclass and there are two subclasses Circle and Square. Below is an example of runtime polymorphism.

Shape是超類, CircleSquare有兩個子類。 以下是運行時多態的示例。

Shape sh = new Circle();
sh.draw();Shape sh1 = getShape(); //some third party logic to determine shape
sh1.draw();

In the above examples, java compiler doesn’t know the actual implementation class of Shape that will be used at runtime, hence runtime polymorphism.

在上面的示例中,java編譯器不知道將在運行時使用的Shape的實際實現類,因此不知道運行時多態性。

4.繼承 (4. Inheritance)

Inheritance is the object-oriented programming concept where an object is based on another object. Inheritance is the mechanism of code reuse. The object that is getting inherited is called the superclass and the object that inherits the superclass is called a subclass.

繼承是一種面向對象的編程概念,其中一個對象基于另一個對象。 繼承是代碼重用的機制。 被繼承的對象稱為超類,而繼承超類的對象稱為子類。

We use extends keyword in java to implement inheritance. Below is a simple example of inheritance in java.

我們在Java中使用extends關鍵字來實現繼承。 以下是Java中繼承的簡單示例。

package com.journaldev.java.examples1;class SuperClassA {public void foo(){System.out.println("SuperClassA");}}class SubClassB extends SuperClassA{public void bar(){System.out.println("SubClassB");}}public class Test {public static void main(String args[]){SubClassB a = new SubClassB();a.foo();a.bar();}
}

5.協會 (5. Association)

Association is the OOPS concept to define the relationship between objects. The association defines the multiplicity between objects. For example Teacher and Student objects. There is a one-to-many relationship between a teacher and students. Similarly, a student can have a one-to-many relationship with teacher objects. However, both student and teacher objects are independent of each other.

關聯是定義對象之間關系的OOPS概念。 關聯定義對象之間的多重性。 例如,教師和學生對象。 師生之間存在一對多的關系。 同樣,學生可以與教師對象建立一對多關系。 但是,學生和教師對象都是彼此獨立的。

聚合 (Aggregation)

Aggregation is a special type of association. In aggregation, objects have their own life cycle but there is ownership. Whenever we have “HAS-A” relationship between objects and ownership then it’s a case of aggregation.

聚合是一種特殊的關聯類型。 在聚合中,對象有其自己的生命周期,但有所有權。 只要我們在對象和所有權之間具有“ HAS-A”關系,就屬于聚合情況。

6.組成 (6. Composition)

The composition is a special case of aggregation. The composition is a more restrictive form of aggregation. When the contained object in “HAS-A” relationship can’t exist on its own, then it’s a case of composition. For example, House has-a Room. Here the room can’t exist without the house. Composition is said to be better than inheritance, read more at Composition vs Inheritance.

組合是聚合的特例。 組合是限制更嚴格的聚合形式。 當“ HAS-A”關系中包含的對象不能單獨存在時,則屬于組合情況。 例如,房屋有一個房間。 在這里,沒有房子就不可能存在房間。 據說組合比繼承更好,請參閱《 組合與繼承》 。

Composition in JavaJava編寫

That’s all for a quick round-up on OOPS concepts.

這就是對OOPS概念的快速總結。

GitHub Repository.GitHub Repository中瀏覽更多Java示例程序。

References: https://docs.oracle.com/javase/tutorial/java/concepts/

參考: https : //docs.oracle.com/javase/tutorial/java/concepts/

翻譯自: https://www.journaldev.com/12496/oops-concepts-java-example

kernel oops

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/183073.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息