问题描述
让我们说我有一个班级
班级基础类
{
...
public int someProperty
{
....
}
}
现在我创建了新的类,我使用了someProperty,但是我不希望我班级的
用户使用它,而不是设计时间,不是实时....
let''s say i have a class
class baseclass
{
...
public int someProperty
{
....
}
}
and now i create new class, there i use "someProperty", but don''t want the
users of my class use it, not in design time, not in real time....
推荐答案
很难猜到你在这里得到了什么。
必然是一个适合您情况的理想架构。并且有一个相当高的百分比,我不会猜对了。但是一种方法是:
抽象类基类
{
public abstract int someProperty {get; }
}
密封类newclass:baseclass
{
public override int someProperty
{
get
{
返回1;
}
}
}
class newclassForUser:baseclass
{
public override int someProperty
{
get
{
返回0;
}
}
}
这样可以防止用户继承你的密封消息。类和行为。
它还使它们实现了自己的someProperty版本。但是那个
可能是微不足道的,从未使用过。
希望在某种程度上有所帮助。
问候,
兰迪
It''s difficult to guess at what you are getting at here. There is bound to
be an ideal architecture that fits your situation. And there is a pretty
high percentage that I will not guess the right one. But one approach is:
abstract class baseclass
{
public abstract int someProperty{ get; }
}
sealed class newclass : baseclass
{
public override int someProperty
{
get
{
return 1;
}
}
}
class newclassForUser : baseclass
{
public override int someProperty
{
get
{
return 0;
}
}
}
This keeps the user from inheriting from your "sealed" class and behavior.
It also makes them implement their own version of "someProperty," but that
can be trivial and never used.
Hope that helps in some way.
Regards,
Randy
这篇关于OOP - OOD问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!