转换 x时出现问题到x
嘿伙计们,我无法使用这段代码进行管理。这个想法是在创建方法执行错误的情况下返回默认的英文字母。谢谢。
覆盖显式运算符的想法很好,但我无法想象强制转换的实现。
namespace trie
{
class AlphabetFactory<T> where T: IConvertible
{
public Alphabet<char> SetDefaultAlphabet()
{
var list = new char[26];
for (var i = Convert.ToInt32('a'); i <= Convert.ToInt32('z'); i++)
list[i] = Convert.ToChar(i);
return new Alphabet<char>(list);
}
public Alphabet<T> Create(params T[] list)
{
if (list != null)
return new Alphabet<T>(list);
else
return SetDefaultAlphabet(); // <- This is not correct
}
}
}
Hey guys I cant manage with this code. The idea is to return default English alphabet in case of erroneous create method execution. Thanks.
An idea to override explicit operator is good, but i cant imagine an implementation of casting.
namespace trie
{
class AlphabetFactory<T> where T: IConvertible
{
public Alphabet<char> SetDefaultAlphabet()
{
var list = new char[26];
for (var i = Convert.ToInt32('a'); i <= Convert.ToInt32('z'); i++)
list[i] = Convert.ToChar(i);
return new Alphabet<char>(list);
}
public Alphabet<T> Create(params T[] list)
{
if (list != null)
return new Alphabet<T>(list);
else
return SetDefaultAlphabet(); // <- This is not correct
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为完成您想要的任务的唯一方法是使 Alphabet从非泛型抽象基继承,或者实现非泛型接口。那么您的代码可能如下所示:
不过,这将使使用
AlphabetFactory
的客户端代码使用起来更加麻烦:The only way I can think to accomplish what you want is to make
Alphabet<T>
inherit from a non-generic abstract base, or else implement a non-generic interface. Then your code might look something like this:This would make client code using your
AlphabetFactory
a bit more cumbersome to use, though: