Java 面向对象语言和非面向对象语言有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1894788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:38:46  来源:igfitidea点击:

What is the difference between object-oriented languages and non object-oriented languages?

javacoop

提问by azn_person

I have been hearing about how C is a non-object-oriented language and how java is an object-oriented language. I was wondering what the difference was?

我一直听说 C 是一种非面向对象的语言,而 java 是一种面向对象的语言。我想知道有什么区别?

采纳答案by Mark S

Wow, a lot of big OOP terms being thrown around to this guy. Being one who started in procedural programming and is now mostly doing OOP, this is conceptually how I think of the difference (without all the big terms):

哇,很多大的 OOP 术语都被扔给了这个家伙。作为一个从过程编程开始并且现在主要从事 OOP 的人,这就是我在概念上对差异的看法(没有所有重要的术语):

In C, you have things called structs that can hold state. They kind of seem like objects, for example you could have a struct called Car and create instances of Cars and set its make, model, and color fields. However, you cannot tell the Car struct instances to do anything. Instead, if you want to wash your car, you have to pass the car instance to some external function like this:

在 C 中,你有一些叫做结构的东西可以保存状态。它们有点像对象,例如,您可以有一个名为 Car 的结构并创建 Cars 的实例并设置其品牌、型号和颜色字段。但是,您不能告诉 Car 结构实例执行任何操作。相反,如果你想洗车,你必须将汽车实例传递给一些外部函数,如下所示:

WashMyCar(myCar);

OOP languages use a different concept from structs called Classes, and objects are instances of those classes. Forget about those big words inheritance and polymorphism for now (those are more advanced topics for once you kind of get Classes). Just think of the example of a car. In Java, for example, you could define a class called Car as such:

OOP 语言使用与称为类的结构不同的概念,对象是这些类的实例。暂时忘记那些大词继承和多态性(一旦你获得类,这些是更高级的主题)。想想汽车的例子。例如,在 Java 中,您可以定义一个名为 Car 的类,如下所示:

public class Car {
  String make;
  String model;
  String color;
}

Then, you make an instance of a car like so:

然后,你像这样创建一个汽车实例:

Car myCar = new Car();
myCar.make = "Honda";
myCar.model = "Accord";
myCar.color = "Black";

This is real similar to a struct. Now, what makes OOP different is that you can expand the Class definition to define class methods - which are similar to functions in procedural except that they always operate on an object. So, let's add the wash method:

这与结构非常相似。现在,OOP 的不同之处在于您可以扩展类定义来定义类方法——这与过程中的函数类似,只是它们总是对对象进行操作。所以,让我们添加wash方法:

public class Car {
  String make;
  String model;
  String color;
  String condition;
  void washMe() {
    this.condition = "clean";
  }
  void goOffroad() {
    this.condition = "dirty";
  }
}

Now you can do this:

现在你可以这样做:

Car myCar = new Car();
myCar.make = "Honda";
myCar.model = "Accord";
myCar.color = "Black";

myCar.goOffroad();
System.out.println(myCar.condition); // dirty
myCar.washMe();
System.out.println(myCar.condition); // clean

Hopefully this example helps. There is, of course, much more to OOP (and procedural) than this simple example. But the core difference is in having classes of objects that "own" their own methods.

希望这个例子有帮助。当然,OOP(和过程)比这个简单的例子要多得多。但核心区别在于拥有“拥有”自己方法的对象类。

回答by Pavel Radzivilovsky

OO languages make it easier to write object oriented programs, by offering you constructs to make data abstraction and polymorphism easy to implement.

OO 语言通过提供构造使数据抽象和多态易于实现,从而使编写面向对象的程序变得更加容易。

回答by PJT

Java is a language mostly used for object oriented programming and C a language mostly used for procedural oriented programming

Java 是一种主要用于面向对象编程的语言,而 C 是一种主要用于面向过程编程的语言

