Esta pregunta parece extraña, pero me encontré con esta pregunta en una de las entrevistas recientemente.¿Hay alguna manera de ocultar los métodos parcialmente en clases para niños?
Me han preguntado, ¿hay alguna manera en C# para ocultar los métodos parcialmente en una clase hija heredada ?. Suponga que la clase base A, expuso 4 métodos. La Clase B implementa A y solo tendrá acceso a los primeros 2 métodos y los implementos de Clase C A solo tendrán acceso a los 2 últimos métodos.
Sé que podemos hacerlo de esta manera
public interface IFirstOne
{
void method1();
void method2();
}
public interface ISecondOne
{
void method3();
void method4();
}
class baseClass : IFirstOne, ISecondOne
{
#region IFirstOne Members
public void method1()
{
throw new NotImplementedException();
}
public void method2()
{
throw new NotImplementedException();
}
#endregion
#region ISecondOne Members
public void method3()
{
throw new NotImplementedException();
}
public void method4()
{
throw new NotImplementedException();
}
#endregion
}
class firstChild<T> where T : IFirstOne, new()
{
public void DoTest()
{
T objt = new T();
objt.method1();
objt.method2();
}
}
class secondChild<T> where T : ISecondOne, new()
{
public void DoTest()
{
T objt = new T();
objt.method3();
objt.method4();
}
}
Pero lo que querían es diferente. Querían ocultar estas clases sobre heredar de clases base. algo como esto
class baseClass : IFirstOne, ISecondOne
{
#region IFirstOne Members
baseClass()
{
}
public void method1()
{
throw new NotImplementedException();
}
public void method2()
{
throw new NotImplementedException();
}
#endregion
#region ISecondOne Members
public void method3()
{
throw new NotImplementedException();
}
public void method4()
{
throw new NotImplementedException();
}
#endregion
}
class firstChild : baseClass.IFirstOne //I know this syntax is weird, but something similar in the functionality
{
public void DoTest()
{
method1();
method2();
}
}
class secondChild : baseClass.ISecondOne
{
public void DoTest()
{
method3();
method4();
}
}
hay una manera en C# podemos lograr algo como esto ...
@thecoop, sí podemos hacerlo de esa manera ..., pero el problema es que los métodos deben ocultarse al heredar la clase base en sí ... – RameshVel