vb.net “实例化”和“初始化”有什么区别?

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

What is the difference between "instantiated" and "initialized"?

vb.netvariables

提问by l--''''''---------''''''''''''

I've been hearing these two words used in Microsoft tutorials for VB.NET. What is the difference between these two words when used in reference to variables?

我一直在微软的 VB.NET 教程中听到这两个词。当用于引用变量时,这两个词之间有什么区别?

回答by radarbob

Value vis-a-vis Reference Types

值相对于引用类型

Variablesin C# are in 1 of 2 groups. Valuetypes or Referencetypes. Types like intand DateTimeare valuetypes. In contrast, any class you create is a referencetype. C# strings are also a referencetype. Most things in the .NET framework are referencetypes.

C# 中的变量属于 2 组中的 1 组。类型或引用类型。像intDateTime这样的类型是类型。相反,您创建的任何类都是引用类型。C# 字符串也是一种引用类型。.NET 框架中的大多数东西都是引用类型。

Parts of a Variable

变量的组成部分

There is the variable nameand it's value. Two parts.

有变量和它的两部分

The variable's nameis what you declareit to be. The valueis what you assignto it.

变量的名称是您声明名称。该是你什么分配给它。

Variables are Initialized

变量被初始化

All variables are always given an initialvalue at the point the variable is declared. Thus all variables are initialized.

所有变量总是在声明变量时被赋予一个初始值。因此所有变量都被初始化

For valuetypes, like intthe compiler will give them a valid value if you do not do so explicitly. int's initializeto zero by default, DateTime's initializeto DateTime.MinValueby default.

对于类型,int如果您不明确这样做,编译器将给它们一个有效值。int初始化默认为零,DateTime初始化DateTime.MinValue默认。

Referencetype variables initializeto the object you give it. The compiler will not assignan object (i.e. a valid value) if you don't. In this case the value is null- nothing. So we say that the reference is initializedto null.

引用类型变量初始化为您提供的对象。如果您不这样做,编译器将不会分配对象(即有效值)。在这种情况下,价值是null- 没有。所以我们说引用被初始化为null。

Objects are Instantiated

对象被实例化

Humans are born. Objects are instantiated. A baby is an instanceof a Human, an object is an instanceof some Class.

人类诞生了。对象被实例化。婴儿是人类的实例,对象是某个类的实例

The act of creating an instanceof a Class is called instantiation(Ta-Da!)

创建类实例的行为称为实例化(Ta-Da!)

So declare, initialize, and instantiatecome together like this

所以声明初始化实例化像这样结合在一起

MyClass myClassyReference = new MyClass();

In the above, it is wrong to say "... creating an instance of an object..."

在上面,说“......创建一个对象的实例......”是错误的



edit - inspired by comments discussion

编辑 - 受评论讨论启发

Three distinct things are going on (above) using distinct terminology and that terminology is not interchangeable :

使用不同的术语(以上)正在发生三件不同的事情,并且该术语不可互换:

  1. A reference variable is declared - MyClass myClassyReference
  2. An object is instantiated (...from/of a given class, implied) - new MyClass()
  3. The object is assigned to the variable. =.
  1. 声明了一个引用变量 - MyClass myClassyReference
  2. 一个对象被实例化(...来自/来自给定的,隐含的) -new MyClass()
  3. 对象被分配给变量。=.

Restating the facts:

重述事实:

  1. A reference-type variable is also called simply "a reference". A "value-type variable" is not a reference.

  2. This: "objectA is an instance of an object" is profoundly wrong. If objectA was "an instance of objectB" then it must be that objectA begins life with objectB's type - whatever that is - and current state - whatever that is. What about creating objects D, E, and F as objectB changes? Nay, nay! It is the conceptual and technical case the "objectA is an instance of a Class". "Instantiation" and "instance of" have precise meaning - an object gets its type, definitions, and values from a Class.

  3. MyClass myClassyReference = nullGenerally we don't say "the variable is assigned to null" and we never say "the variable is referencing null", No. instead we say "the variable is null"; or "the variable is not referencing anything", or "the reference is null"

  1. 引用类型变量也简称为“引用”。“值类型变量”不是引用。

  2. 这个:“objectA 是一个对象的实例”是非常错误的。如果 objectA 是“objectB 的一个实例”,那么它必须是 objectA 以 objectB 的类型开始生命 - 无论是什么 - 以及当前状态 - 无论是什么。当 objectB 更改时创建对象 D、E 和 F 怎么样?不,不!“objectA 是类的实例”是概念和技术案例。“实例化”和“实例化”具有精确的含义——对象从类中获取其类型、定义和值。

  3. MyClass myClassyReference = null一般我们不说“变量被赋值为null”,也从不说“变量引用null”,而是说“变量为null”;或“变量未引用任何内容”,或“引用为空”

