一题练习题跪求解!

uhgujhin123 2012-05-21 09:27:35
//定义一个汽车类,该类具有重量和速度属性;再定义一个跑车类,该类继承汽车类的属性
并拥有自己的颜色属性;然后声明一个汽车类的对象和一个跑车类的对象,并把它们的属性输出到控制台上.
...全文
290 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
山之魂2 2012-05-21
  • 打赏
  • 举报
回复
这种基础的东西也帮他做?
楼主作业懒得做吧?
叫我三三 2012-05-21
  • 打赏
  • 举报
回复
我记得这类问题,教科书上都有吧,好像是人一个,水果一个,LZ要多学会灵活运用才行
IT-Style 2012-05-21
  • 打赏
  • 举报
回复

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)
Ny-6000 2012-05-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
2楼正解!
[/Quote]

Agree
lzxgy8008 2012-05-21
  • 打赏
  • 举报
回复
2楼正解!
wangsong145 2012-05-21
  • 打赏
  • 举报
回复

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

bdmh 2012-05-21
  • 打赏
  • 举报
回复

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();
}
tygh2001 2012-05-21
  • 打赏
  • 举报
回复
这个?
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();
}

大体这样,只是自己这样写的,没测试过。

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