当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET Object.MemberwiseClone方法代码示例

本文整理汇总了VB.NET中System.Object.MemberwiseClone方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Object.MemberwiseClone方法的具体用法?VB.NET Object.MemberwiseClone怎么用?VB.NET Object.MemberwiseClone使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。


在下文中一共展示了Object.MemberwiseClone方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: IdInfo

Public Class IdInfo
    Public IdNumber As Integer
    
    Public Sub New(IdNumber As Integer)
        Me.IdNumber = IdNumber
    End Sub
End Class

Public Class Person 
    Public Age As Integer
    Public Name As String
    Public IdInfo As IdInfo

    Public Function ShallowCopy() As Person
       Return DirectCast(Me.MemberwiseClone(), Person)
    End Function

    Public Function DeepCopy() As Person
       Dim other As Person = DirectCast(Me.MemberwiseClone(), Person) 
       other.IdInfo = New IdInfo(IdInfo.IdNumber)
       other.Name = String.Copy(Name)
       Return other
    End Function
End Class

Module Example
   Public Sub Main()
        ' Create an instance of Person and assign values to its fields.
        Dim p1 As New Person()
        p1.Age = 42
        p1.Name = "Sam"
        p1.IdInfo = New IdInfo(6565)

        ' Perform a shallow copy of p1 and assign it to p2.
        Dim p2 As Person = p1.ShallowCopy()

        ' Display values of p1, p2
        Console.WriteLine("Original values of p1 and p2:")
        Console.WriteLine("   p1 instance values: ")
        DisplayValues(p1)
        Console.WriteLine("   p2 instance values:")
        DisplayValues(p2)
        Console.WriteLine()
                
        ' Change the value of p1 properties and display the values of p1 and p2.
        p1.Age = 32
        p1.Name = "Frank"
        p1.IdInfo.IdNumber = 7878
        Console.WriteLine("Values of p1 and p2 after changes to p1:")
        Console.WriteLine("   p1 instance values: ")
        DisplayValues(p1)
        Console.WriteLine("   p2 instance values:")
        DisplayValues(p2)
        Console.WriteLine()
        
        ' Make a deep copy of p1 and assign it to p3.
        Dim p3 As Person = p1.DeepCopy()
        ' Change the members of the p1 class to new values to show the deep copy.
        p1.Name = "George"
        p1.Age = 39
        p1.IdInfo.IdNumber = 8641
        Console.WriteLine("Values of p1 and p3 after changes to p1:")
        Console.WriteLine("   p1 instance values: ")
        DisplayValues(p1)
        Console.WriteLine("   p3 instance values:")
        DisplayValues(p3)
   End Sub
   
    Public Sub DisplayValues(p As Person)
        Console.WriteLine("      Name: {0:s}, Age: {1:d}", p.Name, p.Age)
        Console.WriteLine("      Value: {0:d}", p.IdInfo.IdNumber)
    End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:73,代码来源:Object.MemberwiseClone

输出:

Original values of m1 and m2:
m1 instance values:
Name: Sam, Age: 42
Value: 6565
m2 instance values:
Name: Sam, Age: 42
Value: 6565

Values of m1 and m2 after changes to m1:
m1 instance values:
Name: Frank, Age: 32
Value: 7878
m2 instance values:
Name: Sam, Age: 42
Value: 7878

Values of m1 and m3 after changes to m1:
m1 instance values:
Name: George, Age: 39
Value: 8641
m3 instance values:
Name: Frank, Age: 32
Value: 7878


注:本文中的System.Object.MemberwiseClone方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。