面向对象和基于对象的语言之间的核心差异 [英] Core difference between object oriented and object based language

查看:126
本文介绍了面向对象和基于对象的语言之间的核心差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道

面向对象语言和基于对象的语言之间的核心区别是什么

我看了很多帖子他们都说了两件事

I have read many post all of them are saying two things


  1. 面向对象语言支持所有OOP和基于对象的语言的功能不支持多态性和继承等OOP的所有功能。

  1. Object-oriented language supports all the features of OOPs and Object-based language doesn't support all the features of OOPs like Polymorphism and Inheritance.

他们给出了基于对象的javascript示例java as object oriented

They are giving example of javascript as object based and java as object oriented

喜欢这篇stackoverflow的帖子

Like this post of stackoverflow

面向对象和基于对象的语言之间的区别

但我想知道这两个概念之间的核心区别是什么,无论使用何种语言。

But I want to know what is the core difference between both the concept regardless of any language.

得到答案

终于得到了东西

感谢Matía s Fidemraizer

不依赖于任何语言的答案,不依赖于任何特征,我所嘲笑的核心差异是

Answer which is not dependent on any language, not dependent on any feature, the core differnce for which I am loooking that is

本身包含对象的语言称为基于对象的语言,具有以下面向对象概念的语言称为面向对象语言

推荐答案

JavaScript是一种面向原型的语言。

JavaScript is a prototype-oriented language.

它可以从构造函数构建实际对象,它几乎具有任何对象可能具有的任何功能:

It can build actual objects from a constructor function and it has almost any feature that any object could have:


  • 构造函数。

  • 方法(即JavaScript中的函数)。

  • 属性(因为ECMA-Script 5,getters / setters)。

  • 实例。

  • Constructor.
  • Methods (i.e. functions in JavaScript).
  • Properties (since ECMA-Script 5, "getters/setters").
  • Instances.

在JavaScript中,任何对象都有原型,包括功能。原型本身是将对象成员添加到整个对象的任何新创建实例的基本方法。

In JavaScript, any object has a prototype, including functions. The prototype itself is a rudimentary way of adding object members to any newly created instance of the whole object.

var constructor = function() { };
constructor.prototype.text = "hello world";

alert(new constructor().text); // This alerts hello world

为什么JavaScript不是面向对象的编程(脚本编写) )语言?因为它没有符合面向对象编程定义要求的功能:

Why JavaScript isn't an object-oriented programming (scripting) language? Because it has no feature that fits the requirements of the definition of object-oriented programming:


  • 多态性没有。您可以更改原型成员的行为,但这只是重用标识符。您无法在伪派生对象中访问该成员的先前实现。

  • 继承完全没有。也许原型链可能与继承相当,但JavaScript(ECMA-Script 5.x或更早版本)没有像其他基于OOP的语言那样的基于语法的继承(即Java,C#,Ruby,Python,VisualBasic.NET,... )。

  • 封装。是的,当然,但没有办法创建实际的私人或内部对象成员。

  • Polymorphism: No. You can change the behavior of a prototype member, but this is just reusing the identifier. You aren't able to access the previous implementation of the member in a pseudo-derived object.
  • Inheritance: Not at all. Maybe prototype chain might be comparable to inheritance but JavaScript (ECMA-Script 5.x or earlier versions) has no syntax-based inheritance like other OOP-based languages (i.e. Java, C#, Ruby, Python, VisualBasic.NET, ...).
  • Encapsulation. Yes, of course, but there's no way to create actual private or internal object members.

也许我忘了提一些其他细节,但我真的相信这是一个很好的总结。

Perhaps I forgot to mention some other detail, but I honestly believe that this is a good summary.

核心差异是一种面向对象的编程语言,具有面向对象范例必须具有的特性,以便被认为是面向对象的编程语言。 因此,JavaScript目前不是一种实际的面向对象编程语言,因为它缺乏实际的多态性和继承

The core difference is an object-oriented programming language has the features that an object-oriented paradigm must have in order to be considered an object-oriented programming language. Thus, JavaScript, for now, isn't an actual object-oriented programming language because it lacks actual polymorphism and inheritance.

从表面上看,ES2015及以上版本有一项重大改进,可以考虑一个不完全但更接近某个对象的情况 - 面向编程:将调用到超类的语法糖。

Esthetically speaking yes, ES2015 and above has a major improvement that let consider a not fully but more closer to an object-oriented programming: syntactic sugar to call to the super class.

例如:

class A {
     doStuff() {
         console.log("hello world");
     }
}

class B extends A {
     doStuff() {
         super.doStuff();
         console.log("...and goodbye!");
     }
}

这是多态。一个更专业的类可以覆盖它的基类,既可以完全改变函数行为,也可以执行基本已经做的操作,为函数添加新代码。

This is polymorphism. A more specialized class can override its base class to both completely change a function behavior or do what the base was already doing, adding new code to the function.

BTW,ES2015以上仍然缺乏真正的封装:这里的访问修饰符如 private public 在这里? 无处

BTW, ES2015 and above still lacks true encapsulation: where are access modifiers like private or public here? Nowhere.

而且,在一天结束时,ES2015及以上版本实现了基于类的OOP,但它仍然是顶级的语法糖层ECMAScript 5.x ...上面的代码仍然适用于引擎盖下的原型,它的工作方式与在ECMAScript 5.x中编码它的方式相同:

And, at the end of the day, ES2015 and above implement class-based OOP but it's still a syntactic sugar layer on top of ECMAScript 5.x... The above code still works with prototypes under the hoods and it works the same way as if you would code it in ECMAScript 5.x:

function A() {
}

A.prototype.doStuff = function() {
    console.log("hello world");
};

function B() {
}

B.prototype = Object.create(A.prototype);
B.prototype.doStuff = function() {
    A.prototype.doStuff.call(this);
    console.log("...and goodbye!");
};

我们希望我再次更新这个答案,因为ES2020已经提出了访问修饰符而且我们'我将能够考虑JavaScript另一种完全支持面向对象编程的语言!

Let's hope I'll need to update this answer again because ES2020 has already proposed access modifiers and we'll be able to consider JavaScript another language which fully-supports object-oriented programming!

这篇关于面向对象和基于对象的语言之间的核心差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