求解 vb.net 如何用一个实例来实例化自己

超级格古达 2015-07-19 10:59:07
Sub New(ByVal car As CarInfo)
Me = car
End Sub
这样写不对。请问怎么写
...全文
227 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mystery1000 2015-07-24
  • 打赏
  • 举报
回复
实例化自己不就是new一个自己的类名就可以了 ?
宝_爸 2015-07-20
  • 打赏
  • 举报
回复
this关键字是只读的C#里也不能。你这个构造函数叫拷贝构造函数(Copy Constructor). 只能一个field,一个field赋值。

class Person
{
    // Copy constructor. 
    public Person(Person previousPerson)
    {
        Name = previousPerson.Name;
        Age = previousPerson.Age;
    }

    //// Alternate copy constructor calls the instance constructor. 
    //public Person(Person previousPerson) 
    //    : this(previousPerson.Name, previousPerson.Age) 
    //{ 
    //} 

    // Instance constructor. 
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    public int Age { get; set; }

    public string Name { get; set; }

    public string Details()
    {
        return Name + " is " + Age.ToString();
    }
}

class TestPerson
{
    static void Main()
    {
        // Create a Person object by using the instance constructor.
        Person person1 = new Person("George", 40);

        // Create another Person object, copying person1.
        Person person2 = new Person(person1);

        // Change each person's age. 
        person1.Age = 39;
        person2.Age = 41;

        // Change person2's name.
        person2.Name = "Charles";

        // Show details to verify that the name and age fields are distinct.
        Console.WriteLine(person1.Details());
        Console.WriteLine(person2.Details());

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
// Output:  
// George is 39 
// Charles is 41
以上代码来自msdn How to: Write a Copy Constructor (C# Programming Guide) https://msdn.microsoft.com/en-us/library/ms173116.aspx
Tiger_Zhao 2015-07-20
  • 打赏
  • 举报
回复
Sub New(ByVal p1 As ..., ByVal p2 As ...)
'一般的构造函数'
End Sub

Sub New(ByVal car As CarInfo)
Me.New(car.p1, car.p2) '用car的属性来构造实例啊'
End Sub
超级格古达 2015-07-20
  • 打赏
  • 举报
回复
引用 1 楼 hdt 的回复:
实现Icloneable 接口
哦。。。。。。C# 可以 vb。net不能 只读。。。。
真相重于对错 2015-07-19
  • 打赏
  • 举报
回复
实现Icloneable 接口

16,556

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