类概念(访问属性然后访问方法的类) [英] Class concept (a class accessing property then accessing method)
问题描述
Hello Everyone
我有一个名为Car的课程:
Hello Everyone
I have a class called Car:
Public Class Car
'Property
Public theColor() As Color
'Method
Public speed As Double
Public Function run()
Return speed
End Function
End Class
在另一个类中我创建一个Ca实例r类:
In another class I create an instance of Car Class:
Public Class SUV
Dim newCar As New Car
Private Sub setColor()
'set theColor property
newCar.theColor = Color.Red
End Sub
Private Sub testDrive()
'call run() function
newCar.run()
End Sub
End Class
从上面的代码中,
如果我想设置一个类的属性我只写一个实例和属性:
newCar.theColor = Color.Red
此外,如果我想调用类的方法,我只需编写一个实例,然后调用函数或子函数:
newCar.run()
但是我不明白一行代码怎么会这样,例如:
From the code above,
If I want to set a property of a class I just write an instance and the property:
newCar.theColor = Color.Red
Also if I want to call a method of a class I just write an instance then call the function or sub:
newCar.run()
But I dont understand how can a line of code be like this, for example:
Dim dgv As New DataGridView
dgv.Rows.Add()
上面的代码显示实例(dgv)调用属性(Rows)而不是调用方法(添加())
回到我班级(汽车和SUV)
怎么写如果我想调用这样的方法,我班上的代码?
The code above show that an instance (dgv) call property (Rows) than call a method (Add())
Back to my class (the Car And SUV)
How to write the code in my class if i want to call the method like this?
newCar.theColor.resetColor()
和另一个代码:
And another code:
newCar.theColor.setColor() = Color.Red
如果问题标题与我提出的问题不符,我很抱歉。
谢谢
我的尝试:
我不知道如何尝试解决上面的问题
I am sorry if the question's title isnt match with what i've asked.
Thanks
What I have tried:
I have no idea how to to try to solve my question above
推荐答案
当你写这个:
When you write this:
Dim dgv As New DataGridView
dgv.Rows.Add(obj)
您创建DataGridView的实例,然后使用它:
dgv.Rows
是DataGridView的一个属性,它返回DataGridViewRowCollection类的一个实例,它是DGV中的行:MSDN [ ^ ]
DataGridViewRowCollection有一个方法添加
,它会在集合中添加一行,因此对于DGV。
要做类似的事情,你需要额外的课程:
You create an instance of the DataGridView, and then use it:
dgv.Rows
is a Property of DataGridView which returns a n instance of the DataGridViewRowCollection class which is the rows in the DGV: MSDN[^]
The DataGridViewRowCollection has a Method Add
which adds a row to the collection, and hence to the DGV.
To do something similar, you'd need an extra class:
Public Class MyColor
Public Sub ResetColor()
End Sub
End Class
Public Class Car
Public Property TheColor() As MyColor
Get
Return m_TheColor
End Get
Set
m_TheColor = Value
End Set
End Property
Private m_TheColor As MyColor
End Class
...
Public Sub myMethod()
Dim newCar As New Car()
newCar.TheColor.ResetColor()
End Sub
我们是否采用类似的方式在另一种语言中创建属性或方法?像java或c#。这是OOP概念的一部分吗?我在学习编程时错过了什么?
an we do the similiar way to create property or method in another language? like java or c#. And is this a part of OOP concept? what did i miss in my learning of programming?
是的。例如,C#中的上述内容是:
Yes. For example, the above in C# is:
public class MyColor
{
public void ResetColor()
{
}
}
public class Car
{
public MyColor TheColor { get; set; }
}
...
public void myMethod()
{
Car newCar = new Car();
newCar.TheColor.ResetColor();
}
这是类似的Java代码。
属性不是OOP的严格组成部分 - 它们是只是涉及方法调用的语法糖,让你将属性视为一个变量 - 但大多数OOP语言实现它们。
你错过了什么? :笑:我不知道,我不在那里!
And it's similar code for Java.
Properties aren't strictly part of OOPs - they are just "syntactic sugar" for the method calls involved that let you treat the property as a variable - but most OOPs languages implement them.
What did you miss? :laugh: I don't know, I wasn't there!
我强烈建议您阅读继承 [ ^ ]和 Polimorphism [ ^ ]。
A汽车
类必须是基类。Suv
类应继承自Car
(因为它是一种Car
)。我是对的吗?
例如:
I'd strongly recommend to read about Inheritance[^] and Polimorphism[^].
ACar
class have to be a base class. ASuv
class should inherits fromCar
(as it is a type ofCar
). Am i right?
For example:
Public Class Car
Private dc As Integer = 5
Private c As Drawing.Color = Drawing.Color.Gray
Public Sub New ()
'default constructor
End Sub
Public Sub New (ByVal doorsCount As Integer, ByVal color As Drawing.Color)
dc = doorsCount
c= color
End Sub
Public Property DoorsCount As Integer
Get
Return dc
End Get
Set (ByVal value As Integer)
dc = value
End Set
End Property
Public Property Color As Drawing.Color
Get
Return c
End Get
Set (ByVal value As Drawing.Color)
c = value
End Set
End Property
End Class
Public Class Suv
Inherits Car
Public Sub New ()
'default constructor
MyBase.DoorsCount = 4
MyBase.Color = Drawing.Color.Green
End Sub
Public Sub New (ByVal doorsCount As Integer, ByVal color As Drawing.Color)
MyBase.DoorsCount = doorsCount
MyBase.Color = color
End Sub
End Class
用法:
Usage:
Dim carcar As Car = New Car()
Console.WriteLine(New String("-", 25))
Console.WriteLine("Car details:")
Console.WriteLine(" - doors: {0}", carcar.DoorsCount)
Console.WriteLine(" - color: {0}", carcar.Color.ToString())
Dim suvcar As Suv = New Suv()
Console.WriteLine(New String("-", 25))
Console.WriteLine("Suv details:")
Console.WriteLine(" - doors: {0}", suvcar.DoorsCount)
Console.WriteLine(" - color: {0}", suvcar.Color.ToString())
suvcar.DoorsCount = 3
suvcar.Color = Drawing.Color.Yellow
Console.WriteLine(New String("-", 25))
Console.WriteLine("Suv details:")
Console.WriteLine(" - doors: {0}", suvcar.DoorsCount)
Console.WriteLine(" - color: {0}", suvcar.Color.ToString())
结果:
Result:
-------------------------
Car details:
- doors: 5
- color: Color [Gray]
-------------------------
Suv details:
- doors: 4
- color: Color [Green]
-------------------------
Suv details:
- doors: 3
- color: Color [Yellow]
如您所见,您不需要在自定义方法中使用getter / setter。你可以只用属性来实现。
试试吧!祝你好运!
As you can see, you don't need getter/setter inside custom method. You can achieve that using only properties.
Try! Good luck!
这篇关于类概念(访问属性然后访问方法的类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!