参数和基类? [英] out Parameter And Base Class ?

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

问题描述

大家好:

我觉得我在使用参数时遇到了问题,为什么

''SubClass''的实例无法转换为'' BaseClass''?我的代码是:

-------------------------------------- --------

使用系统;


命名空间ConsoleApplication2

{


class Class1

{

[STAThread]

static void Main(string [] args)

{

SubClass y = new SubClass();

Test(out y);

}


static void Test(out BaseClass y)

{

Console.WriteLine(y.ToString());

}

}

类BaseClass

{

public BaseClass()

{

Console.WriteLine(" BaseClass");

}

}

class SubClass:BaseClass

{

public SubClass()

{

Console.WriteLine(" SubClass");

}

}

}

提前感谢

亚东赵

Hi all:
I think I had a problem with using out parameter , why the instance of
''SubClass'' can''t convert to ''BaseClass'' ? my code is :
----------------------------------------------
using System;

namespace ConsoleApplication2
{

class Class1
{
[STAThread]
static void Main(string[] args)
{
SubClass y = new SubClass() ;
Test( out y);
}

static void Test( out BaseClass y )
{
Console.WriteLine(y.ToString());
}
}
class BaseClass
{
public BaseClass()
{
Console.WriteLine("BaseClass");
}
}
class SubClass : BaseClass
{
public SubClass()
{
Console.WriteLine("SubClass");
}
}
}
thanks in advance
Yadong Zhao

推荐答案

dahuzizyd,


你得到的理由他是因为y需要是BaseClass类型。如果

你从BaseClass派生了另一个类,比如SubClass2,那么测试

方法可以将参数y赋给SubClass2的一个实例。返回

后,变量y将无法分配给,因为它是

类型SubClass2,而不是SubClass。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" dahuzizyd" <哒******** @ hotmail.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP15.phx.gbl ...
dahuzizyd,

The reason you get this is because y needs to be of type BaseClass. If
you derived another class from BaseClass, say, SubClass2, then the Test
method could assign the parameter y to an instance of SubClass2. Upon
return, the variable y would not be able to be assigned to, because it is of
type SubClass2, not SubClass.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"dahuzizyd" <da********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
大家好:
我认为我在使用参数时遇到了问题,为什么
''SubClass''的实例不能转换为''BaseClass''?我的代码是:
------------------------------------------ ----
使用系统;

命名空间ConsoleApplication2
{Class class
{
[STAThread]
static void Main(string [] args)
{Suboclass y = new SubClass();
测试(out y);
}
static void Test(out BaseClass y)
{
Console.WriteLine(y.ToString());
}
}
类BaseClass
{
public BaseClass()
{
Console.WriteLine(" BaseClass");
}
}类SubClass:BaseClass
{
public SubClass()
{
Console.WriteLine(" SubClass");
}
}
}

提前致谢
赵亚东
Hi all:
I think I had a problem with using out parameter , why the instance of
''SubClass'' can''t convert to ''BaseClass'' ? my code is :
----------------------------------------------
using System;

namespace ConsoleApplication2
{

class Class1
{
[STAThread]
static void Main(string[] args)
{
SubClass y = new SubClass() ;
Test( out y);
}

static void Test( out BaseClass y )
{
Console.WriteLine(y.ToString());
}
}
class BaseClass
{
public BaseClass()
{
Console.WriteLine("BaseClass");
}
}
class SubClass : BaseClass
{
public SubClass()
{
Console.WriteLine("SubClass");
}
}
}
thanks in advance
Yadong Zhao



感谢您的帮助。

