Vb.net保护关键字问题 [英] Vb.net protected keyword question

查看:80
本文介绍了Vb.net保护关键字问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为Form1的WinForm,其中包含以下内容:



Suppose I have a WinForm named Form1 with the following:

Protected sampleVariable As String = "Hello World"





我创建一个名为Class1的派生类以下值:





And I create a derived class called Class1 with the following values inside:

Public Class Class1
   Inherits Form1

   Public Sub Sample()
      MsgBox(sampleVariable)
   End Sub
End Class





对不起,如果这也是noob-ish问。我已经使用了软件OOP已经有一段时间



我尝试过:



在Form1中,如何从Class1调用Sample()?应该是:



Sorry if this too noob-ish to ask. It's been a while since I've used Software OOP

What I have tried:

In Form1 how should I call Sample() from Class1? Should it be:

Dim c As New Class1
c.Sample()






or

Dim c = Class1
c.Sample()



或只是


or just

Class1.Sample()





这三项工作我我只是想知道哪种方法是正确的,因为谷歌目前还没有这方面的解决方案。



These 3 work I'm just wondering which is the correct way since there is currently no solution on Google for this.

推荐答案

为什么要在Form1中创建一个Class1对象? Class1已经是Form1的子级,因此继承了它的所有属性。
Why would you create a Class1 object inside Form1? Class1 is already a child of Form1 so inherits all its properties.


你永远不会这样做。 Form1永远不应该创建Class1的实例,因为Class1继承自Form1。



父类Form1应该提供从Form1继承的所有子类的所有内容需要每个孩子。父母不应该创建或了解任何子类。
You would NEVER do this. Form1 should NEVER create an instance of Class1 since Class1 inherits from Form1.

The parent class, Form1, should supply everything that all of the child classes inheriting from Form1 are going to need in every child. The parent should never create or know anything about any child classes.


这篇关于Vb.net保护关键字问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