在运行时选择类 [英] Selecting class at runtime
问题描述
你好,
我有一个让我发疯的问题。必须有一个简单的解决方案,但是我没有看到它。下面的代码只是一个简单的例子,
演示了我的问题:
我有一个名为Human的抽象类。包含所有派生
类所需的成员。 "人"对于它的子类没有任何了解,
因此无法使用虚拟/覆盖-Construct:
1抽象类人类{
2 public int age = 0;
3}
现在我有两个来自人类的类。包含特定成员:
1 class女人:人类{
2 public void DoFemaleStuff(){
3 System.Console .WriteLine(去购物。);
4}
5}
和(你猜对了)
1级男子:人类{
2 public void DoMaleStuff(){
3 System.Console.WriteLine("修理汽车。);
4}
5}
现在,我可以做以下琐碎的事情:
1女人myWoman =新女人();
2 myWoman.age = 25;
3 myWoman.DoFemaleStuff();
4
5 man myMan = new Man();
6 myMan.age = 30;
7 myMan.DoMaleStuff();
但是,我想要做的是我只有一个人的性别定义了
在运行时,如下所示:
1 if(true){
2女人myHuman =新女人();
3 myHuman.DoFemaleStuff();
4}否则{
5 Man myHuman = new Man();
6 myHuman.DoMaleStuff() ;
7}
8 myHuman.age = 30;
在这个例子中,编译器说myHuman在第8行不知道。对于
我的观点,这是不正确的,虽然我知道在一些
有条件的陈述比这个更复杂的情况可能会发生
其中myHuman不会被宣布。但我想说,这是程序员的问题,而不是编者的问题。
但是下面的例子也失败了(注意) :类人类现在不再是
摘要):
1人类myHuman;
2 if(true) {
3 myHuman = new Woman();
4 myHuman.DoFemaleStuff();
5}否则{
6 myHuman = new Man();
7 myHuman.DoMaleStuff();
8}
9 myHuman.age = 30;
现在编译器说myHuman不包含
" DoFemaleStuff"的定义第4行中没有DoMaleStuff在第7行.Aargh!
看来我只是误解了一个基本的东西。任何建议
保释我都会非常感激。
Daniel
-
http://www.bolege.de
电子邮件不会读取指定的地址。
请改用db [at] bolege [dot] de。
Hello,
I''ve a problem that drives me crazy. There must be a simple solution, but I
just can''t see it. The following code is just a simple example that
demonstrates my problem:
I have an abstract class named "Human" containing members that all derived
classes will need. "Human" does not know anything about its child classes,
so no way for the "virtual/override"-Construct:
1 abstract class Human {
2 public int age = 0;
3 }
Now I have two classes derived from "Human" that contain specific members:
1 class Woman : Human {
2 public void DoFemaleStuff() {
3 System.Console.WriteLine("Go shopping.");
4 }
5 }
and (you guess right)
1 class Man : Human {
2 public void DoMaleStuff() {
3 System.Console.WriteLine("Repair car.");
4 }
5 }
Now, I can do the following trivial stuff:
1 Woman myWoman = new Woman();
2 myWoman.age = 25;
3 myWoman.DoFemaleStuff();
4
5 Man myMan = new Man();
6 myMan.age = 30;
7 myMan.DoMaleStuff();
But, what I want to do is that I only have one human whose sex ist defined
at runtime, like the follwing:
1 if (true) {
2 Woman myHuman = new Woman();
3 myHuman.DoFemaleStuff();
4 } else {
5 Man myHuman = new Man();
6 myHuman.DoMaleStuff();
7 }
8 myHuman.age = 30;
In this example, the compiler says that "myHuman" in Line 8 ist unknown. For
my point of view, this is not correct, although I know that in some
conditional statments more complex than this one a situation may occur
where "myHuman" will not be declared. But I would say, this is the
programmer''s, not the comiler''s problem.
But the following example fails as well (Note: Class "Human" is now NOT
abstract anymore):
1 Human myHuman;
2 if (true) {
3 myHuman = new Woman();
4 myHuman.DoFemaleStuff();
5 } else {
6 myHuman = new Man();
7 myHuman.DoMaleStuff();
8 }
9 myHuman.age = 30;
Now the compiler says that "myHuman" does not contain a definition for
"DoFemaleStuff" in Line 4 and no for "DoMaleStuff" in Line 7. Aargh!
It seems I just misunderstand a fundamental thing. Any suggestion that
bails me out would be very appreciated.
Daniel
--
http://www.bolege.de
E-mails to specified address will NOT be read.
Please use db[at]bolege[dot]de instead.
推荐答案
Daniel,
为什么不这样定义你的Human类:
抽象类人类{
public int age = 0;
public abstract void DoStuff();
}
然后,在你派生的男性和女性课程中,你这样做:
class女人:人类{
public override void DoStuff(){
System.Console.WriteLine( 去购物。;
}
}
这里的想法是男人和女人都做东西这对于他们是男人或女人(专业化)而言是特定的,但在同一时间,作为人类,他们都有他们执行的一般行动。这是一个完美的抽象方法,IMO。
希望这会有所帮助。
-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com
Daniel Bolege <我们****** @ bolege.de>在消息中写道
news:40 ******** @ news.arcor-ip.de ...
Daniel,
Why not define your Human class like this:
abstract class Human {
public int age = 0;
public abstract void DoStuff();
}
Then, in your derived male and female classes, you just do this:
class Woman : Human {
public override void DoStuff() {
System.Console.WriteLine("Go shopping.");
}
}
The idea here is that a man and a woman both do "stuff" which is
specific to them being a man or woman (specialization), but at the same
time, as humans, they both have a generic action that they perform. This is
perfect for an abstract method, IMO.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Daniel Bolege" <us******@bolege.de> wrote in message
news:40********@news.arcor-ip.de...
你好,
>我有一个让我发疯的问题。必须有一个简单的解决方案,但
我只是看不到它。下面的代码只是一个简单的例子来说明我的问题:
我有一个名为Human的抽象类。包含所有派生类需要的成员。 "人"对于它的子类没有任何了解,
因此无法用于虚拟/覆盖-Construct:
1抽象类人类{
2 public int age = 0 ;
3}
现在我有两个来自人类的类。包含特定成员:
1类女性:人类{
2 public void DoFemaleStuff(){3> System.Console.WriteLine(" Go shopping。");
4}
和(你猜对了)
1级男人:人类{
2 public void DoMaleStuff() {
3 System.Console.WriteLine(修理汽车。);
4}
5}现在,我可以做以下琐碎的事情:
1女人myWoman =新女人();
2 myWoman.age = 25;
3 myWoman.DoFemaleStuff();
4
5男人myMan =新男人();
6 myMan.age = 30;
7 myMan.DoMaleStuff();
但是,我想做的是我只有一个人的性别在运行时定义,如下所示:
1 if(true){
2女人myHuman =新女人();
3 myHuman .DoFemaleStuff();
4}否则{
5 man myHuman = new Man();
6 myHuman.DoMaleStuff();
7}
8 myHuman.age = 30;
在这个例子中,编译器说myHuman在第8行不知道。
对于我的观点,这是不正确的,虽然我知道在一些比这更复杂的条件性陈述中可能会出现情况
其中myHuman不会被宣布。但我想说,这是程序员的问题,而不是编者的问题。
但是下面的例子也失败了(注意:ClassHuman现在是不再抽象了):
1人类myHuman;
2 if(true){
3 myHuman = new Woman();
4 myHuman。 DoFemaleStuff();
5}否则{
6 myHuman = new Man();
7 myHuman.DoMaleStuff();
8}
9 myHuman.age = 30;
现在编译器说myHuman不包含
DoFemaleStuff的定义第4行中没有DoMaleStuff在第7行.Aargh!
似乎我只是误解了一个基本的东西。任何暗示让我失望的建议都会非常感激。
Daniel
-
http://www.bolege.de
不会读取指定地址的电子邮件。
请使用db [ at] bolege [dot] de。
Hello,
I''ve a problem that drives me crazy. There must be a simple solution, but I just can''t see it. The following code is just a simple example that
demonstrates my problem:
I have an abstract class named "Human" containing members that all derived
classes will need. "Human" does not know anything about its child classes,
so no way for the "virtual/override"-Construct:
1 abstract class Human {
2 public int age = 0;
3 }
Now I have two classes derived from "Human" that contain specific members:
1 class Woman : Human {
2 public void DoFemaleStuff() {
3 System.Console.WriteLine("Go shopping.");
4 }
5 }
and (you guess right)
1 class Man : Human {
2 public void DoMaleStuff() {
3 System.Console.WriteLine("Repair car.");
4 }
5 }
Now, I can do the following trivial stuff:
1 Woman myWoman = new Woman();
2 myWoman.age = 25;
3 myWoman.DoFemaleStuff();
4
5 Man myMan = new Man();
6 myMan.age = 30;
7 myMan.DoMaleStuff();
But, what I want to do is that I only have one human whose sex ist defined
at runtime, like the follwing:
1 if (true) {
2 Woman myHuman = new Woman();
3 myHuman.DoFemaleStuff();
4 } else {
5 Man myHuman = new Man();
6 myHuman.DoMaleStuff();
7 }
8 myHuman.age = 30;
In this example, the compiler says that "myHuman" in Line 8 ist unknown. For my point of view, this is not correct, although I know that in some
conditional statments more complex than this one a situation may occur
where "myHuman" will not be declared. But I would say, this is the
programmer''s, not the comiler''s problem.
But the following example fails as well (Note: Class "Human" is now NOT
abstract anymore):
1 Human myHuman;
2 if (true) {
3 myHuman = new Woman();
4 myHuman.DoFemaleStuff();
5 } else {
6 myHuman = new Man();
7 myHuman.DoMaleStuff();
8 }
9 myHuman.age = 30;
Now the compiler says that "myHuman" does not contain a definition for
"DoFemaleStuff" in Line 4 and no for "DoMaleStuff" in Line 7. Aargh!
It seems I just misunderstand a fundamental thing. Any suggestion that
bails me out would be very appreciated.
Daniel
--
http://www.bolege.de
E-mails to specified address will NOT be read.
Please use db[at]bolege[dot]de instead.
Daniel Bolege< us ****** @ bolege.de>写道:
Daniel Bolege <us******@bolege.de> wrote:
但是,我想要做的是,我只有一个人在运行时定义了性别,如下所示:
1 if(真的){
2女人myHuman =新女人();
3 myHuman.DoFemaleStuff();
4}否则{
5男人myHuman =新男人();
6 myHuman.DoMaleStuff();
7}
8 myHuman.age = 30;
在这个例子中,编译器说myHuman在第8行不知道。对于我的观点,这是不正确的,虽然我知道在一些比这更复杂的条件性陈述中可能会出现这样的情况。
其中myHuman不会被宣布。但我会说,这是程序员的问题,而不是编者的问题。
不,编译器是完全正确的。两个(不同的)myHuman
变量每个只有一个范围,这个范围是你指定的块。
最后一行不属于任何一个范围。
但是下面的例子也失败了(注意:类人类现在不再是抽象的了):
1人类myHuman;
2 if(true){
3 myHuman = new Woman();
4 myHuman.DoFemaleStuff();
5} else {
6 myHuman = new Man();
7 myHuman.DoMaleStuff();
8}
9 myHuman.age = 30;
现在编译器说myHuman不包含
DoFemaleStuff的定义第4行中没有DoMaleStuff在第7行.Aargh!
似乎我只是误解了一个基本的东西。任何暗示让我失望的建议都会非常感激。
But, what I want to do is that I only have one human whose sex ist defined
at runtime, like the follwing:
1 if (true) {
2 Woman myHuman = new Woman();
3 myHuman.DoFemaleStuff();
4 } else {
5 Man myHuman = new Man();
6 myHuman.DoMaleStuff();
7 }
8 myHuman.age = 30;
In this example, the compiler says that "myHuman" in Line 8 ist unknown. For
my point of view, this is not correct, although I know that in some
conditional statments more complex than this one a situation may occur
where "myHuman" will not be declared. But I would say, this is the
programmer''s, not the comiler''s problem.
No, the compiler is completely correct. The two (distinct) myHuman
variables each only have a scope which is the block you''ve specified.
The last line is not within the scope of either of them.
But the following example fails as well (Note: Class "Human" is now NOT
abstract anymore):
1 Human myHuman;
2 if (true) {
3 myHuman = new Woman();
4 myHuman.DoFemaleStuff();
5 } else {
6 myHuman = new Man();
7 myHuman.DoMaleStuff();
8 }
9 myHuman.age = 30;
Now the compiler says that "myHuman" does not contain a definition for
"DoFemaleStuff" in Line 4 and no for "DoMaleStuff" in Line 7. Aargh!
It seems I just misunderstand a fundamental thing. Any suggestion that
bails me out would be very appreciated.
您可以将第4行更改为:
((女)myHuman).doFemaleStuff();
和类似于第7行的东西。
或者,做:
女人女人=新女人();
woman.DoFemaleStuff();
myHuman =女人;
等
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复对于小组,请不要给我发邮件
You could change line 4 to:
((Woman)myHuman).doFemaleStuff();
and something similar for line 7.
Alternatively, do:
Woman woman = new Woman();
woman.DoFemaleStuff();
myHuman = woman;
etc
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
C#是强类型的,因此你将无法访问派生的成员
你的抽象类变量中的类,而不是先抛出它。
例如。
((Man)myHuman).DoMaleStuff();
但是,作为neatuer,代码应该看起来像这样:
人类myHuman;
if(true){
Woman myWoman = new Woman();
myWoman.DoFemaleStuff();
myHuman = myWoman;
}其他{
Man myMan = new Man();
myMan.DoMaleStuff();
myHuman = myMan;
}
myHuman.age = 30;
" Daniel Bolege" <我们****** @ bolege.de>在消息中写道
news:40 ******** @ news.arcor-ip.de ...
C# is strongly typed so you won''t be able to access members of derived
classes from your abstract class variable without casting it first.
eg.
((Man)myHuman).DoMaleStuff();
However, to be neatuer the code should probably look like this:
Human myHuman;
if (true) {
Woman myWoman = new Woman();
myWoman.DoFemaleStuff();
myHuman = myWoman;
} else {
Man myMan = new Man();
myMan.DoMaleStuff();
myHuman = myMan;
}
myHuman.age = 30;
"Daniel Bolege" <us******@bolege.de> wrote in message
news:40********@news.arcor-ip.de...
你好,
>我有一个让我发疯的问题。必须有一个简单的解决方案,但
我只是看不到它。下面的代码只是一个简单的例子来说明我的问题:
我有一个名为Human的抽象类。包含所有派生类需要的成员。 "人"对于它的子类没有任何了解,
因此无法用于虚拟/覆盖-Construct:
1抽象类人类{
2 public int age = 0 ;
3}
现在我有两个来自人类的类。包含特定成员:
1类女性:人类{
2 public void DoFemaleStuff(){3> System.Console.WriteLine(" Go shopping。");
4}
和(你猜对了)
1级男人:人类{
2 public void DoMaleStuff() {
3 System.Console.WriteLine(修理汽车。);
4}
5}现在,我可以做以下琐碎的事情:
1女人myWoman =新女人();
2 myWoman.age = 25;
3 myWoman.DoFemaleStuff();
4
5男人myMan =新男人();
6 myMan.age = 30;
7 myMan.DoMaleStuff();
但是,我想做的是我只有一个人的性别在运行时定义,如下所示:
1 if(true){
2女人myHuman =新女人();
3 myHuman .DoFemaleStuff();
4}否则{
5 man myHuman = new Man();
6 myHuman.DoMaleStuff();
7}
8 myHuman.age = 30;
在这个例子中,编译器说myHuman在第8行不知道。
对于我的观点,这是不正确的,虽然我知道在一些比这更复杂的条件性陈述中可能会出现情况
其中myHuman不会被宣布。但我想说,这是程序员的问题,而不是编者的问题。
但是下面的例子也失败了(注意:ClassHuman现在是不再抽象了):
1人类myHuman;
2 if(true){
3 myHuman = new Woman();
4 myHuman。 DoFemaleStuff();
5}否则{
6 myHuman = new Man();
7 myHuman.DoMaleStuff();
8}
9 myHuman.age = 30;
现在编译器说myHuman不包含
DoFemaleStuff的定义第4行中没有DoMaleStuff在第7行.Aargh!
似乎我只是误解了一个基本的东西。任何暗示让我失望的建议都会非常感激。
Daniel
-
http://www.bolege.de
不会读取指定地址的电子邮件。
请使用db [ at] bolege [dot] de。
Hello,
I''ve a problem that drives me crazy. There must be a simple solution, but I just can''t see it. The following code is just a simple example that
demonstrates my problem:
I have an abstract class named "Human" containing members that all derived
classes will need. "Human" does not know anything about its child classes,
so no way for the "virtual/override"-Construct:
1 abstract class Human {
2 public int age = 0;
3 }
Now I have two classes derived from "Human" that contain specific members:
1 class Woman : Human {
2 public void DoFemaleStuff() {
3 System.Console.WriteLine("Go shopping.");
4 }
5 }
and (you guess right)
1 class Man : Human {
2 public void DoMaleStuff() {
3 System.Console.WriteLine("Repair car.");
4 }
5 }
Now, I can do the following trivial stuff:
1 Woman myWoman = new Woman();
2 myWoman.age = 25;
3 myWoman.DoFemaleStuff();
4
5 Man myMan = new Man();
6 myMan.age = 30;
7 myMan.DoMaleStuff();
But, what I want to do is that I only have one human whose sex ist defined
at runtime, like the follwing:
1 if (true) {
2 Woman myHuman = new Woman();
3 myHuman.DoFemaleStuff();
4 } else {
5 Man myHuman = new Man();
6 myHuman.DoMaleStuff();
7 }
8 myHuman.age = 30;
In this example, the compiler says that "myHuman" in Line 8 ist unknown. For my point of view, this is not correct, although I know that in some
conditional statments more complex than this one a situation may occur
where "myHuman" will not be declared. But I would say, this is the
programmer''s, not the comiler''s problem.
But the following example fails as well (Note: Class "Human" is now NOT
abstract anymore):
1 Human myHuman;
2 if (true) {
3 myHuman = new Woman();
4 myHuman.DoFemaleStuff();
5 } else {
6 myHuman = new Man();
7 myHuman.DoMaleStuff();
8 }
9 myHuman.age = 30;
Now the compiler says that "myHuman" does not contain a definition for
"DoFemaleStuff" in Line 4 and no for "DoMaleStuff" in Line 7. Aargh!
It seems I just misunderstand a fundamental thing. Any suggestion that
bails me out would be very appreciated.
Daniel
--
http://www.bolege.de
E-mails to specified address will NOT be read.
Please use db[at]bolege[dot]de instead.
这篇关于在运行时选择类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!