在类初始化

时间:2017-06-15 12:58:57

标签: c#

假设我们有一些课程CarsBase

public class CarsBase
{
    public string DisplayName { get; set; }
}

然后我们有一些其他课程Toyota

public class Toyota : CarsBase
{
    public EngineType EngineType { get; set; }
}

然后我们通过使用对象初始化器来初始化我们的类实例:

var myVar = new Toyota()
{
    // DisplayName = "", ← We could do this by our hands, but can it be done automatically?
    EngineType = EngineType.UZ
}

问题:有没有办法在对象初始化时自动填充CarsBase的{​​{1}}属性?

就像,如果我有更多的汽车类(DisplayNameBMW等),每个都在扩展Suzuki,因此每个都有CarsBase个属性类。

4 个答案:

答案 0 :(得分:1)

这听起来应该在构造函数中完成。

public class Toyota : CarsBase
{
    public Toyota() : base()
    {
        base.DisplayName = "Toyota";
    }
    public EngineType EngineType { get; set; }
}

另一个选项,不管多少推荐,而不是以相同的意义获取/设置DisplayName,可以将基类更改为使用反射检索类名并将其用作显示名称:

public class CarsBase
{
    public string DisplayName 
    { 
        get 
        { 
            return this.GetType().Name; 
        }
    }
}

这种方法应该归还"丰田"但是,从类名中可以防止使用空格或其他特殊字符。像这样的反射代码也有变慢的倾向。

答案 1 :(得分:0)

创建构造函数以传递dispay名称(或其他所需参数) -

Toyota(string displayName)
{
    DisplayName = displayName;
    EngineType = EngineType.UZ;
}

然后你可以这样打电话 -

new Toyota("some display name");

答案 2 :(得分:0)

只需在构造函数中设置属性值即可。像这样:

    internal class Program
{
    private static void Main(string[] args)
    {
        Toyota t = new Toyota() { EngineType = new EngineType() };
        Console.WriteLine(t.DisplayName);
        Console.ReadLine();
    }
}

public class CarsBase
{
    public string DisplayName { get; set; }
}
public class Toyota : CarsBase
{
    public EngineType EngineType { get; set; }

    public Toyota()
    {
        // set the default Display Name
        // that way you don't have to set it everytime
        this.DisplayName = "Oh what a feeling!";
    }
}
public class EngineType { }

答案 3 :(得分:0)

是的,可以在触发构造函数的对象的初始化阶段完成。我创建了两个类 *一个用于保持engine_Types的枚举常量值 - > EngineType

  • 一个用于解释继承,构造函数链接,创建一个对象类的实例----> CarsBase

[预]

    namespace stacketst
     {

    public class CarsBase
     {
    public string DisplayName { get; set; }
          public CarsBase()
          {
    //called when CarBase object is initialized
     DisplayName = "Base Car";

     }
    }

     public class Toyota : CarsBase
     {
    //getters , setters called as properties in C#         
     public int number_of_wheels { get; set; }
    public double fuel_capacity { get; set; }
    public string engine_type { get; set; }

     public Toyota() //called when an instance of Toyota is created
     {
    //assinging value to this property calls set 
    fuel_capacity = 4.2; 
    number_of_wheels = 4; 
    engine_type = EngineType.name_engines.UZ.ToString(); 
   }     
   }

     public class TestClass
    {
    static void Main()
    {
        //when below line is executed,constructor is fired & the initialization of variable inside constructor takes place    

    var myVar = new Toyota();
  Console.WriteLine(myVar.DisplayName);
     }
    }
    }

    namespace stacketst
        {
     public class EngineType
    {
     //enums to hold constants, common for any Car Class
         public enum name_engines 
    { 

    V12, V10, V8, V6, UZ        
     };
     }
    }

[/预]