There is a great explanation on Wikipedia about Procedural programming vs object oriented programming:

维基百科上有一个关于过程式编程与面向对象编程的很好的解释

Comparison with object-oriented programming The focus of procedural programming is to break down a programming task into a collection of variables, data structures, and subroutines, whereas in object-oriented programming it is to break down a programming task into objects with each "object" encapsulating its own data and methods (subroutines). The most important distinction is whereas procedural programming uses procedures to operate on data structures, object-oriented programming bundles the two together so an "object" operates on its "own" data structure. Nomenclature varies between the two, although they have much the same semantics: object-oriented procedural methods functions objects modules message argument attribute variable

与面向对象编程的比较过程编程的重点是将编程任务分解为变量、数据结构和子例程的集合,而在面向对象编程中,则是将编程任务分解为具有每个“对象”的对象。 " 封装自己的数据和方法(子程序)。最重要的区别是过程编程使用过程对数据结构进行操作,而面向对象编程将两者捆绑在一起,因此“对象”对其“自己的”数据结构进行操作。两者之间的命名法有所不同,尽管它们具有相同的语义:面向对象的过程方法函数对象模块消息参数属性变量

But to make things more basic you can think of objects like you think of objects in the real world such as a car would have all the attributes of a car as other objects like windows and tires.

但是为了让事情变得更基本,您可以像想象现实世界中的物体一样考虑物体,例如汽车,它具有汽车的所有属性,就像窗户和轮胎等其他物体一样。

回答by Fred

OOP makes it easier to break down one big problem into smaller self contained parts. You can then create something more complex by combining these parts.

OOP 可以更轻松地将一个大问题分解为更小的独立部分。然后,您可以通过组合这些部分来创建更复杂的东西。

回答by Christoph

The object-oriented programming paradigm tells you to encapsulate state variables in entities called 'objects' which communicate via message passing, most often implemented as functions with a 'special' thisor selfargument.

面向对象的编程范式告诉您将状态变量封装在称为“对象”的实体中,这些实体通过消息传递进行通信,通常实现为具有“特殊”thisself参数的函数。

An object-oriented programming language is a language designed to make using the oo paradigm easy. Its semantics and syntax are geared towards this goal. Inheritance (either class-based or prototypal) and subtype polymorphism are important techniques which make the abstract concept of oo feasible in practice.

面向对象的编程语言是一种旨在使 oo 范式的使用变得容易的语言。它的语义和句法是面向这个目标的。继承(基于类或原型)和子类型多态性是重要的技术,它们使 oo 的抽象概念在实践中可行。

回答by outis

Procedural programming and OOP, both different programming paradigms, are the proverbial apples and oranges. PP relies on "subroutines". OOP sends "messages" to "objects", which change the "state" of the objects and send messages to other objects; furthermore, the behavior objects can be extended, creating new types of objects. Both rely on assignment and side-effects. Problems may have natural solutions in one paradigm, but rather baroque solutions in another. That is, a problem may be readily modeled by using objects passing messages, or by procedures.

过程式编程和 OOP 这两种不同的编程范式,是众所周知的苹果和橘子。PP 依赖于“子程序”。OOP向“对象”发送“消息”,改变对象的“状态”,向其他对象发送消息;此外,行为对象可以扩展,创建新的对象类型。两者都依赖于赋值和副作用。问题在一种范式中可能有自然的解决方案,但在另一种范式中可能有巴洛克式的解决方案。也就是说,通过使用传递消息的对象或过程可以很容易地对问题建模。

Programming languages can support paradigms beyond ones they natively support, but it requires the programmer to implement the necessary structures. For example, you can do OOP with C; in fact, some OO extensions of C relied on preprocessors which produced C code as output.

编程语言可以支持超出其本机支持的范式,但它需要程序员实现必要的结构。例如,你可以用 C 来做 OOP;事实上,C 的一些 OO 扩展依赖于产生 C 代码作为输出的预处理器。

