代码之家  ›  专栏  ›  技术社区  ›  Raj

从基类创建继承类

  •  5
  • Raj  · 技术社区  · 14 年前
    public class Car 
    { 
        private string make;
        private string model;
        public Car(string make, string model)
        {
             this.make = make;
             this.model = model;
        }
        public virtual void Display()
        {
           Console.WriteLine("Make: {0}", make);
           Console.WriteLine("Model: {0}", model);
        }
        public string Make
        {
           get{return make;}
           set{make = value;}
        }
        public string Model
        {
           get{return model;}
           set{model = value;}
        }
    }
    
    public class SuperCar:Car
    {
        private Car car;
        private int horsePower;
        public SuperCar(Car car)
        {
            this.car = car;
        }
        public int HorsePower
        {
           get{return horsePower;}
           set{horsepower = value;}
        }
        public override void Display()
        {
           base.Display();
           Console.WriteLine("I am a super car");
    }
    

    当我做类似的事情

    Car myCar = new Car("Porsche", "911");
    SuperCar mySupcar = new SuperCar(myCar);
    mySupcar.Display();
    

    7 回复  |  直到 14 年前
        1
  •  9
  •   kemiller2002    14 年前

    public class Car
    {
        public Car(string make, string model)
        {
             this.make = make;
             this.model = model;
        }
    
    
        public Car (Car car):this(car.Make, Car.Model){}
    }
    
    public class SuperCar : Car
    {
      SuperCar(Car car): base(car){}
    }
    

    这样,您可以从car继承任何类,并从提供的对象填充car内容。继承的对象不需要知道要设置什么。它们只是将当前的Car对象传递到基类上,它就完成了这项工作。

        2
  •  7
  •   David BouÅ¡ka    6 年前

    我可能会来晚一点,但万一有人发现这个有用:

    public SuperCar(Car car)
    {
        var props = typeof(Car).GetProperties().Where(p => !p.GetIndexParameters().Any());
        foreach (var prop in props)
        {
            if (prop.CanWrite)
                prop.SetValue(this, prop.GetValue(car));
        }
    
        // Set SuperCarcentric properties
        // .
        // .
        // .
    }
    

    我从您的示例中显式地编写了这篇文章,以清楚地说明这个概念,但我认为这最好是一个通用方法,可以在您的解决方案的所有类似实例中使用。

    希望这有帮助。

        3
  •  1
  •   Craig Suchanec    14 年前

    看看你的代码,我不知道它是如何编译的。您的构造函数是错误的,因为基构造函数不知道如何运行接受car类型的构造函数。看起来您正在尝试实现decorator模式,但是没有正确地实现它。你真的应该有一个 ICar 实现和来自的接口 Display() SuperCar 你应该打电话 car.Display() 你还必须在超级汽车上实现Make和Model,并让他们返回汽车。Make&汽车模型来正确地实现装饰模式。

    public interface ICar
    {
        string Make {get; set;}
        string Model {get; set;}
        void Display();
    }
    
    public class Car :ICar
    { 
        private string make;
        private string model;
        public Car(string make, string model)
        {
             this.make = make;
             this.model = model;
        }
        public virtual void Display()
        {
           Console.WriteLine("Make: {0}", make);
           Console.WriteLine("Model: {0}", model);
        }
        public string Make
        {
           get{return make;}
           set{make = value;}
        }
        public string Model
        {
           get{return model;}
           set{model = value;}
        }
    }
    
    public class SuperCar:ICar
    {
        private ICar car;
        private int horsePower;
        public SuperCar(ICar car)
        {
            this.car = car;
        }
    
        public string Make
        {
           get{return car.Make;}
           set{car.Make = value;}
        }
        public string Model
        {
           get{return car.Model;}
           set{car.Model = value;}
        }
        public int HorsePower
        {
           get{return horsePower;}
           set{horsepower = value;}
        }
        public override void Display()
        {
           car.Display();
           Console.WriteLine("I am a super car");
    }
    
        4
  •  0
  •   jpabluz    14 年前

    将private属性更改为protected属性,除了创建它们的类之外,任何人都不能访问private,而继承类可以访问受保护的变量。

        5
  •  0
  •   Somedeveloper    14 年前

    public SuperCar(Car car):base(car.make,car.model)
    {
      this.car = car;
    }
    
        6
  •  0
  •   Daniel    14 年前

    你还没有完全实现 decorator pattern

      public abstract class CarDecorator
      {
        protected Car DecoratedCar { get; private set; }
    
        protected CarDecorator(Car decoratedCar)
        {
          DecoratedCar = decoratedCar;
        }
      }
    
      public class SuperCar : CarDecorator
      {
        public SuperCar(Car car)
          : base(car)
        {
        }
        public int HorsePower
        {
          get { return horsePower; }
          set { horsepower = value; }
        }
        public override void Display()
        {
          DecoratedCar.Display()
          Console.WriteLine("Plus I'm a super car.");
        }
      }
    
        7
  •  0
  •   Fabrice T    7 年前

    它克隆类,所以你可以用它来复制一个基类继承人,你可以调整 参数

    //...
    #if WINDOWS_UWP
    using System.Reflection;
    #endif
    //...
    
    public void CloneIn<T>(T src, T dest)
    {
    #if WINDOWS_UWP
        var props = typeof(T).GetTypeInfo().DeclaredProperties.Where(p => !p.GetIndexParameters().Any());
    #else
        var props = typeof(T).GetProperties().Where(p => !p.GetIndexParameters().Any());
    #endif
        foreach (var prop in props)
        {
            if(prop.SetMethod!=null)
                prop.SetValue(dest, prop.GetValue(src));
        }
    }