Practical Application:

实际应用:

  • I jab my finger at your code and say "this instance has an invalid property. Maybe that's why the loop fails. You gotta validate parameters during instantiation." (i.e. constructor arguments).

  • I see this in your code ,

    MyClass myClassyReference;
    myClassyReference.DoSomething();
    

    "You declared the variable but never assigned it. it's null so it's not referencing anything. That's why the method call throws an exception."

  • 我用手指指着你的代码说“这个实例有一个无效的属性。也许这就是循环失败的原因。你必须在实例化过程中验证参数。” (即构造函数参数)。

  • 我在你的代码中看到了这一点,

    MyClass myClassyReference;
    myClassyReference.DoSomething();
    

    “你声明了变量,但从未分配过它。它是空的,所以它没有引用任何东西。这就是方法调用抛出异常的原因。”

end edit

结束编辑



The Unbearable Lightness of Being

生命中不能承受之轻

A reference typevariable's name and value exists independently. And I do mean independent.

引用类型变量的名称和值独立地存在。我的意思是独立。

An instantiatedobject may or may not have a reference to it.

一个实例化的对象可以或可以不具有它的一个引用。

An instantiatedobject may have many references to it.

一个实例化的对象可能有很多对它的引用。

A declaredreference may or may not be pointing toan object.

声明参考可以或可以不被指向的对象。

回答by Andrew Hare

A variableis initialized with a value. An objectis instantiated when memory is allocated for it and it's constructor has been run.

一个变量用一个值初始化。当为它分配内存并且它的构造函数已经运行时,一个对象被实例化。

For instance here is a variable:

例如这里是一个变量:

Dim obj as Object

This variable has not been initialized. Once I assign a value to the objvariable, the variable will be initialized. Here are examples of initialization:

此变量尚未初始化。一旦我为obj变量赋值,变量就会被初始化。下面是初始化的例子:

obj = 1
obj = "foo"

Instantiation is a very different thing but is related since instantiation is usually followed by initialization:

实例化是一个非常不同的事情,但是是相关的,因为实例化通常跟在初始化之后:

Dim obj As New Object()

In the preceding line of code, the objvariable is initializedwith the reference to the new Objectthat was instantiated. We say that the new Objectwas instantiated because we have created a new instance of it.

在代码的前面的线,所述obj变量被初始化与参照新Object这是实例化。我们说 newObject被实例化是因为我们已经创建了它的一个新实例。

Now I believe that VB.NET makes this a lot more confusing than C# because it is not clear that an assignment is taking place in the code above. In C# it is much clearer that there is both an instantiation of an instance and an initialization of a variable:

现在我相信 VB.NET 使这比 C# 更令人困惑,因为不清楚上面的代码中是否发生了赋值。在 C# 中,实例的实例化和变量的初始化都更加清晰:

Object obj = new Object();

回答by Robert Rossney

To initializesomething is to set it to its initial value. To instantiatesomething is to create an instance of it.

初始化的东西就是把它设置为初始值。要实例化的东西是创建它的一个实例。

Often this is the more or less same thing. This:

通常这或多或少是相同的。这个:

SqlConnection conn = new SqlConnection();

instantiatesa SqlConnectionobject, and initializesthe connvariable by setting it to the that instance.

实例化一个SqlConnection对象,并通过将其设置为该实例来初始化conn变量。

Since an object's constructor also sets the object's properties to their default values, it's often correct to say that instantiating an object initializes it. (Misleading, if the object exposes a method that you have to explictly call to initialize it after it's instantiated, as is sometimes the case.)

由于对象的构造函数还将对象的属性设置为其默认值,因此通常说实例化对象会对其进行初始化是正确的。(误导,如果对象公开一个方法,您必须在实例化后显式调用以初始化它,有时就是这种情况。)

