如何从继承的类访问类

发布于 2024-12-09 15:29:27 字数 172 浏览 0 评论 0原文

我有两个类:

class class2
inherits class1

public sub modify()

'modify property of class1

end sub

end class

如何修改 class2 中的子类中的 class1?

I have two classes:

class class2
inherits class1

public sub modify()

'modify property of class1

end sub

end class

How can I modify class1 in a sub in class2?

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

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

发布评论

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

评论(3

你只要调用它即可。示例:

Public Class class1
  Private _Value As String = String.Empty

  Property Value() As String
    Get
      Return _Value
    End Get
    Set(ByVal value As String)
      _Value = value
    End Set
  End Property

End Class

Public Class class2
  Inherits class1

  Public Sub modify()
    Value = "modified"
  End Sub

End Class

并展示它的工作原理:

Dim c2 As New class2
c2.modify()
MessageBox.Show(c2.Value)

You just call it. Example:

Public Class class1
  Private _Value As String = String.Empty

  Property Value() As String
    Get
      Return _Value
    End Get
    Set(ByVal value As String)
      _Value = value
    End Set
  End Property

End Class

Public Class class2
  Inherits class1

  Public Sub modify()
    Value = "modified"
  End Sub

End Class

And to show it works:

Dim c2 As New class2
c2.modify()
MessageBox.Show(c2.Value)
滥情稳全场 2024-12-16 15:29:27

您询问的是属性,请注意,只有 protectedpublic 属性对继承的类可见。
当您重写父类中的现有函数时,您需要 MyBase 关键字。其他protectedpublic属性或函数可以定期访问,无需任何特殊关键字。

You are asking about properties, note that only protected and public properties are visible to inherited classes.
You need the MyBase keyword when you are overriding an existing function in the parent class. Other protected or public properties or functions can be accessed regulary without any special keyword.

泛滥成性 2024-12-16 15:29:27

我想在上述有关访问基类信息的评论中添加一个提示,即您有一个没有默认构造函数的基类,或者想要使用特定的构造函数,这是使用 Mybase 的好机会。在这种情况下,您必须先调用构造函数,然后再执行任何其他操作。

<代码>

Public Class MyClass
  Inherits baseClass
  Public Sub New()
   mybase.new("Oranges")
  End Sub
End Class

Public Class baseClass
Private _someVariable as String
Public Sub New(byval passedString as string)
   _someVariable = passedString
End Sub
End Class

<代码>

One tip I wanted to add to the above comments regarding accessing base class info is where you have a base class without a default contructor or want to use a specific constructor This is a good opportunity to use Mybase. You have to call the constructor before any additional actions take place in this scenario.

Public Class MyClass
  Inherits baseClass
  Public Sub New()
   mybase.new("Oranges")
  End Sub
End Class

Public Class baseClass
Private _someVariable as String
Public Sub New(byval passedString as string)
   _someVariable = passedString
End Sub
End Class

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