Java 类、对象和实例的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1215881/
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
The difference between Classes, Objects, and Instances
提问by Pranjut
What is a class, an object and an instance in Java?
Java 中的类、对象和实例是什么?
采纳答案by Stephen C
Java (and any other programming language) is modeled in terms of typesand values. At the theoretical level, a valueis a representation for some quantum of information, and a typeis a set of values. When we say value X is an instanceof type Y, we are simply saying that X is a member of the set of values that is the type Y.
Java(和任何其他编程语言)是根据类型和值建模的。在理论层面,值是某种信息量的表示,类型是一组值。当我们说值 X是类型 Y的实例时,我们只是说 X 是类型 Y 的值集的成员。
So that's what the term "instance" really means: it describes a relationship not a thing.
所以这就是术语“实例”的真正含义:它描述的是关系而不是事物。
The type system of the Java programming language supports two kinds of types, primitive typesand reference types. The reference types are further divided into the classesand array types. A Java objectis an instance of a reference type.
Java 编程语言的类型系统支持两种类型,原始类型和引用类型。引用类型进一步分为类和数组类型。Java对象是引用类型的实例。
An object is a class instance or an array. (JLS 4.3.1)
一个对象是一个类实例或一个数组。( JLS 4.3.1)
That's the type theoretic view.
这就是类型论的观点。
In practice, most Java developers treat the words "instance" and "object" as synonyms. (And that includes me then I'm trying to explain something quickly.) And most developers use the word "value" rather than "instance" to refer to an instance of a primitive type.
在实践中,大多数 Java 开发人员将“实例”和“对象”这两个词视为同义词。(这包括我,然后我试图快速解释一些东西。)大多数开发人员使用“值”而不是“实例”这个词来指代原始类型的实例。
回答by mustafabar
A class is basically a definition, and contains the object's code. An object is an instance of a class
类基本上是一个定义,包含对象的代码。对象是类的实例
for example if you say
例如,如果你说
String word = new String();
the class is the String class, which describes the object (instance) word.
该类是String类,它描述了对象(实例)词。
When a class is declared, no memory is allocated so class is just a template.
当一个类被声明时,没有分配内存,所以类只是一个模板。
When the object of the class is declared, memory is allocated.
当类的对象被声明时,内存被分配。
回答by Rune FS
If you have a program that models cars you have a class to represent cars, so in Code you could say:
如果你有一个对汽车建模的程序,你有一个类来表示汽车,所以在代码中你可以说:
Car someCar = new Car();
someCar is now an instance of the class Car. If the program is used at a repairshop and the someCar represents your car in their system, then your car is the object.
someCar 现在是 Car 类的一个实例。如果该程序用于维修店并且 someCar 在他们的系统中代表您的汽车,那么您的汽车就是对象。
So Car is a class that can represent any real world car someCar is an instance of the Car class and someCare represents onereal life object (your car)
所以 Car 是一个可以代表任何现实世界汽车的类 someCar 是 Car 类的实例而 someCare 代表一个现实生活中的对象(你的车)
however instance and object is very often used interchangably when it comes to discussing coding
然而,在讨论编码时,实例和对象经常互换使用
回答by Kage
Any kind of data your computer stores and processes is in its most basic representation a row of bits. The way those bits are interpreted is done through data types. Data types can be primitive or complex. Primitive data types are - for instance - int or double. They have a specific length and a specific way of being interpreted. In the case of an integer, usually the first bit is used for the sign, the others are used for the value.
您的计算机存储和处理的任何类型的数据在其最基本的表示中都是一行位。这些位的解释方式是通过数据类型完成的。数据类型可以是原始的或复杂的。原始数据类型是 - 例如 - int 或 double。它们有特定的长度和特定的解释方式。在整数的情况下,通常第一位用于符号,其他位用于值。
Complex data types can be combinations of primitive and other complex data types and are called "Class" in Java.
复杂数据类型可以是原始数据类型和其他复杂数据类型的组合,在 Java 中称为“类”。
You can define the complex data type PeopleName consisting of two Strings called first and last name. Each String in Java is another complex data type. Strings in return are (probably) implemented using the primitive data type char for which Java knows how many bits they take to store and how to interpret them.
您可以定义复杂的数据类型 PeopleName,它由两个名为名字和姓氏的字符串组成。Java 中的每个 String 都是另一种复杂的数据类型。返回的字符串(可能)是使用原始数据类型 char 实现的,Java 知道它们需要存储多少位以及如何解释它们。
When you create an instance of a data type, you get an object and your computers reserves some memory for it and remembers its location and the name of that instance. An instance of PeopleName in memory will take up the space of the two String variables plus a bit more for bookkeeping. An integer takes up 32 bits in Java.
当你创建一个数据类型的实例时,你会得到一个对象,你的计算机会为它保留一些内存并记住它的位置和那个实例的名称。内存中的 PeopleName 实例将占用两个 String 变量的空间,再加上一些用于簿记的空间。Java 中一个整数占 32 位。
Complex data types can have methods assigned to them. Methods can perform actions on their arguments or on the instance of the data type you call this method from. If you have two instances of PeopleName called p1 and p2 and you call a method p1.getFirstName(), it usually returns the first name of the first person but not the second person's.
复杂数据类型可以分配有方法。方法可以对其参数或调用此方法的数据类型的实例执行操作。如果您有两个名为 p1 和 p2 的 PeopleName 实例,并且您调用了一个方法 p1.getFirstName(),它通常会返回第一个人的名字,而不是第二个人的名字。
回答by Jesper
A classis a blueprint which you use to create objects. An object is an instanceof a class - it's a concrete 'thing' that you made using a specific class. So, 'object' and 'instance' are the same thing, but the word 'instance' indicates the relationship of an object to its class.
一类是用来创建一个蓝图的对象。对象是类的实例——它是您使用特定类创建的具体“事物”。因此,“对象”和“实例”是同一个东西,但“实例”一词表示对象与其类的关系。
This is easy to understand if you look at an example. For example, suppose you have a class House
. Your own house is an object and is an instance of class House
. Your sister's house is another object (another instance of class House
).
如果你看一个例子,这很容易理解。例如,假设您有一个类House
。你自己的房子是一个对象,是 class 的一个实例House
。你姐姐的房子是另一个对象(类的另一个实例House
)。
// Class House describes what a house is
class House {
// ...
}
// You can use class House to create objects (instances of class House)
House myHouse = new House();
House sistersHouse = new House();
The class House
describes the concept of what a house is, and there are specific, concrete houses which are objects and instances of class House
.
类House
描述了房屋的概念,并且有具体的具体房屋,它们是类的对象和实例House
。
Note: This is exactly the same in Java as in all object oriented programming languages.
注意:这在 Java 中与在所有面向对象的编程语言中完全相同。
回答by SwatiKothari
Class is Data Type,You use this type to create object.
类是数据类型,您使用此类型来创建对象。
Instance is Logical but object is Physical means occupies some memory.
We can create an instance for abstract class as well as for interface, but we cannot create an
object for those.Object is instance of class and instance means representative of class i.e object.
Instance refers to Reference of an object.
Object is actually pointing to memory address of that instance.
You can't pass instance over the layers but you can pass the object over the layers
You can't store an instance but you can store an object
A single object can have more than one instance.
Instance will have the both class definition and the object definition where as in object it will have only the object definition.
实例是逻辑但对象是物理意味着占用一些内存。
我们可以为抽象类和接口创建实例,但我们不能
为它们创建对象。对象是类的实例,实例表示类即对象的代表。
实例是指对象的引用。
对象实际上是指向该实例的内存地址。
您不能在图层上传递实例,但可以在图层上传递对象
你不能存储一个实例,但你可以存储一个对象
一个对象可以有多个实例。
实例将同时具有类定义和对象定义,而在对象中它将只有对象定义。
Syntax of Object:
对象的语法:
Classname var=new Classname();
But for instance creation it returns only a pointer refering to an object, syntax is :
但是例如创建它只返回一个指向一个对象的指针,语法是:
Classname varname;
回答by Arun
In java, the objects are spawned on heap memory. These require reference to be pointed and used in our application. The reference has the memory location of the object with which we can use the objects in our application. A reference in short is nothing but a name of the variable which stores the address of the object instantiated on a memory location.
在 Java 中,对象是在堆内存上生成的。这些需要在我们的应用程序中指出和使用的引用。引用具有对象的内存位置,我们可以在应用程序中使用该对象。简而言之,引用只不过是存储在内存位置上实例化的对象地址的变量的名称。
An instance
is a general term for object
. FYI, Object
is a class.
Aninstance
是 的总称object
。仅供参考,Object
是一个类。
For Example,
例如,
Class A{
}
A ref = new A();
For the above code snippet, ref is the reference for an object of class A generated on heap.
对于上面的代码片段,ref 是在堆上生成的类 A 对象的引用。
回答by user2390183
I like Jesper's explanation in layman terms
我喜欢 Jesper 通俗易懂的解释
By improvising examples from Jesper's answer,
通过即兴创作 Jesper 回答中的例子,
class House {
// blue print for House Objects
}
class Car {
// blue print for Instances of Class Car
}
House myHouse = new House();
Car myCar = new Car();
myHouse and myCar are objects
myHouse 和 myCar 是对象
myHouse is an instance of House (relates Object-myHouse to its Class-House) myCar is an instance of Car
myHouse 是 House 的一个实例(将 Object-myHouse 与其类-House 相关联) myCar 是 Car 的一个实例
in short
简而言之
"myHouse is an instanceof ClassHouse" which is same as saying "myHouse is an Objectof typeHouse"
“的MyHouse是一个实例的类屋”,这是等于说“的MyHouse是对象的类型屋”
回答by filip_j
The concept behind classes and objects is to encapsulate logic into single programming unit. Classesare the blueprints of which objects are created.
类和对象背后的概念是将逻辑封装到单个编程单元中。类是创建对象的蓝图。
Here an example of a class representing a Car:
这是一个表示 Car 的类的示例:
public class Car {
int currentSpeed;
String name;
public void accelerate() {
}
public void park() {
}
public void printCurrentSpeed() {
}
}
You can create instancesof the objectCar like this:
您可以创建实例中的对象这样的车:
Car audi = new Car();
Car toyota = new Car();
I have taken the example from this tutorial
我已经从本教程中获取了示例
回答by Gaurav Kathuria
The definition "Object is an instance of a class", is conceptually wrong, but correct as per implementation. Actually the object oriented features are taken from the real life, for focusing the mind of programmer from more to less. In real life classes are designed to manage the object.For eg- we human beings have a caste, religion,nationality and much more. These casts, religion, nationality are the classes and have no existence without human beings. But in implementation there is no existence of objects without classes. Object- Object is an discrete entity having some well defined attribute. Here discrete mean something that makes it unique from other. Well defined attribute make sense in some context. Class- Classification of object having some common behaviour or objects of some common type.
“对象是类的实例”的定义在概念上是错误的,但根据实现是正确的。其实面向对象的特性取材于现实生活,是为了让程序员的思维由多变少。在现实生活中,课程旨在管理对象。例如,我们人类有种姓、宗教、国籍等等。这些演员、宗教、国籍都是阶级,没有人类就没有存在。但是在实现中,不存在没有类的对象。对象 - 对象是具有一些明确定义的属性的离散实体。这里的离散意味着使它与众不同的东西。在某些情况下,定义明确的属性是有意义的。类 - 具有某些共同行为的对象或某些共同类型的对象的分类。