覆盖静态派生变量 [英] Override static derived variable

查看:71
本文介绍了覆盖静态派生变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



你好。


我有C#语言的问题。我想在抽象类中的

静态函数上定义一个算法。此函数在同一个类中调用静态

变量。然后我想定义一些派生的

类继承该函数并覆盖变量,但是它没有工作。


测试用例:


使用系统;


命名空间测试

{

抽象公共类X {

protected static string var =" x" ;;


public static void print_my_var(){

Console.WriteLine(" my var is" + var);

}

}


公共类A:X {

protected static new string var =" a" ;;

}


公共类B: X

{

protected static new string var =" b";

}


class ClassTest {


[STAThread]

static void Main(string [] args)

{

X.print_my_var();

A.print_my_var();

B.print_my_var();

}

}

}

结果:

我的var是x

我的var是x

我的var是x


预期结果:

我的var是x

我的var是一个

我的var是b


如何使用static获得预期的结果方法和静态

变量而不创建静态方法A或B?

提前致谢。


安德鲁


-


Hello.

I have a problem with C# language. I want to define an algorithm on a
static function inside an abstract class. This function calls a static
variable inside the same class. Then I want to define some derived
classes wich inherit the function and override the variable, but it
doesn''t work.

Testcase:

using System;

namespace Test
{

abstract public class X {
protected static string var = "x";

public static void print_my_var(){
Console.WriteLine("my var is " + var);
}
}

public class A : X {
protected static new string var = "a";
}

public class B : X
{
protected static new string var = "b";
}

class ClassTest {

[STAThread]
static void Main(string[] args)
{
X.print_my_var();
A.print_my_var();
B.print_my_var();
}
}
}
result:
my var is x
my var is x
my var is x

expected results:
my var is x
my var is a
my var is b

How can obtain the expected results using static methods and static
variables and without creating the static method A or B?

Thanks in advance.

Andrew

--

推荐答案

" knocte" < KN **** @ NO-SPAM-PLEASE-gmail.com> écritdansle message de news:

d9 ********** @ nsnmrro2-gest.nuria.telefonica-data.net ...
"knocte" <kn****@NO-SPAM-PLEASE-gmail.com> a écrit dans le message de news:
d9**********@nsnmrro2-gest.nuria.telefonica-data.net...
我遇到了C#语言的问题。我想在抽象类中的静态函数上定义一个算法。此函数在同一个类中调用静态
变量。然后我想定义一些派生的
类继承函数并覆盖变量,但它不起作用。
I have a problem with C# language. I want to define an algorithm on a
static function inside an abstract class. This function calls a static
variable inside the same class. Then I want to define some derived
classes wich inherit the function and override the variable, but it
doesn''t work.




我本来应该说你不应该覆盖变量,只需要在静态构造函数中初始化代码。


抽象公共类X

{

保护静态字符串变量;


保护静态SetVar(字符串值)

{

var = value;

}


静态X()

{

SetVar( X);

}


public static void print_my_var()

{

Console.WriteLine(" my var is" + var);

}

}


公共类A:X

{

静态A()

{

SetVar(" A");

}


但这不起作用,因为看起来在A类中调用的唯一静态构造函数

是X ()。


我必须承认这不像我预期的那样。有谁知道

这是测试版1中的错误吗?


Joanna


-

Joanna Carter

顾问软件工程师



I would have said that you should not override the variable, just the
initialising code in a static constructor.

abstract public class X
{
protected static string var;

protected static SetVar(string value)
{
var = value;
}

static X()
{
SetVar("X");
}

public static void print_my_var()
{
Console.WriteLine("my var is " + var);
}
}

public class A : X
{
static A()
{
SetVar("A");
}

But this doesn''t work either as it appears that the only static constructor
to be called in class A is X().

I must admit that this is not as I would have expected. Does anyone know if
this is a bug in beta 1 ?

Joanna

--
Joanna Carter
Consultant Software Engineer


knocte< kn **** @ NO-SPAM-PLEASE-gmail。 COM>在新闻中写道:d93v33
knocte <kn****@NO-SPAM-PLEASE-gmail.com> wrote in news:d93v33


skl


这篇关于覆盖静态派生变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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