class Program : CarClass1 //子类:父类 冒号表示继承;
{
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();
CarClass1 bujiadiCar = new CarClass1();
bujiadiCar.Name = "布加迪.威航";
bujiadiCar.Brand = "布加迪";
bujiadiCar.Speed = 388;
bujiadiCar.Colour = "Red";
string Str = CarClass1.Insurance;//常量的值是不能改变的
Console.WriteLine(CarClass1.Insurance);//直接打印
Console.WriteLine(Str);
bujiadiCar.Capactiy = 50;//注意类的设置;简写过程 set get 都要写!
bujiadiCar.ShowMsg();
Console.WriteLine(bujiadiCar.Colour);
Console.ReadKey();
AnimalClass duck = new AnimalClass("鸭", 80, 90, "白色");
duck.ShowMsg();
//继承
Program myProgram = new Program();
Console.WriteLine(myProgram.sex1);//获取字段 protected 修饰符 只能通过子类的实例去获取;
FunctionClass fun = new FunctionClass();
Console.WriteLine(fun.ShowTestFun("张三", 100));//调用方法,有返回值;
Console.WriteLine(fun.ShowTestFun("李四", 88));
fun.showTest(8888);//调用方法,没有返回值
fun.ShouTest1(bujiadiCar);
MujiClass muji = new MujiClass("小小");
GongjiClass gongji = new GongjiClass();
gongji.chenJiao(muji);//传参之前必须实例化
Console.ReadKey();
}
}
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;
}
}
public class CarClass1//public 公开的类 不写是只限当前程序集使用 internal
{//类或结构的默认访问类型是internal
//类中所有的成员,默认均为private
/// <summary>
/// 汽车保险年限设置为常量
/// </summary>
public const string Insurance = "3年";//常量默认静态的;必须初始化赋值;常量的值是不能改变的;
//---------------字段---------------
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
{
if (value > 0 && value <= 500)
{
speed = value;
}
else
{
Console.WriteLine("超出范围!");
}
}
get { return speed * 2; }
}
//油箱容量L
public int Capactiy { set; get; }//系统默认创建一个油箱容量字段
public string Colour
{
set
{ colour = value; }
get { return colour; }
}
//男女属性
public string Sex
{
set
{
if (Sex == "男" || Sex == "女")
{
Sex = value;
}
else
{
Console.WriteLine("输入错误!");
}
}
get { return Sex; }
}
//受保护属性
protected string sex1 = "女";
//-------------------方法-----------------
/// <summary>
/// 跑
/// </summary>
private void Run()
{
Console.WriteLine("汽车开动了!");
}
/// <summary>
/// 载人
/// </summary>
public void Manned()//注意private 和 public 的区别和应用范围;私有的,公开的,
{
Console.WriteLine("可以载人!");
}
/// <summary>
/// 屏幕显示信息
/// </summary>
public void ShowMsg()
{
Console.WriteLine("汽车:{0} 品牌:{1} 颜色:{2} 速度:{3}km/h 油箱容量:{4}L", name, brand, colour, speed, Capactiy);
}
}
class FunctionClass
{
public string ShowTestFun(string name, int money)
{//有返回值的方法
string str = "";
str = name + "说今天星期四!";
return str;
}
//没有返回值的方法
public void showTest(int money)
{
//方法体
Console.WriteLine("我身上有" + money + "元!");
}
public void ShouTest1(CarClass1 car)
{
Console.WriteLine("我的汽车是:" + car.Name);
}
}
class GongjiClass
{
private string name = "小明";
public const string tedian = "不能下蛋!";
public string Name
{
set { name = value; }
get { return name; }
}
public void chenJiao(MujiClass muji)
{
Console.WriteLine(name + "公鸡晨叫:太阳晒屁股了!!!起床下蛋了!!!");
muji.xiaDan();
}
private GongjiClass()
{ //私有的函数不能被实例化,只能被当前类使用
}
}
class MujiClass
{
private string name = "小花";
public string Name
{
set { name = value; }
get { return name; }
}
public void xiaDan()
{
Console.WriteLine(name + "母鸡下蛋了!");
}
public MujiClass()
{//优先级最高,首先执行 构造函数;
Console.WriteLine("正在实例化一个母鸡");
}
public MujiClass(string muji)
{ //构造函数可以重名 只有两个构造函数,只有传参不传参
Console.WriteLine("正在实例化一个{0}母鸡", muji);
}
}