这个? public class clsCar { private double _heavy = 0; public double Heavy { get {return _heavy;} set {_heavy = value;} } private double _speed = 0; public double Speed { get {return _speed ;} set {_speed = value;} } }public class clsSCar:clsCar { private Color _color = new Color(255,255,255,255); public Color Color { get {return _color;} set {_color = value;} } }public void main() { clsCar car = new clsCar(); clsSCar scar = new clsSCar(); console.write(); }大体这样,只是自己这样写的,没测试过。
public class Car { public Car() { } public Car(float weight,float speed) { this._weight = weight; this._speed = speed; } private float _weight; public float Weight { get; set; } private float _speed; public float Speed { get; set; } public virtual void WriteInfo() { Console.WriteLine("重量:" + this._weight.ToString() + ",速度:" + this._speed.ToString()); } } public class RunCar : Car { public RunCar(float weight, float speed, ConsoleColor color):base(weight, speed) { this._color = color; } private ConsoleColor _color; public ConsoleColor Color { get; set; } public override void WriteInfo() { base.WriteInfo(); Console.WriteLine("颜色:" + this._color.ToString()); } } static void Main(string[] args) { Car car = new Car(12f, 34f); Car runcar = new RunCar(23f, 55f, ConsoleColor.Red); car.WriteInfo(); runcar.WriteInfo(); }
using System; using System.Collections.Generic; using System.Text; using System.Drawing;namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Car car = new Car(1000, 80); Console.WriteLine("汽车的重量为:{0},速度为:{1}", car.Weight, car.Speed); SportCar sportCar = new SportCar(2000, 120, Color.Red); Console.WriteLine("跑车的重量为:{0},速度为:{1},颜色为:{2}", sportCar.Weight, sportCar.Speed, sportCar.Color.ToString()); Console.Read(); } } public class Car { public Car(double weight, double speed) { this.m_Weight = weight; this.m_Speed = speed; } private double m_Weight; /// <summary> /// 重量 /// </summary> public double Weight { get { return this.m_Weight; } set { this.m_Weight = value; } } private double m_Speed; /// <summary> /// 速度 /// </summary> public double Speed { get { return this.m_Speed; } set { this.m_Speed = value; } } } public class SportCar : Car { public SportCar(double weight, double speed, Color color) : base(weight, speed) { this.m_Color = color; } private Color m_Color; /// <summary> /// 颜色 /// </summary> public Color Color { get { return this.m_Color; } set { this.m_Color = value; } } } }
public class Car { public int Weight { get; set; } public int Speed { get; set; } public Car() { } public Car(int weight,int speed) { this.Weight = weight; this.Speed = speed; } public virtual void GetCarInfo() { Console.WriteLine("Car Weight:" + Weight.ToString() + ",Car Speed:" + Speed.ToString()); } } public class SportsCar:Car { public System.Drawing.Color CarColor { get; set; } public SportsCar() { } public SportsCar(int weight,int speed,System.Drawing.Color color):base(weight,speed) { this.CarColor = color; } public override void GetCarInfo() { base.GetCarInfo(); Console.WriteLine(this.CarColor.ToString()); } }static void Main(string[] args) { Car car = new Car { Weight = 100, Speed = 200 }; SportsCar sportCar = new SportsCar { Weight = 100, Speed = 200,CarColor=System.Drawing.Color.Red }; car.GetCarInfo(); sportCar.GetCarInfo(); Console.ReadLine(); } 这其中用到的知识点: 1.自动属性 2.对象初始化器 3.继承与多态(virtual--override)
public class clsCar
{
private double _heavy = 0;
public double Heavy
{
get {return _heavy;}
set {_heavy = value;}
}
private double _speed = 0;
public double Speed
{
get {return _speed ;}
set {_speed = value;}
}
}public class clsSCar:clsCar
{
private Color _color = new Color(255,255,255,255);
public Color Color
{
get {return _color;}
set {_color = value;}
}
}public void main()
{
clsCar car = new clsCar();
clsSCar scar = new clsSCar();
console.write();
}大体这样,只是自己这样写的,没测试过。
public class Car
{
public Car()
{
}
public Car(float weight,float speed)
{
this._weight = weight;
this._speed = speed;
}
private float _weight;
public float Weight
{
get;
set;
}
private float _speed;
public float Speed
{
get;
set;
}
public virtual void WriteInfo()
{
Console.WriteLine("重量:" + this._weight.ToString() + ",速度:" + this._speed.ToString());
}
}
public class RunCar : Car
{
public RunCar(float weight, float speed, ConsoleColor color):base(weight, speed)
{
this._color = color;
}
private ConsoleColor _color;
public ConsoleColor Color
{
get;
set;
}
public override void WriteInfo()
{
base.WriteInfo();
Console.WriteLine("颜色:" + this._color.ToString());
}
} static void Main(string[] args)
{
Car car = new Car(12f, 34f);
Car runcar = new RunCar(23f, 55f, ConsoleColor.Red);
car.WriteInfo();
runcar.WriteInfo();
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Car car = new Car(1000, 80);
Console.WriteLine("汽车的重量为:{0},速度为:{1}", car.Weight, car.Speed); SportCar sportCar = new SportCar(2000, 120, Color.Red);
Console.WriteLine("跑车的重量为:{0},速度为:{1},颜色为:{2}", sportCar.Weight, sportCar.Speed, sportCar.Color.ToString()); Console.Read();
}
} public class Car
{
public Car(double weight, double speed)
{
this.m_Weight = weight;
this.m_Speed = speed;
} private double m_Weight; /// <summary>
/// 重量
/// </summary>
public double Weight
{
get { return this.m_Weight; }
set { this.m_Weight = value; }
} private double m_Speed; /// <summary>
/// 速度
/// </summary>
public double Speed
{
get { return this.m_Speed; }
set { this.m_Speed = value; }
}
} public class SportCar : Car
{
public SportCar(double weight, double speed, Color color)
: base(weight, speed)
{
this.m_Color = color;
} private Color m_Color; /// <summary>
/// 颜色
/// </summary>
public Color Color
{
get { return this.m_Color; }
set { this.m_Color = value; }
}
}
}
public class Car
{
public int Weight { get; set; }
public int Speed { get; set; }
public Car()
{ }
public Car(int weight,int speed)
{
this.Weight = weight;
this.Speed = speed;
}
public virtual void GetCarInfo()
{
Console.WriteLine("Car Weight:" + Weight.ToString() + ",Car Speed:" + Speed.ToString());
}
}
public class SportsCar:Car
{
public System.Drawing.Color CarColor
{
get;
set;
}
public SportsCar()
{ }
public SportsCar(int weight,int speed,System.Drawing.Color color):base(weight,speed)
{
this.CarColor = color;
}
public override void GetCarInfo()
{
base.GetCarInfo();
Console.WriteLine(this.CarColor.ToString());
}
}static void Main(string[] args)
{ Car car = new Car { Weight = 100, Speed = 200 };
SportsCar sportCar = new SportsCar { Weight = 100, Speed = 200,CarColor=System.Drawing.Color.Red };
car.GetCarInfo();
sportCar.GetCarInfo();
Console.ReadLine();
}
这其中用到的知识点:
1.自动属性
2.对象初始化器
3.继承与多态(virtual--override)