回答by Sona

*Instantiation means to create an instance for a class or object.Initialization means to *initiate the same object or class for any purpose.**

*实例化意味着为类或对象创建实例。初始化意味着*为任何目的启动相同的对象或类。**

回答by Gabriel McAdams

Instantiated means that an instance of the object has been created. Initiated means that that same object has done some initialization.

实例化意味着已经创建了对象的实例。Initiated 意味着同一个对象已经完成了一些初始化。

回答by typeoneerror

When you instantiate a class or object, you're creating a new instance of it, or allocating memory to "hold" one. Initializing that object would be the instructions that are performed during instantiation.

当你实例化一个类或对象时,你正在创建它的一个新实例,或者分配内存来“保存”一个。初始化该对象将是在实例化期间执行的指令。

回答by ChrisA

Instantiation is when you create an instance of a class. That instance is then an object, and you can set its properties, or call methods on it (tell it to do things).

实例化是指创建类的实例。那个实例就是一个对象,你可以设置它的属性,或者调用它的方法(告诉它做事)。

Initiation is when you set up a set of initial conditions for something. That something might be an object, where you tell it to initiate itself, or just a variable to which you assign a value.

启动是当您为某事设置一组初始条件时。那个东西可能是一个对象,你告诉它启动它自己,或者只是一个你赋值的变量。

An object might initialise some other things, or even instantiate other objects as part of its initiation.

一个对象可能会初始化一些其他的东西,甚至实例化其他对象作为其初始化的一部分。

The difference is that instantiation is creation of a thing that can do stuff; initiation is stuff that gets done.

不同之处在于实例化是创建一个可以做某事的东西;启动是完成的事情。

回答by jaros

See the Java docs: https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html

请参阅 Java 文档:https: //docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html

"Point originOne= new Point(23, 94);

" Point originOne= new Point(23, 94);

Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.
Instantiation: The new keyword is a Java operator that creates the object.
Initialization: The new operator is followed by a call to a constructor, which initializes the new object."

回答by nawfal

Others have explained the difference, so I wont go into detail. But there are cases where instantiation does not properly initialize an object. When you instantiate an object you also initialize it with some data. The class/type will have the initialization logic, whereas the instantiation logic is typically carried out by thenewkeyword (basically memory allocation, reference copying etc). But instantiation need not necessarily result in a valid state for objects which is when we can say the object is uninitialzed. Here's a practical example where an object can be instantiated but not initialized (sorry e.g. in C#).

其他人已经解释了差异,所以我不会详细说明。但是有些情况下实例化没有正确初始化一个对象。当你实例化一个对象时,你也用一些数据初始化它。类/类型将具有初始化逻辑,而实例化逻辑通常由new关键字执行(基本上是内存分配、引用复制等)。但是实例化不一定会导致对象的有效状态,即我们可以说对象未初始化的时候。这是一个实际示例,其中一个对象可以被实例化但不能被初始化(抱歉,例如在 C# 中)。

class P { string name = "Ralf"; }

WriteLine(new P().name); // "Ralf";
WriteLine((FormatterServices.GetUninitializedObject(typeof(P)) as P).name); // null

GetUninitializedObjectdoesn't call the constructor to instantiate object there (but some internal magic).

GetUninitializedObject不调用构造函数在那里实例化对象(但有一些内部魔法)。

One could also argue value types are not instantiated but only initialized as it doesn't need new allocation when you do new..but that's up to one's definition of instantiation.

人们也可能会争辩说值类型没有被实例化而只是被初始化,因为当你这样做时它不需要新的分配,new..但这取决于一个人对实例化的定义。

回答by MrClan

We can see it this way. For a line of code below:

我们可以这样看。对于下面的一行代码:

var p = new Person();

The above line can be read as following two ways:

可以通过以下两种方式阅读上面的行:

  1. The variable p has been initializedas a person class
  2. Person class has been instantiatedin variable p
  1. 变量 p 已被初始化为一个人类
  2. Person 类已在变量 p 中实例化

The subject of reference or context matters. When talking in terms of variable, we use the word initialize. When talking in terms of class/type, we use the word instantiate.

参考主题或上下文很重要。当谈到变量时,我们使用初始化这个词。在谈论class/type 时,我们使用instanceiate这个词。