class Program
{
static void Main(string[] args)
{
CarClass BugattiCar = new CarClass();
BugattiCar.Name = "布加迪.威航";
BugattiCar.Brand = "布加迪";
BugattiCar.Speed = 350;
BugattiCar.Colour = "red";
BugattiCar.ShowMsg();
CarClass ferrariCar = new CarClass();
ferrariCar.Name = "法拉利.欧美亚";
ferrariCar.Brand = "法拉利";
ferrariCar.Speed = 300;
ferrariCar.Colour = "yellow";
ferrariCar.ShowMsg();
Console.ReadKey();
CarClass bujiadiCar = new CarClass();
bujiadiCar.Name = "布加迪.威航";
bujiadiCar.Brand = "布加迪";
bujiadiCar.Speed = 388;
bujiadiCar.Colour = "Red";
bujiadiCar.ShowMsg();
Console.WriteLine(bujiadiCar.Colour);
Console.ReadKey();
AnimalClass duck = new AnimalClass("鸭", 80, 90, "白色");
duck.ShowMsg();
Console.ReadKey();
}
}
/// <summary>
/// 汽车类
/// </summary>
class CarClass
{
//---------------字段---------------
private string name;//名字
private string brand;//品牌
private string colour;//颜色
private int speed;//速度
//-----------构造函数----------------通过构造函数初始化相关值
//public CarClass(string _name, string _brand, int _speed) {
// this.name = _name;
// this.brand = _brand;
// this.speed = _speed;
//}
//--------------属性-------------------
//name属性
public string Name
{
set { name = value; }
get { return name; }
}
//品牌属性
public string Brand
{
set { brand = value; }
get { return brand; }
}
//速度属性
public int Speed
{
set { speed = value; }
get { return speed; }
}
public string Colour
{
set { colour = value; }
get { return colour; }
}
//-------------------方法-----------------
/// <summary>
/// 跑
/// </summary>
private void Run()
{
Console.WriteLine("汽车开动了!");
}
/// <summary>
/// 载人
/// </summary>
public void Manned()
{
Console.WriteLine("可以载人!");
}
/// <summary>
/// 屏幕显示信息
/// </summary>
public void ShowMsg()
{
Console.WriteLine("汽车:{0} 品牌:{1} 颜色:{2} 速度:{3}km/h", name, brand, colour, speed);
}
}
/// <summary>
/// 动物类
/// </summary>
class AnimalClass
{
//--------字段--------
private string Animalname;
private int Animalweingth;
private int Animalheigth;
private string Aniamalcolour;
//-----------属性---------------
public string Name
{
set { Animalname = value; }//set 设置值 Animalname=“鸭子”否则报错
get { return Animalname; }//get 获取值 Animalname.Name 否则报错;
}
public int Weigth
{
set { Animalweingth = value; }
get { return Animalweingth; }
}
public int Heigth
{
set { Animalheigth = value; }
get { return Animalheigth; }
}
public string Colour
{
set { Aniamalcolour = value; }
get { return Aniamalcolour; }
}
//---------------------方法----------------
public void Eatting()
{
Console.WriteLine("吃!");
}
public void Running()
{
Console.WriteLine("跑!");
}
public void Latting()
{
Console.WriteLine("拉翔!");
}
/// <summary>
/// 打印到控制台
/// </summary>
public void ShowMsg()
{
Console.WriteLine("动物名称:{0} 重量:{1}g 高度:{2}cm 颜色:{3}", Animalname, Animalheigth, Animalweingth, Aniamalcolour);
Console.WriteLine("具有以下能力:");
Eatting();
Running();
Latting();
}
//------------------------构造函数-------------
public AnimalClass(string _name, int _weigth, int _heigth, string _colour)
{
Animalname = _name;
Animalheigth = _heigth;
Animalweingth = _weigth;
Aniamalcolour = _colour;
}
}