1

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

    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”的扩展要求

非常感谢您的时间和建议。

提前致谢,

4

2 回答 2

2

问题在这里:

  private void GetDetails(ShowRoomA objShowRoom)
  {
     objShowRoom = new 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";

你应该没事。

于 2012-12-08T17:05:44.013 回答
2

我会为初学者创建一个 Car 类:

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
{
}

还有一个 ShowRoom 类,其中包含 Car 列表:

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

    public virtual void GetDetails(ShowRoomA showRoom)
    {
        // Do your stuff
    }
} 

您也可以扩展它:

public class ShowRoomB : ShowRoomA
{
    public override void GetDetails(ShowRoomA showRoom)
    {
        // Do some other 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);
    } 
}
于 2012-12-08T17:11:27.310 回答