kernel oops_Java中的OOPS概念– OOPS概念示例

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.

面向对象的编程模型围绕对象的概念展开。

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.

对象是类的实例。 它包含属性和功能。 它们就像现实世界中的对象。 例如,您的汽车,房屋,笔记本电脑等都是对象。 它们具有一些特定的属性和方法来执行某些操作。

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:

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.

抽象是隐藏内部细节并用简单术语描述事物的概念。 例如,一个将两个整数相加的方法。 该方法的内部处理对于外部世界是隐藏的。 在面向对象的程序设计中有很多方法可以实现抽象,例如封装和继承。

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负责将简单的语句转换为机器语言,并从外部隐藏了内部实现细节。

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.

访问修饰符关键字用于面向对象编程中的封装。 例如,使用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.");
	}
}

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{

	@Override
	public void draw(){
		System.out.println("Drwaing circle");
	}

}
package com.journaldev.test;

public class Square implements Shape {

	@Override
	public 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”关系中包含的对象不能单独存在时,则属于组合情况。 例如,房屋有一个房间。 在这里,没有房子就不可能存在房间。 据说组合比继承更好,请参阅《 组合与继承》

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值