Calling this and base constructor?

后端 未结 1 1024
面向向阳花
面向向阳花 2021-01-27 18:44

I have a pretty simple and straightforward question. What is the standardized way, or the right way, of calling another constructor of a class, along with the base constructor o

相关标签:
1条回答
  • 2021-01-27 19:25

    Can't you simplify your approach by using an optional parameter?

        public class Person
        {
            public int Id { get; protected set; }
            public string Name { get; protected set; }
            public Person(string name = "")
            {
                Id = 8;
                Name = name;
            }
        }
    
        public class Engineer : Person
        {
            public int Problems { get; private set; }
            public Engineer(string name = "")
                : base(name)
            {
                Problems = 88;
            }
        }
    
        [TestFixture]
        public class EngineerFixture
        {
            [Test]
            public void Ctor_SetsProperties_AsSpecified()
            {
                var e = new Engineer("bogus");
                Assert.AreEqual("bogus", e.Name);
                Assert.AreEqual(88, e.Problems);
                Assert.AreEqual(8, e.Id);
            }
        }
    
    0 讨论(0)
提交回复
热议问题