我创建了一个类说一个有定义为受保护的部分功能。
现在B类继承了A和C类继承B. A类有私有的默认构造函数和保护参数的构造函数。
我想B类为能够访问类中定义的所有保护功能,但C级可以对某些功能的访问不仅没有所有的功能类C继承B级。
我怎样才能限制访问某些A类从C类?
功能编辑:
命名空间分贝
{
大众A级
{
私人A(){}
保护A(字符串CON){分配该值}
保护数据表getTable(){返回表;}
保护Sqlparameters setParameters(){返回参数;}
}
}
空间数据
{
公共类B:
{
受保护的B():基座(constring){}
保护数据表输出(){返回getTable();}
受保护的SqlParameter值(参数的IDataParameter []参数){}
}
}
命名空间的BSN
{
公共类C:将B
{
保护的C-():碱(){}
受保护的数据表显示()
{返回值(通过setParameter());}
}
}
修改
我想我试图做的,是多重继承。
请检查。
A级
{
//假设10函数声明
}
B类:一
{
声明// 5功能,这在身体内部正在使用的功能
}
C类:乙
{
//使用B的所有功能,但需要的只有4种功能是由C.访问
}
解决方案
我建议你重新考虑你的设计。也许还有一个更简单的方法。如果C使用从它(组成)得出的B的实例,而不是么?这样,C可使用B的公开方法,但不能访问受保护的。
A类不应该关心后代的水平/深度。如果某物被标记保护,应当(无论继承链的深度的)保护的B和C两者。 B可以选择通过紧缩的限制来界定它的后代(但这是罕见的)。
如果你能告诉我更多关于你的背景 - 你正在试图解决。我可以给你一个更详细的/有用的答案的问题
I have created a class say A which has some functions defined as protected.
Now Class B inherits A and class C inherits B. Class A has private default constructor and protected parameterized constructor.
I want Class B to be able to access all the protected functions defined in Class A but class C can have access on some of the functions only not all the functions and class C is inheriting class B.
How can I restrict access to some of the functions of Class A from Class C ?
EDIT:
namespace Db
{
public class A
{
private A(){}
protected A(string con){assign this value}
protected DataTable getTable(){return Table;}
protected Sqlparameters setParameters(){return parameter;}
}
}
namespace Data
{
public class B:A
{
protected B():base("constring"){}
protected DataTable output(){return getTable();}
protected sqlparameter values(param IDataParameter[] parameter){}
}
}
namespace Bsns
{
public class C:B
{
protected C():base(){}
protected DataTable show()
{return values(setparameter());}
}
}
EDIT
I think what I am trying to do here is Multiple inheritance.
Please check.
class A
{
//suppose 10 functions are declared
}
class B:A
{
//5 functions declared which are using A's function in internal body
}
class C:B
{
//using all functions of B but require only 4 functions of A to be accessible by C.
}
解决方案
I'd suggest that you rethink your design. Maybe there is a simpler way. What if C uses an instance of B instead of deriving from it (composition) ? That way C can use B's public methods but not get access to the protected ones.
Class A should not care about the level/depth of a descendant. If something is marked protected, it should be protected for both B and C (regardless of the depth of the inheritance chain). B may choose to delimit its descendants by tightening the constraints (but this is rare).
If you can tell me more about your context - the problem you are trying to solve.. I can give you a more detailed/useful answer.