我的英语太差了我无法清楚地表明我的问题: - (


我写这段代码使Test方法可以重新使用。所有类<从BaseClass派生的
可以是这个方法的参数。但是如果我这样写




BaseClass y;

测试(out y);


static void测试(out BaseClass y)

{

y = new SubClass();

Console.WriteLine(1);

}


此代码可以编译。但是BaseClass什么都不会..如果我不用
使用外出的话,一切都很好。

所以我觉得有些东西我从来都不知道一句话。


class Class1

{

[STAThread]

static void Main (string [] args)

{

DeriveClass y;

测试(out y);


DeriveClass s = new DeriveClass();

DeriveClass2 s1 = new DeriveClass2();

Test1(s);

Test1(s1 );

}

static void测试(out BaseClass y)

{

y = new DeriveClass();

Console.WriteLine(1);

}

static void Test1(BaseClass y)

{

Console.WriteLine(2);

}

}

class BaseClass

{

}

类DeriveClass:BaseClass

{


}

class DeriveClass2:BaseClass

{


}

谢谢你dvance

亚东赵

Thanks for your help .
My english is so bad that I can''t show my problem clearly : - (

I write this code to make the Test method can reusnable. All class which
derived from BaseClass can be a parameter for this method.But if I write
like this:

BaseClass y ;
Test( out y);

static void Test( out BaseClass y )
{
y = new SubClass();
Console.WriteLine(1);
}

this code can be compiled . But the BaseClass will mean nothing.. If I don''t
use the out word,everything is good.
So I think there are some thing that I never knew with the out word.

class Class1
{
[STAThread]
static void Main(string[] args)
{
DeriveClass y ;
Test( out y);

DeriveClass s = new DeriveClass();
DeriveClass2 s1 = new DeriveClass2();
Test1(s);
Test1(s1);
}
static void Test( out BaseClass y )
{
y = new DeriveClass();
Console.WriteLine(1);
}
static void Test1 ( BaseClass y )
{
Console.WriteLine(2);
}
}
class BaseClass
{
}
class DeriveClass : BaseClass
{

}
class DeriveClass2 : BaseClass
{

}
thanks in advance
Yadong Zhao


dahuzizyd< da ******** @ hotmail.com>写道:
dahuzizyd <da********@hotmail.com> wrote:
感谢您的帮助。
我的英语非常糟糕,我无法清楚地表明我的问题: - (


否,我认为你的问题很好。

我编写这段代码使Test方法可以重用。所有从BaseClass派生的类都可以作为这个方法的参数。但是如果我这样写:

BaseClass y;
测试(out y);

static void测试(out BaseClass y)
{
y = new SubClass();
Console.WriteLine(1);
}
这个代码可以编译。但BaseClass什么都没有..如果我没有使用外面的单词,一切都很好。


的确如此。

所以我认为有些事我从来没有知道外面的话
Thanks for your help .
My english is so bad that I can''t show my problem clearly : - (
No, I think you showed your problem fine.
I write this code to make the Test method can reusnable. All class which
derived from BaseClass can be a parameter for this method.But if I write
like this:

BaseClass y ;
Test( out y);

static void Test( out BaseClass y )
{
y = new SubClass();
Console.WriteLine(1);
}

this code can be compiled . But the BaseClass will mean nothing.. If I don''t
use the out word,everything is good.
Indeed.
So I think there are some thing that I never knew with the out word




就像Nick说的那样。你不能使用不同类型的变量

(即使它是一个子类)用于out参数。你的测试方法(在

你的第一篇文章中)可以创建一个新的BaseClass实例(或一个

不同的子类)而不是SubClass - 并且在那个阶段,当
$编译b $ b方法,变量没有正确的价值。


这在C#语言规范中指定为:

< ; quote>

当形式参数是输出参数时,方法调用中相应的

参数必须包含关键字out

后跟通过与正式

参数相同类型的变量引用。

< / quote>


参见 http://www.pobox.com/~skeet/csharp/parameters。 html 更多

有关参数传递的信息。


-

Jon Skeet - < sk ** *@pobox.com>
http://www.pobox.com/~小号keet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件



It''s exactly as Nick said. You can''t use a variable of a different type
(even if it''s a subclass) for an "out" argument. Your Test method (in
your first post) could create a new instance of BaseClass (or a
different subclass) instead of SubClass - and at that stage, when the
method compiled, the variable wouldn''t have the right kind of value.

This is specified in the C# language spec as:
<quote>
When a formal parameter is an output parameter, the corresponding
argument in a method invocation must consist of the keyword out
followed by a variable-reference of the same type as the formal
parameter.
</quote>

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information about parameter passing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


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

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