一题练习题跪求解!
//定义一个汽车类,该类具有重量和速度属性;再定义一个跑车类,该类继承汽车类的属性并拥有自己的颜色属性;然后声明一个汽车类的对象和一个跑车类的对象,并把它们的属性输出到控制台上. --------------------编程问答-------------------- 这个?
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();
}
--------------------编程问答-------------------- 2楼正解! --------------------编程问答--------------------
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; }
}
}
}
Agree --------------------编程问答--------------------
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) --------------------编程问答-------------------- 我记得这类问题,教科书上都有吧,好像是人一个,水果一个,LZ要多学会灵活运用才行 --------------------编程问答-------------------- 这种基础的东西也帮他做?
楼主作业懒得做吧?
补充:.NET技术 , C#