简单的继承
定义一个车辆(Vehicle)基类,具有Run、Stop等方法,具有Speed(速度)、MaxSpeed(最大速度)、Weight(重量)等域。然后以该类为基类,派生出bicycle、car等类。并编程对该派生类的功能进行验证。
父类
using UnityEngine;
using System.Collections;public class Vehicle {
public float Speed;
public float MaxSpeed;
public float Weight;
public void Run()
{
Debug.Log("Vehicle-Run");
}
public void Stop()
{
Debug.Log("Vehicle-Stop");
}
}
--------------------------
子类
using UnityEngine;
using System.Collections;
public class car :Vehicle {
public void Run()
{
Debug.Log("car-Run"+"speed->"+Speed+"\t"+"Weight->"+Weight+"\t"+"MaxSpeed"+MaxSpeed);
}
public void Stop()
{
Debug.Log("car-Stop"+"speed->"+Speed+"\t"+"Weight->"+Weight+"\t"+"MaxSpeed"+MaxSpeed);
}
}
using UnityEngine;
using System.Collections;
public class bicycle : Vehicle {
public void Run()
{
Debug.Log("bicycle-Run"+"speed->"+Speed+"\t"+"Weight->"+Weight+"\t"+"MaxSpeed"+MaxSpeed);
}
public void Stop()
{
Debug.Log("bicycle-Stop"+"speed->"+Speed+"\t"+"Weight->"+Weight+"\t"+"MaxSpeed"+MaxSpeed);
}
}
-----------------------
引用
using UnityEngine;
using System.Collections;
public class chen : MonoBehaviour {
public bicycle s1=new bicycle();
public car s2=new car();
void Start () {
s1.Speed = 30;
s1.MaxSpeed = 90;
s1.Weight = 1000;
s2.Speed = 50;
s2.MaxSpeed = 120;
s2.Weight = 1500;
s1.Run ();
s1.Stop ();
s2.Run ();
s2.Stop ();
}
void Update () {
}
}