创建对象

时间:2016-09-12 09:58:47

标签: vb.net clone deep-copy

我需要帮助深度复制VB.net中的对象。我知道有很多主题涉及到这个问题,但我无法使其适应我的问题。所以希望有人可以使用我的代码向我解释。

问题:我设计了一个类clsParameter,它有一个名称,一个单元,一个值类型和一个值。该值可以是double或clsVectorParameter类型的对象,其属性为X,Y,Z。现在我想对参数进行深度复制,以便复制X,Y,Z。

这是两个班级。下面的克隆函数只代表一个虚拟。我知道它不会像这样工作,但我不知道更好的方式......

Public Class clsParameter
' Using the ICloneable interface
Implements ICloneable

' Variable definition
Private m_Name As String
Private m_Unit As String
Private m_Type As String
Private m_Value As Object

' Define set and get methods
Public Property Name As String
    Get
        Return m_Name
    End Get
    Set(ByVal value As String)
        m_Name = value
    End Set
End Property

Public Property Unit As String
    Get
        Return m_Unit
    End Get
    Set(ByVal value As String)
        m_Unit = value
    End Set
End Property


Public Property Value As Object
    Get
        Return m_Value
    End Get
    Set(ByVal value As Object)
        m_Value = value
    End Set
End Property

Public Property Type As String
    Get
        Return m_Type
    End Get
    Set(ByVal value As String)
        m_Type = value
    End Set
End Property

' Define constructor
Public Sub New(ByVal p_Name As String, ByVal p_Unit As String, ByVal p_Value As Object, ByVal p_Type As String)
    m_Name = p_Name
    m_Unit = p_Unit
    m_Type = p_Type
    m_Value = p_Value
End Sub

' Define Clone function to create independent copies of parameter instances
Public Function Clone() As Object Implements System.ICloneable.Clone
    Dim cloneParam As New clsParameter(m_Name, m_Unit, m_Value, m_Type)
    Return cloneParam
End Function
End Class

和另一个班级:

Public Class clsVectorParameter

Implements ICloneable


' Variable definition
Private m_x As Double
Private m_y As Double
Private m_z As Double

Public Property X As Double
    Get
        Return m_x
    End Get
    Set(ByVal value As Double)
        m_x = value
    End Set
End Property


Public Property Y As Double
    Get
        Return m_y
    End Get
    Set(ByVal value As Double)
        m_y = value
    End Set
End Property


Public Property Z As Double
    Get
        Return m_z
    End Get
    Set(ByVal value As Double)
        m_z = value
    End Set
End Property

' Define constructor
Public Sub New(ByVal p_x As Double, ByVal p_y As Double, ByVal p_z As Double)
    m_x = p_x
    m_y = p_y
    m_z = p_z
End Sub

' Define Clone function to create independent copies
Public Function Clone() As Object Implements System.ICloneable.Clone
    Dim cloneVecParam As New clsParameter(m_x, m_y, m_z, "Vec")
    Return cloneVecParam
End Function
End Class

我在这一行中使用了这个类:

Dim aNewParam As New clsParameter("Name", "Unit", New clsVectorParameter(x,y,z), "Type")

Dim aNewParam As New clsParameter("Name", "Unit", Double, "Type")

稍后我需要创建此aNewParam的深层副本,因此x,y,z值对于所有参数也是独立的。

非常感谢你的帮助! 最好的祝福, 塞巴斯蒂安

2 个答案:

答案 0 :(得分:0)

为了保持代码符合例如创建XElement的克隆,请从构造函数执行此操作:

Dim obj1 = new clsVectorParameter(1, 1, 1)
Dim obj2 = new clsVectorParameter(obj1)

所以现在你只需要编写一个重载的构造函数,而不需要接口或单独的函数。

可以像这样制作一个重载的构造函数:(你需要根据自己的类调整它):

Public Class Foo
Dim x As Integer
Dim y As Integer
Dim z As Integer

Sub New(a As Integer, b As Integer, c As Integer)
    x = a
    y = b
    z = c
End Sub

Sub New(old As Foo)
    x = old.x
    y = old.y
    z = old.z
End Sub
End Class

答案 1 :(得分:0)

Public Sub New(ByVal p_Name As String, ByVal p_Unit As String, ByVal p_Value As Object, ByVal p_Type As String)
  m_Name = p_Name
  m_Unit = p_Unit
  m_Type = p_Type
  If TypeOf (p_Value) Is Double Then
    m_Value = p_Value
  ElseIf TypeOf (p_Value) Is clsVectorParameter Then
    m_Value = p_Value.Clone()
  End If
End Sub