开发者

How to hide methods from base class?

I use Castle Active Record' ActiveRecordBase class as a base class for my db objects. That allows me to take advantage of the AR pattern goodies, like:

MyClass clas = new MyClass();
clas.Number = 5;
clas.Save();

The problem is, for some objects, I want to hide these methods. How can I do that? I don't use interfaces for these objects.

Edit1: To make it more clear: I want to have 10 classes inheriting from the base one, but 3 of them shouldn't expose some of the methods from the base class. So for example for MyClassB, this:

MyClassB clasB = new MyClassB();
clasB.Number = 5;
clasB.Save();

shouldn't be possible - this object SHOULDN'T be able to Save().

Edit2: I've read c# hide from dervied classes some properties of base class . I agree that this is ba开发者_StackOverflow中文版d approach, but bear in mind that I have to use ActiveRecordBase, which I cannot simply modify (at least if I want to stick with the original Castle Active Record sources).


You don't do this.

You restructure your design so that the 7 objects inherit from one object that provides the functionality they want, the other 3 inherit from one that doesn't provide this, and they all inherit from the main "core" object. Think of it like a tree. Instead of trying to hide leaves from view, you simple change the location of the branch. I'm not sure this analogy works, but lets pretend it does.


If you want to hide them, the probably do not belong in that base class. I suggest expanding the inheritance hierarchy to achieve this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