kernel oops_Java中的OOPS概念– OOPS概念示例-程序员宅基地

技术标签: python  java  编程语言  多态  设计模式  

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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cunchi4221/article/details/107478899

智能推荐

解决win10/win8/8.1 64位操作系统MT65xx preloader线刷驱动无法安装_mt65驱动-程序员宅基地

文章浏览阅读1.3w次。转载自 http://www.miui.com/thread-2003672-1-1.html 当手机在刷错包或者误修改删除系统文件后会出现无法开机或者是移动定制(联通合约机)版想刷标准版,这时就会用到线刷,首先就是安装线刷驱动。 在XP和win7上线刷是比较方便的,用那个驱动自动安装版,直接就可以安装好,完成线刷。不过现在也有好多机友换成了win8/8.1系统,再使用这个_mt65驱动

SonarQube简介及客户端集成_sonar的客户端区别-程序员宅基地

文章浏览阅读1k次。SonarQube是一个代码质量管理平台,可以扫描监测代码并给出质量评价及修改建议,通过插件机制支持25+中开发语言,可以很容易与gradle\maven\jenkins等工具进行集成,是非常流行的代码质量管控平台。通CheckStyle、findbugs等工具定位不同,SonarQube定位于平台,有完善的管理机制及强大的管理页面,并通过插件支持checkstyle及findbugs等既有的流..._sonar的客户端区别

元学习系列(六):神经图灵机详细分析_神经图灵机方法改进-程序员宅基地

文章浏览阅读3.4k次,点赞2次,收藏27次。神经图灵机是LSTM、GRU的改进版本,本质上依然包含一个外部记忆结构、可对记忆进行读写操作,主要针对读写操作进行了改进,或者说提出了一种新的读写操作思路。神经图灵机之所以叫这个名字是因为它通过深度学习模型模拟了图灵机,但是我觉得如果先去介绍图灵机的概念,就会搞得很混乱,所以这里主要从神经图灵机改进了LSTM的哪些方面入手进行讲解,同时,由于模型的结构比较复杂,为了让思路更清晰,这次也会分开几..._神经图灵机方法改进

【机器学习】机器学习模型迭代方法(Python)-程序员宅基地

文章浏览阅读2.8k次。一、模型迭代方法机器学习模型在实际应用的场景,通常要根据新增的数据下进行模型的迭代,常见的模型迭代方法有以下几种:1、全量数据重新训练一个模型,直接合并历史训练数据与新增的数据,模型直接离线学习全量数据,学习得到一个全新的模型。优缺点:这也是实际最为常见的模型迭代方式,通常模型效果也是最好的,但这样模型迭代比较耗时,资源耗费比较多,实时性较差,特别是在大数据场景更为困难;2、模型融合的方法,将旧模..._模型迭代

base64图片打成Zip包上传,以及服务端解压的简单实现_base64可以装换zip吗-程序员宅基地

文章浏览阅读2.3k次。1、前言上传图片一般采用异步上传的方式,但是异步上传带来不好的地方,就如果图片有改变或者删除,图片服务器端就会造成浪费。所以有时候就会和参数同步提交。笔者喜欢base64图片一起上传,但是图片过多时就会出现数据丢失等异常。因为tomcat的post请求默认是2M的长度限制。2、解决办法有两种:① 修改tomcat的servel.xml的配置文件,设置 maxPostSize=..._base64可以装换zip吗

Opencv自然场景文本识别系统(源码&教程)_opencv自然场景实时识别文字-程序员宅基地

文章浏览阅读1k次,点赞17次,收藏22次。Opencv自然场景文本识别系统(源码&教程)_opencv自然场景实时识别文字

随便推点

ESXi 快速复制虚拟机脚本_exsi6.7快速克隆centos-程序员宅基地

文章浏览阅读1.3k次。拷贝虚拟机文件时间比较长,因为虚拟机 flat 文件很大,所以要等。脚本完成后,以复制虚拟机文件夹。将以下脚本内容写入文件。_exsi6.7快速克隆centos

好友推荐—基于关系的java和spark代码实现_本关任务:使用 spark core 知识完成 " 好友推荐 " 的程序。-程序员宅基地

文章浏览阅读2k次。本文主要实现基于二度好友的推荐。数学公式参考于:http://blog.csdn.net/qq_14950717/article/details/52197565测试数据为自己随手画的关系图把图片整理成文本信息如下:a b c d e f yb c a f gc a b dd c a e h q re f h d af e a b gg h f bh e g i di j m n ..._本关任务:使用 spark core 知识完成 " 好友推荐 " 的程序。

南京大学-高级程序设计复习总结_南京大学高级程序设计-程序员宅基地

文章浏览阅读367次。南京大学高级程序设计期末复习总结,c++面向对象编程_南京大学高级程序设计

4.朴素贝叶斯分类器实现-matlab_朴素贝叶斯 matlab训练和测试输出-程序员宅基地

文章浏览阅读3.1k次,点赞2次,收藏12次。实现朴素贝叶斯分类器,并且根据李航《统计机器学习》第四章提供的数据训练与测试,结果与书中一致分别实现了朴素贝叶斯以及带有laplace平滑的朴素贝叶斯%书中例题实现朴素贝叶斯%特征1的取值集合A1=[1;2;3];%特征2的取值集合A2=[4;5;6];%S M LAValues={A1;A2};%Y的取值集合YValue=[-1;1];%数据集和T=[ 1,4,-1;..._朴素贝叶斯 matlab训练和测试输出

Markdown 文本换行_markdowntext 换行-程序员宅基地

文章浏览阅读1.6k次。Markdown 文本换行_markdowntext 换行

错误:0xC0000022 在运行 Microsoft Windows 非核心版本的计算机上,运行”slui.exe 0x2a 0xC0000022″以显示错误文本_错误: 0xc0000022 在运行 microsoft windows 非核心版本的计算机上,运行-程序员宅基地

文章浏览阅读6.7w次,点赞2次,收藏37次。win10 2016长期服务版激活错误解决方法:打开“注册表编辑器”;(Windows + R然后输入Regedit)修改SkipRearm的值为1:(在HKEY_LOCAL_MACHINE–》SOFTWARE–》Microsoft–》Windows NT–》CurrentVersion–》SoftwareProtectionPlatform里面,将SkipRearm的值修改为1)重..._错误: 0xc0000022 在运行 microsoft windows 非核心版本的计算机上,运行“slui.ex