【问题标题】:How to call different constructors in class using single instance of that class in C#?如何在 C# 中使用该类的单个实例在类中调用不同的构造函数?
【发布时间】:2018-01-31 09:41:44
【问题描述】:

Code description

在这里,我创建了两个类 Program,其中 Main() 存在和 Customer Class,其中我有两个 Customer 类构造函数 1. 没有参数 2. 有参数。如何使用 Main 中创建的 Customer 类的单个实例 C1 调用两个构造函数?

Code

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


【解决方案1】:

创建一个新对象中固有地涉及一个类构造函数 - 所以没有针对现有实例运行构造函数的内置概念,除非你做了一些 非常 讨厌的事情你不应该做的事情。

如果您希望能够使用构造函数那样设置所有属性,那么也许可以添加一个方法:

var obj = new SomeType();
obj.Init();
// ...
obj.Init("foo", "bar", 123);
// ...

或者:只需使用现有的构造函数并接受它将是具有不同引用的不同实例:

var obj = new SomeType();
// ...
obj = new SomeType("foo", "bar", 123);
// ...

【讨论】:

  • 好的。因此,要在类中调用不同的构造函数,我们只能选择创建不同的实例。我们可以为此使用单例模式吗?
  • @UV009 单例Customer 听起来是一个可怕 的想法,除非你正在编写一个只在单个Customer 上下文中运行的客户端系统
  • @UV009 我认为这是一个 x/y 问题;你问的是具体的东西,比如构造函数和单例,但你还没有告诉我们你实际上想要实现什么;所以...很难评论
  • 是的。我现在正在学习这些概念。感谢您的帮助。@Marc Gravell
【解决方案2】:

您可以找到更多信息here

using System;

public class Program
{
    public static void Main()
    {
        Customer C1 = new Customer("Bob", "Robert", "BobyBob 23, 41423");
        C1.PrintFullName();
        C1 = new Customer("Bob", "Robert");
        C1.PrintFullName();
    }
}

class Customer
{

    string _firstName;
    string _lastName;
    string _address;


    public Customer()
    {
        Console.WriteLine("I'm the last one :)");
        Console.WriteLine("Full Name is {0}", this._firstName+" "+this._lastName);
    }

    public Customer(string FirstName, string LastName) : this()
    {
        Console.WriteLine("I'm gonna call the constructor within the same class that has no parameters.");      
        _firstName = FirstName;
        _lastName = LastName;
    }

    public Customer(string FirstName, string LastName, string address) : this(FirstName, LastName)
    {
        Console.WriteLine("I'm gonna call the constructor within the same class that has 2 parameters that are of type string.");
        _address = _address;
    }

    public void PrintFullName()
    {
        Console.WriteLine("Full Name is {0}", this._firstName+" "+this._lastName);
    }
}

【讨论】:

  • 我完全不清楚如何 - 如果有的话(我怀疑它没有)这回答了顶部的问题:“我如何使用单个实例调用两个构造函数在 Main 中创建的 Customer 类的 C1?”
  • 我认为他不确定如何提出这个问题,因为他可能不熟悉这个主题,所以我认为他指的是那个。
  • 这有助于我理解这个概念。谢谢@Gilad Reich
  • @UV009 很高兴它有帮助:)
  • 看了上面的代码,我什至明白了这个关键字是如何正确工作的。 @GiladReich
猜你喜欢
  • 2014-08-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
  • 2011-02-15
  • 1970-01-01
  • 2013-06-14
相关资源
最近更新 更多