using System;
using System.Collections.Generic;
using System.Text;namespace ChooseSubject
{
    public class Car
    {
        private int weight;
        private int speed;
        public Car(int Weight, int Speed)
        {
            weight = Weight;
            speed = Speed;
        }
        public void setweight(int Weight)
        {
            weight = Weight;
        }
        public void setspeed(int Speed)
        {
            speed = Speed;
        }
        public int getspeed()
        {
            return speed;
        }
        public int getweight()
        {
            return weight;
        }
    }
    public class Sportcar : Car
    {
        private string color;
        //public Sportcar(int Weight, int Speed, string Color): base(Weight, Speed)
        //{
        //    setweight(Weight);
        //    setspeed(Speed);
        //    color = Color;
        //}
        public Sportcar(int Weight, int Speed, string Color)
        {
            setweight(Weight);
            setspeed(Speed);
            color = Color;
        }
        public void setcolor(string Color)
        {
            color = Color;
        }
        public string getcolor()
        {
            return color;
        }
        public static void Main()
        {
            Car car = new Car(100, 100);
            Sportcar sportcar = new Sportcar(100, 200, "blcak");
            Console.WriteLine("car's weight is " + car.getweight());
            Console.WriteLine("car's speed is " + car.getspeed());
            Console.WriteLine("sportcar's weight is " + sportcar.getweight());
            Console.WriteLine("sportcar's speed is " + sportcar.getspeed());
            Console.WriteLine("sportcar's speed is " + sportcar.getcolor());
        }
    }
}
提示的错误是:错误 1 “Car”方法没有采用“0”个参数的重载 J:\demo\test1\ChooseSubject\ChooseSubject\Program.cs 44 16 ChooseSubject是这里出的错。“public Sportcar(int Weight, int Speed, string Color)”
一定要这样才行。public Sportcar(int Weight, int Speed, string Color):base(int Weight,int Speed)
为什么要这样?烦请详细解释下,小弟正在自学。

解决方案 »

  1.   

    提示的错误是:错误 1 “Car”方法没有采用“0”个参数的重载 J:\demo\test1\ChooseSubject\ChooseSubject\Program.cs 44 16 ChooseSubject 是这里出的错。“public Sportcar(int Weight, int Speed, string Color)” 
    一定要这样才行。public Sportcar(int Weight, int Speed, string Color):base(int Weight,int Speed) 
    ========================================================================
    这个错误只是说明 Car类 没有 名为Car不带参数的构造方法而已;并不是一定改成你说的这种情况;但你改的也是可以的.
    你的做法是指定创建派生类实例时应调用的基类构造函数;
      

  2.   

    public class Car
    {
    public Car(){}
    }
      

  3.   

    添加一个无参 的构造函数例如:public Car(){}
    这样就不用去调他的父类了
      

  4.   

    的确,只要添加一个Car()这样的构造函数就OK。而且,你不需要写任何内容~
      

  5.   

    添加一个无参 的构造函数例如:public Car(){} 
    这样就不用去调他的父类了
      

  6.   

    添加一个无参 的构造函数例如:public Car(){} 
    也可在下面用 override 重写如 public override  Car(){int Weight, int Speed)}
    再就是base了