当我不想知道子类的名称时创建子类的新实例
(我对 C# 还很陌生,所以请耐心等待,2 天前开始)
好的,这是一般结构和想法,
namespace Tokenize{
abstract class Token{
public Base(object id){
this.id = id;
}
private object id;
public object Id
{
get{...};
set{...};
}
public abstract object PerformOperation(object a, object b);
}
class NumberNine: Base{
public NumberNine(int num) : base(num){ }
public override int PerformOperation(int a, int b);
}
class LetterT: Base{
public LetterT(char letter) : base(letter){ }
public override string PerformOperation(string a, char b);
}
}
using Tokenize;
namespace Program{
class MyProg{
static void Main(string[] args)
{
Token token;
Stack stack;
//.....
//Read some input
string s = "T";
//.....
if(s==anonymousDerivedClassId){
//How do I come up with class LetterT and keep it anonymous?
Token token = new anonymousDerivedClass(); //
stack.Push(token)
}
//.....
//do some stuff
//.....
object a;
object b;
Token q = stack.Pop(token);
q.PerformOperation(a,b);
}
}
}
我想创建一个我不想要的子类的新实例知道名字吗?
我“甚至不知道”它是否存在?
希望这不是太复杂...
编辑:
我基本上不想跟踪所有子类...
编辑2(新示例):
考虑:
static void Main(string[] args)
{
Operator op;
string s = "*";
if(s==MulOperator.Identifier){
op = new MulOperator();
}
else{
op = new DivOperator();
}
perform(op);
}
static void perform(Operator op)
{
Console.WriteLine("Sum:{0}", op.PerformOperation(2.2,2.0));
}
我想用更通用的方法来摆脱 new MulOperator()
和 MulOperator.Identifier
,在本例中创建 Operator
的子类>。
编辑3:*
(解决方案) 我要尝试 C# 中的通用工厂< /a>, 看来这就是我想要实现的目标。
(I'm pretty new to C# so bear with me, started 2 days ago)
Ok, here is the general structure and idea
namespace Tokenize{
abstract class Token{
public Base(object id){
this.id = id;
}
private object id;
public object Id
{
get{...};
set{...};
}
public abstract object PerformOperation(object a, object b);
}
class NumberNine: Base{
public NumberNine(int num) : base(num){ }
public override int PerformOperation(int a, int b);
}
class LetterT: Base{
public LetterT(char letter) : base(letter){ }
public override string PerformOperation(string a, char b);
}
}
using Tokenize;
namespace Program{
class MyProg{
static void Main(string[] args)
{
Token token;
Stack stack;
//.....
//Read some input
string s = "T";
//.....
if(s==anonymousDerivedClassId){
//How do I come up with class LetterT and keep it anonymous?
Token token = new anonymousDerivedClass(); //
stack.Push(token)
}
//.....
//do some stuff
//.....
object a;
object b;
Token q = stack.Pop(token);
q.PerformOperation(a,b);
}
}
}
I want to create a new instance of a subclass where i don't want to know the name of it?
I "dont even know" if it exists?
Hopefully this wasn't too complicated...
EDIT:
I don't want to keep track of all the subclasses basically...
EDIT2 (New example):
Consider:
static void Main(string[] args)
{
Operator op;
string s = "*";
if(s==MulOperator.Identifier){
op = new MulOperator();
}
else{
op = new DivOperator();
}
perform(op);
}
static void perform(Operator op)
{
Console.WriteLine("Sum:{0}", op.PerformOperation(2.2,2.0));
}
Where I want to get rid of the new MulOperator()
and MulOperator.Identifier
with a more generic method of creating a subclass of in this case Operator
.
EDIT 3:*
(Solution)
I'm going to try A Generic Factory in C#,
it seems like that is what I want to achieve.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须编辑构造函数,以便它们将
string s
作为参数,并进行正确的验证和解析。You'll have to edit your constructors so they take
string s
as parameter, and do the proper validation and parsing.