.NET 中属性的嵌套类访问方法

发布于 2024-10-13 05:55:39 字数 1008 浏览 7 评论 0原文

我试图找出在我正在创建的嵌套类中设置和获取属性的最佳方法。

我有一个类 Car,它有一个嵌套类 ControlPanel,并且希望使控制面板的属性只能由 Car 和控制面板类访问。

(即:不在程序集或命名空间内,也不在将要使用类库的应用程序内)...我已将类访问属性更改为friend、protectedfriend、private、public,但任何组合都不匹配我的预期结果。

我想更改类的 Drive() Sub 中的属性,如下所示。

有什么想法吗?

 Public Class Car

    Dim cp As New ControlPanel

    Public Class ControlPanel
      Private _Speedometer As Integer = 0
      Private _Odometer As Integer = 0

      Public Property Speedometer() As Integer
        Get
            Return _Speedometer
        End Get
        Protected Set(ByVal value As Integer)
            _Speedometer = value
        End Set
      End Property

      Public Property Odometer() As Integer
        Get
            Return _Odometer
        End Get
        Protected Set(ByVal value As Integer)
            _Odometer = value
        End Set
     End Property

    End Class

   Public Sub Drive()

        cp.Odometer = 76323
        co.Speedometer = 86

   End Sub

End Class

I am trying to figure out the best approach for setting and getting properties in a nested class I am creating.

I have a class, Car which has a nested class ControlPanel and want to make the properties of the Control Panel only accessible to the Car and Control Panel class.

(ie: not within the assembly or namespace and not within the application the class library will be going to be used)... I have changed the class access properties to friend, protected friend, private, public, but any combination is not matching my expected results.

I want to change the properties in the Drive() Sub of a class as shown below.

Any thoughts?

 Public Class Car

    Dim cp As New ControlPanel

    Public Class ControlPanel
      Private _Speedometer As Integer = 0
      Private _Odometer As Integer = 0

      Public Property Speedometer() As Integer
        Get
            Return _Speedometer
        End Get
        Protected Set(ByVal value As Integer)
            _Speedometer = value
        End Set
      End Property

      Public Property Odometer() As Integer
        Get
            Return _Odometer
        End Get
        Protected Set(ByVal value As Integer)
            _Odometer = value
        End Set
     End Property

    End Class

   Public Sub Drive()

        cp.Odometer = 76323
        co.Speedometer = 86

   End Sub

End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光匆匆的小流年 2024-10-20 05:55:39

正如 Robert Levy 指出的那样,您指的是“嵌套类”而不是“子类”等。

至于如何实现您正在寻找的内容......您只是想使 ControlPanel 成为私有类吗?这将确保 ControlPanel 的所有成员只能由 Car 访问。如果 ControlPanel 上有其他需要公开的成员,或者外部世界需要以某种方式保存对 ControlPanel 的引用,请考虑使用接口来仅公开那些您希望公开可用的成员。

Public Class Car

  Dim cp As New ControlPanel

  Private Class ControlPanel

    Public Property Speedometer As Integer
    Public Property Odometer As Integer

  End Class

  Public Sub Drive()
    cp.Odometer = 76323
    cp.Speedometer = 86
  End Sub

End Class

或者...

Friend Interface IControlPanel
  //Whatever actually needs to be publically accessible.    
End Interface

// Other Code...

Private Class ControlPanel
  Implements IControlPanel

// Other Code...

您在 API 方面试图实现的目标是什么?

As Robert Levy pointed out, you are referring to a "Nested Class" and not a "Subclass" etc.

As for how to achieve what you are looking for... are you simply looking to make ControlPanel a private class? That will ensure that all members of ControlPanel are only accessible to Car. If you have other members on ControlPanel that need to be exposed, or the outside outside world needs to hold a reference to ControlPanel in someway, consider using an Interface to expose only those members that you want to be publically available.

Public Class Car

  Dim cp As New ControlPanel

  Private Class ControlPanel

    Public Property Speedometer As Integer
    Public Property Odometer As Integer

  End Class

  Public Sub Drive()
    cp.Odometer = 76323
    cp.Speedometer = 86
  End Sub

End Class

Optionally...

Friend Interface IControlPanel
  //Whatever actually needs to be publically accessible.    
End Interface

// Other Code...

Private Class ControlPanel
  Implements IControlPanel

// Other Code...

What is the goal that you are trying to achieve in terms of API?

