【发布时间】:2018-01-31 09:41:44
【问题描述】:
在这里,我创建了两个类 Program,其中 Main() 存在和 Customer Class,其中我有两个 Customer 类构造函数 1. 没有参数 2. 有参数。如何使用 Main 中创建的 Customer 类的单个实例 C1 调用两个构造函数?
using System;
public class Program
{
public static void Main()
{
Customer C1 = new Customer();
C1.PrintFullName();
C1 = new Customer();
C1.PrintFullName();
}
}
class Customer
{
string _firstName;
string _lastName;
public Customer() : this("No firstname","No lastname")
{
}
public Customer(string FirstName, string LastName)
{
this._firstName = FirstName;
this._lastName = LastName;
}
public void PrintFullName()
{
Console.WriteLine("Full Name is {0}", this._firstName+" "+this._lastName);
}
}
【问题讨论】:
-
为什么要使用同一个实例两次调用构造函数?你只是想重置内容吗?
-
无参数构造函数调用
: this("No firstname","No lastname"),因此您已经在执行两个构造函数的代码。你想达到什么目的?
标签: c# class constructor instances