C# 有没有办法在子类中部分隐藏方法?
这个问题看起来很奇怪,但我最近在一次采访中遇到了这个问题 有人问我,在c语言中有没有办法将部分方法隐藏在继承的子类中?。假设基类为A,公开了4个方法。类B实现了A,它将只能访问前2个方法,而类C实现了A将只能访问后2个方法 我知道我们可以这样做C# 有没有办法在子类中部分隐藏方法?,c#,inheritance,interface,C#,Inheritance,Interface,这个问题看起来很奇怪,但我最近在一次采访中遇到了这个问题 有人问我,在c语言中有没有办法将部分方法隐藏在继承的子类中?。假设基类为A,公开了4个方法。类B实现了A,它将只能访问前2个方法,而类C实现了A将只能访问后2个方法 我知道我们可以这样做 public interface IFirstOne { void method1(); void method2(); } public interface ISecondOne { void method3(
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();
}
}
在c语言中有没有一种方法可以实现这样的效果…尽管你不能完全按照自己的意愿去做,但是你可以使用显式接口实现来帮助我们,在这种方法中,只有当接口成员显式地被转换到该接口时,接口成员才会被公开…也许面试官提到过 在这里,您可以声明一个与基类中的on具有相同签名的方法,但不使用
override
关键字(因为您不使用或不能使用该关键字,就像基类中的方法是非虚拟的一样)
方法隐藏,与之相反,允许您定义一个完全不同的方法-一个只能通过对派生类的引用调用的方法。如果通过对基类的引用调用,您将调用基类上的原始方法。不要使用继承。它使基类的公共设施或受保护设施直接在派生类中可用,因此它根本不是您想要的 相反,让派生类实现相关接口,并(如有必要)将方法转发到基础类的私有实例上。也就是说,使用组合(或“聚合”)而不是继承来扩展原始类
class firstChild : IFirstOne
{
private baseClass _owned = new baseClass();
public void method1() { _owned.method1(); }
// etc.
}
顺便说一下,类名应该以大写字母开头。有两种方法可以隐藏从基类继承的方法:
- 正如coop所提到的,您可以显式地实现声明要隐藏的方法的接口
- 或者,您可以简单地在基类中创建这些方法(不是从任何接口继承的),并将它们标记为private
关于。如何将基类作为
IFirst
注入
interface IFirst {
void method1();
void method2();
}
interface ISecond {
void method3();
void method4();
}
abstract class Base : IFirst, ISecond {
public abstract void method1();
public abstract void method2();
public abstract void method3();
public abstract void method4();
}
class FirstChild : IFirst {
private readonly IFirst _first;
public FirstChild(IFirst first) {
_first = first;
}
public void method1() { _first.method1(); }
public void method2() { _first.method2(); }
}
注射可以防止你违反规定。纯继承意味着您的
FirstChild
依赖于它不使用的接口。如果您只想保留Base
中的IFirst
功能,而忽略它的其余部分,那么您不能纯粹从Base
继承我是通过拥有1个主基类和2个子基类来实现的
// Start with Base class of all methods
public class MyBase
{
protected void Method1()
{
}
protected void Method2()
{
}
protected void Method3()
{
}
protected void Method4()
{
}
}
// Create a A base class only exposing the methods that are allowed to the A class
public class MyBaseA : MyBase
{
public new void Method1()
{
base.Method1();
}
public new void Method2()
{
base.Method2();
}
}
// Create a A base class only exposing the methods that are allowed to the B class
public class MyBaseB : MyBase
{
public new void Method3()
{
base.Method3();
}
public new void Method4()
{
base.Method4();
}
}
// Create classes A and B
public class A : MyBaseA {}
public class B : MyBaseB {}
public class MyClass
{
void Test()
{
A a = new A();
// No access to Method 3 or 4
a.Method1();
a.Method2();
B b = new B();
// No Access to 1 or 2
b.Method3();
b.Method4();
}
}
@是的,我们可以这样做,但问题是在继承基类本身时,方法必须被隐藏。。。
// Start with Base class of all methods
public class MyBase
{
protected void Method1()
{
}
protected void Method2()
{
}
protected void Method3()
{
}
protected void Method4()
{
}
}
// Create a A base class only exposing the methods that are allowed to the A class
public class MyBaseA : MyBase
{
public new void Method1()
{
base.Method1();
}
public new void Method2()
{
base.Method2();
}
}
// Create a A base class only exposing the methods that are allowed to the B class
public class MyBaseB : MyBase
{
public new void Method3()
{
base.Method3();
}
public new void Method4()
{
base.Method4();
}
}
// Create classes A and B
public class A : MyBaseA {}
public class B : MyBaseB {}
public class MyClass
{
void Test()
{
A a = new A();
// No access to Method 3 or 4
a.Method1();
a.Method2();
B b = new B();
// No Access to 1 or 2
b.Method3();
b.Method4();
}
}