类实例 [英] Class Instances

查看:51
本文介绍了类实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在创建类实例时,你必须先声明类名




newClass ras = new newClass();


谢谢

詹姆斯


-

http://www.goldwatches.com/watches.asp?Brand=39

Why when you create a class instance do you have to declare the class name
before.

newClass ras = new newClass();

Thanks
James

--

http://www.goldwatches.com/watches.asp?Brand=39

推荐答案



" James Matthews" < ja ********* @ gmail.comwrote in message

news:81 ********************* ************* @ microsof t.com ...

"James Matthews" <ja*********@gmail.comwrote in message
news:81**********************************@microsof t.com...

为什么在创建类实例时你必须声明班级名称

之前。


newClass ras = new newClass();
Why when you create a class instance do you have to declare the class name
before.

newClass ras = new newClass();



你没有。


对象ras;

ras = new newClass( );


工作得很好。


你必须声明一个变量,形式为type varname;。之后

你可以为它分配一个实例。有些语言区分

组合声明+赋值,称为初始化和单独步骤。 C#

没有。

You don''t.

object ras;
ras = new newClass();

works just fine.

You have to declare a variable, with the form "type varname;". After that
you can assign it an instance. Some languages distinguish between the
combined declare+assign, called initialization, and separate steps. C#
doesn''t.


>

谢谢

詹姆斯
>
Thanks
James



请从您的签名中获取看起来像垃圾邮件的内容。

And get what looks like spam out of your signature, please.


>

-

http ://www.goldwatches.com/watches.asp?Brand = 39


对该类的第一个引用只是一种告诉CLR的方法善意

您的变量将有的参考。对该类的第二个引用是

来实际实例化它。


例如你可能想创建一个指向已经的变量

现有课程:


//这里我们想要一个新的变量(x),所以我们需要告诉CLR

留出的地方记忆和多少记忆要留出来,所以我们首先说明x将指向什么类型。但是,

然后就会有什么将实际放入该内存的问题。如果它应该是一个新的类实例

你需要制作那个实例。

//你不能说一个实例而不说你在做什么

实例,所以我们需要第二个

//语句的一部分。

数据集x =新的DataSet();


//在下一行我们没有制作新的实例,但我们需要

表示

//什么样的数据变量o会指出,所以我们必须从

开始说

//类型(对象)

对象o = x;


" James Matthews" < ja ********* @ gmail.comwrote in message

news:81 ********************* ************* @ microsof t.com ...
The first reference to the class is just a way of telling the CLR what kind
of reference your variable will have. The second reference to the class is
to actually instantiate it.

For example you might want to create a variable that points to an already
existing class:

//Here we want a new variable (x) and so we need to tell the CLR where to
set aside memory and how much memory
//to set aside, so we start off by saying what type x will point to. But,
then there is the question of what will
//actually be placed into that memory. If it should be a new class instance
you need to make that instance.
//And you can''t make an instance without saying what you are making an
instance of, so we need the second
//part of the statement.
dataset x = new DataSet();

// On this next line we are not making a new instance, but we need to
indicate
// what kind of data the variable "o" will point to, so we must start by
saying the
// type (object)
object o = x;

"James Matthews" <ja*********@gmail.comwrote in message
news:81**********************************@microsof t.com...

为什么在创建类实例时你必须声明班级名称

之前。


newClass ras = new newClass();


谢谢

詹姆斯


-

http://www.goldwatches.com/watches.asp?Brand=39



James Matthews写道:
James Matthews wrote:

为什么在创建类实例时,你必须先声明类

名称。


newClass ras = new newClass();


谢谢

James
Why when you create a class instance do you have to declare the class
name before.

newClass ras = new newClass();

Thanks
James



因为第一个是引用的类型,第二个是对象的

类型。


代码相当于:


newClass ras;

ras = new newClass();


引用和对象的类型是并不总是一样的。对于

示例:


表格对话框=新的EditUserDialog();


这里的参考类型表单,但对象是派生的

类型的EditUserDialog。

在C#3中会有var关键字:


var builder = new System.Text.StringBuilder();


这将使引用与对象的类型相同。它应该是

但是只有当对象的类型很明显时才会被使用,并且

它实际上提高了可读性。


-

G ??跑安德森

_____
http://www.guffa.com


这篇关于类实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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