“实例化"和“实例化"之间有什么区别?和“初始化"? [英] What is the difference between "instantiated" and "initialized"?

查看:42
本文介绍了“实例化"和“实例化"之间有什么区别?和“初始化"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

Value vis-a-vis Reference Types

C# 中的变量 属于 2 组中的 1 组. 类型或引用 类型.intDateTime 等类型是 value 类型.相比之下,您创建的任何类都是 reference 类型.C# 字符串也是 reference 类型..NET 框架中的大部分内容都是引用类型.

Value vis-a-vis Reference Types

Variables in C# are in 1 of 2 groups. Value types or Reference types. Types like int and DateTime are value types. In contrast, any class you create is a reference type. C# strings are also a reference type. Most things in the .NET framework are reference types.

有变量name 和它的.两部分.

变量的名称就是你声明它是什么.价值是您分配给它的.

The variable's name is what you declare it to be. The value is what you assign to it.

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

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

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

For value types, like int the compiler will give them a valid value if you do not do so explicitly. int's initialize to zero by default, DateTime's initialize to DateTime.MinValue by default.

引用类型变量初始化到你给它的对象.如果您不这样做,编译器将不会分配一个对象(即有效值).在这种情况下,该值为 null - 没有.所以我们说引用被初始化为空.

Reference type variables initialize to the object you give it. The compiler will not assign an 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 initialized to null.

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

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

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

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

MyClass myClassyReference = new MyClass();

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

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

编辑 - 受到评论讨论的启发

使用不同的术语进行三件不同的事情(以上),并且该术语不可互换:

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

  1. 声明了一个引用变量 - MyClass myClassyReference
  2. 一个对象被实例化(...来自/来自给定的,隐含的) - new MyClass()
  3. 对象被分配给变量.=.
  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. 引用类型变量也简称为引用".值类型变量"不是参考.

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

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

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.

MyClass myClassyReference = null 通常我们不说变量被赋值为 null";并且我们从不说变量引用空值",而不是说变量为空值";或者变量没有引用任何东西",或者引用为空"

MyClass myClassyReference = null Generally 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"

实际应用:

  • 我用手指戳了一下你的代码,然后说这个实例有一个无效的属性.也许这就是循环失败的原因.您必须在实例化期间验证参数."(即构造函数参数).

  • 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."

结束编辑

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

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

一个实例化对象可能有也可能没有对它的引用.

An instantiated object may or may not have a reference to it.

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

An instantiated object may have many references to it.

声明的引用可能会也可能不会指向一个对象.

A declared reference may or may not be pointing to an object.

这篇关于“实例化"和“实例化"之间有什么区别?和“初始化"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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