using System;
namespace C_Project
{
public abstract class Vehicle //抽象父类
{
protected int wheels;
protected float weight;
protected readonly static int staticWheels = 1;
protected const float staticWeight = 2;
public Vehicle() { }//父类不带参数的构造函数
public Vehicle(int w, float g)//父类带参数的构造函数
{
wheels =w;
weight =g;
}
public abstract void AbstractFunction();//抽象方法
public static void StaticFunction()//静态方法
{
Console.WriteLine("父类静态方法:" + staticWheels * staticWeight);
}
public virtual void Speak()//非抽象静态方法
{
Console.WriteLine("父类非抽象静态方法" + wheels * weight);
}
}
class Car : Vehicle //子类
{
int passger;
public Car(int w, float g, int p)
: base(w, g)//调用父类构造函数
{
wheels = w;
weight = g;
passger = p;
}
public override void AbstractFunction()//实现父类抽象方法
{
Console.WriteLine("子类实现父类抽象方法:" + wheels + passger);
}
public new static void StaticFunction()//重写父类静态方法,并且隐藏父类方法
{
Console.WriteLine("子类重写父类静态方法:" + staticWheels + staticWeight);
}
public new void Speak()//重写父类非抽象静态方法,并且隐藏父类方法
{
Console.WriteLine("子类重写父类非抽象静态方法,并且隐藏父类方法:" + wheels +
weight);
}
public void DSpeak()//子类非静态抽象方法
{
base.Speak();
}
public static void Main(string[] args)
{
Car cc = new Car(2,3,4);
cc.Speak();//调用子类方法
cc.DSpeak();//调用父类方法
cc.AbstractFunction();//调用子类实现的父类抽象方法
Vehicle.StaticFunction();//调用父类静态方法
Car.StaticFunction();//调用子类静态方法
}
}
}