As you progress in your studies, you'll want to look over the other paradigms, especially functional programming. It wouldn't hurt to look at logic programming(as exemplified by Prolog) and dataflow programming(see Pure Datafor an example).

随着学习的进展,您会想要查看其他范式,尤其是函数式编程。看看逻辑编程(以 Prolog 为例)和数据流编程(示例参见Pure Data)不会有什么坏处。

回答by Chris Fulstow

Object oriented languages are built around the idea of data structures, called objects, that each contain their own data and behaviours. By combining these objects and having them interact with eachother you can design a program that better reflects the domain of the problem you're trying to solve.

面向对象的语言是围绕数据结构的思想构建的,称为对象,每个对象都包含自己的数据和行为。通过组合这些对象并让它们相互交互,您可以设计一个程序,以更好地反映您要解决的问题的领域。

For example, in a banking application you might have objects that represent accounts, which each store data like the balance and implement certain behaviours like transferring money.

例如,在银行应用程序中,您可能有代表帐户的对象,每个对象都存储余额等数据并实现某些行为,例如转账。

In a more traditional language, you would keep data and behaviour in different places.

在更传统的语言中,您会将数据和行为保存在不同的地方。

回答by Yuval Adam

I'll give you the most simple and intuitive answer you can get.

我会给你你能得到的最简单直观的答案。

(Considering Javais an object-oriented languageand Cis a procedural language)

(考虑到Java面向对象的语言C过程语言

Object-Oriented Language

面向对象语言

  • core concept is objects(consider a car)
  • objects have properties that define them, they can be constant or changing (a car is red, 2004 model, and has cruise control. It also currently has 100K on the odometer and 3/4 tank fuel)
  • objects can be sent messages which affect it (a car can be driven from A to B, a car can be refueled)
  • 核心概念是对象(考虑汽车)
  • 对象具有定义它们的属性,它们可以是恒定的或变化的(一辆汽车是红色的,2004 年型号,并具有巡航控制。它目前还有 100K 里程表和 3/4 油箱燃料)
  • 可以向对象发送影响它的消息(汽车可以从 A 开到 B,汽车可以加油)

Procedural Language

程序语言

  • core concept is process(consider placing on online order)
  • for a process to complete it must go through several steps (an order must be (1) sent to warehouse, (2) boxed, and (3) shipped)
  • one of the steps might be a process by itself, or it can be atomic
  • 核心概念是流程(考虑在线下订单)
  • 要完成一个流程,它必须经过几个步骤(订单必须(1)发送到仓库,(2)装箱,以及(3)发货)
  • 其中一个步骤本身可能是一个过程,也可以是原子的

回答by seh

Apart from trying to characterize a language as having a particular paradigm at its core, you might find this paper from William R. Cook useful: On understanding data abstraction, revisited. There's a draft versionavailable. It explores the difference between abstract data typesand objects-- a distinction that, after years of absorption in various programming languages, I could no longer see.

除了试图将语言描述为以特定范式为核心的语言之外,您可能会发现 William R. Cook 的这篇论文很有用:关于理解数据抽象,重新访问. 有一个草稿版本可用。它探讨了抽象数据类型对象之间的区别——在我对各种编程语言的多年吸收之后,我再也看不到这种区别。

I found the paper by way of Guy Steele's recent essay, Why Object-Oriented Languages Need Tail Calls.

我通过 Guy Steele 最近的文章“为什么面向对象语言需要尾调用”找到了这篇论文。

回答by fastcodejava

Object-Oriented Languageis where you think in terms of objects. However, this takes lot of skills. You cannot take some C-code and stuff it into an object and call it OOP. Key concepts of OOP are inheritance, encapsulation and dynamic binding.

面向对象语言是您根据对象进行思考的地方。然而,这需要很多技巧。你不能把一些 C 代码塞进一个对象并称之为 OOP。OOP 的关键概念是继承、封装和动态绑定。