Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 扩展基类属性_C#_Oop - Fatal编程技术网

C# 扩展基类属性

C# 扩展基类属性,c#,oop,C#,Oop,我需要你的帮助来扩展我的基类,这是我拥有的类似结构 public class ShowRoomA { public audi AudiModelA { get; set; } public benz benzModelA { get; set; } } public class audi { public string Name { get; set; }

我需要你的帮助来扩展我的基类,这是我拥有的类似结构

    public class ShowRoomA
    {
            public audi AudiModelA { get; set; }
            public benz benzModelA { get; set; }
    }   
    public class audi
    {
            public string Name { get; set; }
            public string AC { get; set; }
            public string PowerStearing { get; set; }
    }
    public class benz
    {
            public string Name { get; set; }
            public string AC { get; set; }
            public string AirBag { get; set; }
            public string MusicSystem { get; set; }
    }
//My Implementation class like this
    class Main()
    {
      private void UpdateDetails()
      {
        ShowRoomA ojbMahi = new ShowRoomA();
        GetDetails( ojbMahi ); // this works fine
      }
      private void GetDetails(ShowRoomA objShowRoom)
      {
         objShowRoom;
         objShowRoom.audi = new audi();
         objShowRoom.audi.Name = "AUDIMODEL94CD698";
         objShowRoom.audi.AC = "6 TON";
         objShowRoom.audi.PowerStearing = "Electric";

         objShowRoom.benz= new benz();
         objShowRoom.benz.Name = "BENZMODEL34LCX";
         objShowRoom.benz.AC = "8 TON";
         objShowRoom.benz.AirBag = "Two (1+1)";
         objShowRoom.benz.MusicSystem = "Poineer 3500W";
      } 
    }
// Till this cool.
// Now I got requirement for ShowRoomB with replacement of old audi and benz with new models and new other brand cars also added.
// I don't want to modify GetDetails() method. by reusing this method additional logic i want to apply to my new extended model.
// Here I struck in designing my new model of ShowRoomB (base of ShowRoomA) ... I have tried some thing like... but not sure.

    public class audiModelB:audi
    {
            public string JetEngine { get; set; }
    }
    public class benzModelB:benz
    {
            public string JetEngine { get; set; }
    }
    public class ShowRoomB
    {
            public audiModelB AudiModelB { get; set; }
            public benzModelB benzModelB { get; set; }
    } 

// My new code to Implementation class like this
    class Main()
    {
      private void UpdateDetails()
      {
        ShowRoomB ojbNahi = new ShowRoomB();
        GetDetails( ojbNahi ); // this is NOT working! I know this object does not contain base class directly, still some what i want fill my new model with old properties. Kindly suggest here 
      }
    }
有谁能给我一些解决方案来满足我对基本类“ShowroomA”的扩展需求吗

非常感谢您的时间和建议

提前感谢,

问题在于:

  private void GetDetails(ShowRoomA objShowRoom)
  {
     objShowRoom = new objShowRoom();  //<-----
并修复此块:

     objShowRoom.benz= new benz();
     objShowRoom.audi.Name = "BENZMODEL34LCX";   //<--- should be objShowRoom.benz?
     objShowRoom.audi.AC = "8 TON";
     objShowRoom.audi.AirBag = "Two (1+1)";
     objShowRoom.audi.MusicSystem = "Poineer 3500W";
objShowRoom.benz=新奔驰();
objShowRoom.audi.Name=“BENZMODEL34LCX”// 问题在于:

  private void GetDetails(ShowRoomA objShowRoom)
  {
     objShowRoom = new objShowRoom();  //<-----
并修复此块:

     objShowRoom.benz= new benz();
     objShowRoom.audi.Name = "BENZMODEL34LCX";   //<--- should be objShowRoom.benz?
     objShowRoom.audi.AC = "8 TON";
     objShowRoom.audi.AirBag = "Two (1+1)";
     objShowRoom.audi.MusicSystem = "Poineer 3500W";
objShowRoom.benz=新奔驰();

objShowRoom.audi.Name=“BENZMODEL34LCX”// 我将为初学者创建一个汽车类:

public class Car
{
    public string Name { get; set; }
    public string AC { get; set; }
    public string PowerStearing { get; set; }
    public string MusicSystem { get; set; }
}
public class Audi : Car
{
}
public class Benz : Car
{
}
和一个展厅类,以及一系列汽车:

public class ShowRoomA
{
    public List<Car> Cars { get; set; }

    public virtual void GetDetails(ShowRoomA showRoom)
    {
        // Do your stuff
    }
} 
并以此方式使用:

class Main()
{
    private void UpdateDetails()
    {
        ShowRoom ojbMahi = new ShowRoom();

        Audi audi = new Audi() { Name = "AUDIMODEL94CD698", AC = "6 TON" };
        objMahi.Cars.Add(audi);

        Benz benz = new Benz() { Name = "BENZMODEL34LCX", AC = "8 TON" };
        objMahi.Cars.Add(benz);
    } 
}

我将为初学者创建一个汽车类:

public class Car
{
    public string Name { get; set; }
    public string AC { get; set; }
    public string PowerStearing { get; set; }
    public string MusicSystem { get; set; }
}
public class Audi : Car
{
}
public class Benz : Car
{
}
和一个展厅类,以及一系列汽车:

public class ShowRoomA
{
    public List<Car> Cars { get; set; }

    public virtual void GetDetails(ShowRoomA showRoom)
    {
        // Do your stuff
    }
} 
并以此方式使用:

class Main()
{
    private void UpdateDetails()
    {
        ShowRoom ojbMahi = new ShowRoom();

        Audi audi = new Audi() { Name = "AUDIMODEL94CD698", AC = "6 TON" };
        objMahi.Cars.Add(audi);

        Benz benz = new Benz() { Name = "BENZMODEL34LCX", AC = "8 TON" };
        objMahi.Cars.Add(benz);
    } 
}

曾经给出的例子是模拟我原来的类,它更复杂。我无法更改我的类结构,因为我的ShowroomA模型已投入生产。@user1888033我将根据您目前的结构和类重新考虑您的实现,因为它们将在将来成为一个问题。但是,您可以使用ShowRoomB扩展ShowRoomA并覆盖ShowRoomB中的GetDetails方法。@user1888033更新了答案以包含新的更改。@user1888033更新了答案以包含示例。你需要更具体地说明你想做什么,但目前还不能。曾经给出的例子是模拟我原来的类,它更复杂。我无法更改我的类结构,因为我的ShowroomA模型已投入生产。@user1888033我将根据您目前的结构和类重新考虑您的实现,因为它们将在将来成为一个问题。但是,您可以使用ShowRoomB扩展ShowRoomA并覆盖ShowRoomB中的GetDetails方法。@user1888033更新了答案以包含新的更改。@user1888033更新了答案以包含示例。你需要更具体地说明你想做什么,但现在不行。谢谢,我刚刚纠正了。它的类型错误。给出的示例是模拟我的原始示例的示例。为了弄清楚,我以汽车为例。谢谢你,谢谢我刚刚改正了。它的类型错误。给出的示例是模拟我的原始示例的示例。为了弄清楚,我以汽车为例。谢谢,如果有帮助,请选择答案。如果有帮助,请选择答案?