java 类、对象、实体:有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14161753/
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-10-31 15:17:57  来源:igfitidea点击:

Class, Object, Entity: What's the difference?

javaterminology

提问by jmrjulian

I also see other terms as well: Entity Object, Value Object, etc. Are there other terms out there that I should know, and what do these terms refer to?

我还看到其他术语:实体对象、值对象等。是否还有其他我应该知道的术语,这些术语指的是什么?

Can the differences between them, if any, be identified by reading code?

它们之间的差异(如果有)可以通过阅读代码来识别吗?

回答by Nathan Hughes

A class is a template for creating objects. Not all OO languages use classes (see Self, Javascript). Typically classes are implemented as objects.

类是用于创建对象的模板。并非所有 OO 语言都使用类(请参阅 Self、Javascript)。通常,类被实现为对象。

An object is a bundle of data that is packaged with functions that act on that data (called methods). Calling a class's constructor allocates memory for the object and initializes its member variables.

对象是一组数据,其中包含对这些数据起作用的函数(称为方法)。调用类的构造函数为对象分配内存并初始化其成员变量。

An entity is an object that represents something that has an identity that the system is interested in tracking. Typical examples are Customers and Accounts.

实体是一个对象,它代表具有系统有兴趣跟踪的身份的事物。典型的例子是客户和帐户。

A value object is a value, it doesn't have an identity, and two instances with the same value are considered to be identical. Typical examples are monetary amounts, locations, payment types.

值对象是一个值,它没有身份,具有相同值的两个实例被认为是相同的。典型的例子是货币金额、地点、支付类型。

A data transfer object is used for passing a bunch of data around. Typically they're used in distributed systems to send data as a bundle in order to avoid repeated network calls. Data transfer objects have no identity (or there is no expectation they should have any), they are just containers for data.

数据传输对象用于传递一堆数据。通常,它们在分布式系统中用于将数据作为一个包发送,以避免重复的网络调用。数据传输对象没有身份(或者不期望它们应该有身份),它们只是数据的容器。

Generally you can tell the difference between entities and value objects because entities have a recognizable identity, and the system is concerned with creating them, storing them, and changing them. In cases where objects map to some database, entities have primary keys that are either some kind of composite natural key or an artificial key, while value objects are compared by value.

通常,您可以区分实体和值对象,因为实体具有可识别的身份,而系统关注的是创建、存储和更改它们。在对象映射到某个数据库的情况下,实体的主键是某种复合自然键或人工键,而值对象按值进行比较。

回答by Mr Zorn

In general a class is a construct which defines a set of properties and methods/functions while an Object is the actual instance of a class which is created at runtime.

通常,类是定义一组属性和方法/函数的构造,而对象是在运行时创建的类的实际实例。

Sample class definition:

示例类定义:

public class Example{

...

}

The following will create an instance of the Example class as an Object at runtime;

下面将在运行时创建一个 Example 类的实例作为一个对象;

new Example();

回答by Thaycacac

  • Class defines an entity, while an object is the actualentity
  • Class is a conceptual modelthat defines allthecharacteristics and actions required of an object, whilean object is a real model
  • Class is a prototype of an object
  • All objects belonging to the same class have the samecharacteristics and actions
  • 类定义了一个实体,而一个对象是实际实体
  • 类是一个概念模型,它定义了一个对象所需的所有特征和动作,而一个对象是一个真实的模型
  • 类是对象的原型
  • 属于同一类的所有对象具有相同的特征和动作