using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ObjTest
{
class Program
{
static void Main(string[] args)
{
//car a = new car();
//a.name = "bmw";
//a.run();
//car b = new car();
//b.name = "lbjn";
//b.run();
// 子类创建对象并初始化
Bmw b = new Bmw();
b.name = "bmw";
b.you = 100;
b.lun = 4;
b.run();
// 子类创建对象并初始化
Dcar d = new Dcar();
d.name = "电车";
d.dian = 50;
d.lun = 6;
d.run();
//buyCar(b);
//buyCar(d);
Car c = new Car();
c.name = "本田";
buyCar(c);
Console.ReadKey();
}
public static void buyCar(Car a) //写一个静态父类方法(由类调用),父类的指向,指向子类的对象;(多态的体现),参数可以是基类Car的不同对象;
{
Console.Write("我要买" + a .name+ "\n");
}
//定义一个父类(基类)Car;
public class Car {
//定义父类的name,lun属性;
public string name;
public int lun;
//定义父类的方法;
public void run (){
Console.Write(name+" run"+"\n");
}
}
//子类继承父类,并子类拥有自己的属性和方法
public class Bmw :Car{
//子类的属性
public int you;
//子类的拥有父类方法同名方法(重写父类的方法)
public void run()
{
//父类调用子类的方法用base
base.run();
Console.Write(lun+"个轮"+you + "升油" + name + " run" + "\n");
}
}
//定义另一个子类电车
public class Dcar : Car
{
public int dian;
public void run()
{
//base.run();
Console.Write(lun + "个轮" + dian + "度电" + name + " run" + "\n");
}
}
}
}