关于类继承的问题,子类中能不能屏蔽父类的一些公共方法?

webfactory 2005-03-30 03:04:26
在类继承时,我想在子类中屏蔽父类的一些方法(太多了,有些根本用不着了),如何办呢?
...全文
353 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
比尔咔咔 2005-04-15
  • 打赏
  • 举报
回复
[Browsable(false)]
public virtual int Count { get; }
请加[Browable(false)]
Ivony 2005-03-30
  • 打赏
  • 举报
回复
你可以利用微软的一个小Bug来解决这个问题。

覆盖这个方法,并加上Attribute:[Obsolete("", true)]。

这只能改善显示效果,另外,也能使得派生类的对象在调用这些方法的时候产生编译错误。但无法避免基类类型的派生类对象调用这些方法。
梅青松 2005-03-30
  • 打赏
  • 举报
回复
显式接口实现可能是你要的
实现接口的类可以显式实现该接口的成员。当显式实现某成员时,不能通过类实例访问该成员,而只能通过该接口的实例访问该成员。
示例 1
本示例声明一个 IDimensions 接口和一个 Box 类,该类显式实现接口成员 Length 和 Width。通过接口实例 myDimensions 访问这些成员。
// explicit1.cs
interface IDimensions
{
float Length();
float Width();
}

class Box : IDimensions
{
float lengthInches;
float widthInches;

public Box(float length, float width)
{
lengthInches = length;
widthInches = width;
}
// Explicit interface member implementation:
float IDimensions.Length()
{
return lengthInches;
}
// Explicit interface member implementation:
float IDimensions.Width()
{
return widthInches;
}

public static void Main()
{
// Declare a class instance "myBox":
Box myBox = new Box(30.0f, 20.0f);
// Declare an interface instance "myDimensions":
IDimensions myDimensions = (IDimensions) myBox;
// Print out the dimensions of the box:
/* The following commented lines would produce compilation
errors because they try to access an explicitly implemented
interface member from a class instance: */
//System.Console.WriteLine("Length: {0}", myBox.Length());
//System.Console.WriteLine("Width: {0}", myBox.Width());
/* Print out the dimensions of the box by calling the methods
from an instance of the interface: */
System.Console.WriteLine("Length: {0}", myDimensions.Length());
System.Console.WriteLine("Width: {0}", myDimensions.Width());
}
}
输出
Length: 30
Width: 20
ruihuahan 2005-03-30
  • 打赏
  • 举报
回复
想屏蔽他们,免得在vs.net中输入类时,出来一大片方法,选择起来很麻烦----有必要吗?
webfactory 2005-03-30
  • 打赏
  • 举报
回复
楼上大家说的很对,我也能理解,不过,由于多次的继承,以前许多公共方法都没用了,我想屏蔽他们,免得在vs.net中输入类时,出来一大片方法,选择起来很麻烦
srz007 2005-03-30
  • 打赏
  • 举报
回复
class A
{
public void qqq()
{}
}
class B : A
{
public new void qqq()
{}
}
Afritxia 2005-03-30
  • 打赏
  • 举报
回复
可以,但是应该将类中不用的部分提到一个新类里。

继承是对类的扩充,而不是屏蔽...
梅青松 2005-03-30
  • 打赏
  • 举报
回复
虚函数可以在重载中抛出异常,
非虚函数可以覆盖
Ivony 2005-03-30
  • 打赏
  • 举报
回复
override 然后抛出异常。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