如何在VB.NET中的派生类中从基类中抑制属性或方法?

时间:2011-04-07 13:17:20

标签: .net vb.net oop

设置基类

Public Class airplane

Private var_num_seats As Integer
Private var_num_engines As Integer

Protected Property num_seats As Integer
    Get
        Return var_num_seats
    End Get
    Set(ByVal param_num_seats As Integer)
        var_num_seats = param_num_seats
    End Set
End Property

Protected Friend Property num_engines As Integer
    Get
        Return var_num_engines

    End Get
    Set(ByVal param_num_engines As Integer)
        var_num_engines = param_num_engines
    End Set
End Property

Protected Friend Sub take_off()
    'Do take off tasks
End Sub

Protected Friend Sub start_engines()
    'start each engine
End Sub

End Class

和一个儿童班

Public Class glider
Inherits airplane

Private var_towed As Boolean
Private var_glide_rate As Double

Public ReadOnly Property towed As Boolean
    Get
        Return var_towed
    End Get
End Property

Public ReadOnly Property glide_rate As Double
    Get
        Return var_glide_rate
    End Get
End Property

Public Sub to_glide()
    'do gliding
End Sub

End Class

显然,我不希望类Glider的方法“start_engines”既不是属性“num_engines”。否则,其他子课程可能有。我怎样才能在子类中压制这些属性和方法,而不仅仅是忽略(如果可以的话)?

谢谢!

2 个答案:

答案 0 :(得分:3)

你不能!

原因:打破面向对象!!!!

假设你有:

public void ThisIsAMethod(Airplane plane)
{
    plane.start_engines();
}

现在你这样使用它:

Airplane aPlane = new Glider();
ThisIsAMethod(aPlane);

哦不,aPlane真的是一个滑翔机,所以你不应该打电话给它。但是,它是一架飞机,因此有了这种方法。

解决方案:

  1. 形成真正的遗产。滑翔机不是飞机。也许你可以拥有“FlyingMachine”或“WingedFlier”的基类或者它们都能延伸的东西。
  2. Hackmode!在滑翔机:

    受保护的朋友Sub start_engines()     抛出新的NotSupportedException() End Sub

答案 1 :(得分:1)

我怀疑你正在寻找使用界面。

http://msdn.microsoft.com/en-us/library/h9xt0sdd.aspx