相权↑美人 2024-10-20 05:55:39

您可以这样做:

Public Class Car

  Private Interface IControlPanel
    Property Odometer As Integer
    Property Speedometer As Integer
  End Interface

  Public Class ControlPanel
    Implements IControlPanel
    Public ReadOnly Property Odometer As Integer
      Get
        Return CType(Me, IControlPanel).Odometer
      End Get
    End Property
    Public ReadOnly Property Speedometer As Integer
      Get
        Return CType(Me, IControlPanel).Speedometer
      End Get
    End Property
    Private Property IControlPanel_Odometer As Integer Implements IControlPanel.Odometer
    Private Property IControlPanel_Speedometer As Integer Implements IControlPanel.Speedometer
  End Class

  Dim cp As IControlPanel = New ControlPanel()

  Public Sub Drive()
    cp.Odometer = 76323
    cp.Speedometer = 86 
  End Sub

End Class

它使用嵌套在 Car 类中的私有接口,并在 ControlPanel 中使用私有实现和别名成员。这样,只有 Car 可以访问接口成员。

You can do it like this:

Public Class Car

  Private Interface IControlPanel
    Property Odometer As Integer
    Property Speedometer As Integer
  End Interface

  Public Class ControlPanel
    Implements IControlPanel
    Public ReadOnly Property Odometer As Integer
      Get
        Return CType(Me, IControlPanel).Odometer
      End Get
    End Property
    Public ReadOnly Property Speedometer As Integer
      Get
        Return CType(Me, IControlPanel).Speedometer
      End Get
    End Property
    Private Property IControlPanel_Odometer As Integer Implements IControlPanel.Odometer
    Private Property IControlPanel_Speedometer As Integer Implements IControlPanel.Speedometer
  End Class

  Dim cp As IControlPanel = New ControlPanel()

  Public Sub Drive()
    cp.Odometer = 76323
    cp.Speedometer = 86 
  End Sub

End Class

It uses a private interface nested in the Car class with privately implemented and aliased members in ControlPanel. This way, only Car can access the interface members.

太阳男子 2024-10-20 05:55:39

首先,让我们澄清术语。你在这里谈论的是一个“嵌套”类,而不是一个子类,

我不相信你想要做的事情在.NET中是可能的。然而,你真的需要它吗?如果 cp 在汽车内是私有的,则没有人可以增加该汽车控制面板上的里程表。他们可以创建自己的不属于汽车一部分的控制面板,然后对其进行破坏,但这有什么坏处呢?

First, let's clarify terminology. What you are talking about here is a "nested" class and not a subclass

I dont believe what you are trying to do is possible in .NET. However, do you really need it? If cp is private within Car, nobody can increment the odometer on that car's control panel. They could create their own controlpanel that isn't part of a car and then mess with it, but what's the harm in that?

冷血 2024-10-20 05:55:39

将您的 ControlPanel 类设为私有,并将其属性设为公开。然后,ControlPanel 类将仅对 Car 类型可见,但 Car 仍然能够修改 ControlPanel的属性。

Make your ControlPanel class private and its properties public. The ControlPanel class will then only be visible to the Car type, but Car will still be able to modify ControlPanel's properties.

乱世争霸 2024-10-20 05:55:39

嵌套类是一种方法,或者您可以将您的汽车作为复合类。控制面板可用于许多不同的汽车(子类),那么为什么要使其嵌套呢?汽车有一个控制面板。汽车有发动机

总线:汽车也有那些东西。

收音机可能是控制面板上有的东西。车速表可以是仪表板的一部分,也可以是控制面板的一部分。

然后你最终得到

MyCar.ItsControlPanel.Radio
MyCar.ItsControlPanel.Dashboard.Speedometer.CurrentSpeed;

摩托车怎么样?它不是汽车,但它仍然有一个带有车速表的仪表板。

明白我的意思了吗?复合对象。

nested classes is one way to go or you can have your Car as a composite class. Control panel can be used in many different cars (sub classes) so why make it nested? Car has a Control Panel. Car has an Engine

Bus : Car also has those things.

Radio might be something that Control Panel has. Speedometer could be part of Dashboard that can also be part of the Control Panel.

Then you end up with

MyCar.ItsControlPanel.Radio
MyCar.ItsControlPanel.Dashboard.Speedometer.CurrentSpeed;

How about Motorcycle? It isnt a car but it still has a Dashboard which has a Speedometer.

Get my point? Composite objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文