虚函数 继承 + 方法重载 + 抽象类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ExtendsTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//1.方法的重写
//存在于子类和父类之间 有继承关系 2.父类的方法前面使用虚方法 子类用重写它 3.存在父类的引用指向子类 就是声明父类的引用 指向new的一个子类对象
/* 子类和父类有一样的方法 且没有什么特殊的修饰词 那么会调用父类的方法 如果是上面2的情况 new的是子类就调用子类的方法 子类和父类有一样的方法 子类的方法前面用了New 如果new的是子类对象就会调用子类的方法
Animalll a = new Animalll(10,“woerjianmin”);
Debug.Log(a.name + “,” + a.ID);
Dog d = new Dog();
d.name = “jianmin”;
d.ID = 10;
d.weight = 555999;
Debug.Log(d.name + “,” + d.ID + d.bark() + “woerjianmin的重量” + d.weight);*/
Animalll ani = new Dog();
ani.Eat();
//Animalll ani2 = new Animalll();
//ani2.Eat();
Dog a = new Dog();
a.Eat();
ani.walk();
//ani2.walk();
a.walk();
}

// Update is called once per frame
void Update()
{}

}

public abstract class Animalll
{
public abstract void fly(); //abstract 可以不写方法体 但是要在类前面加上abstract 抽象类不能被实例化 也就是new出来对象 然后子类必须把这个方法重写
//public void fly()
//{
// Debug.Log(“base fly”);
//}
public int ID { get; set; }
public string name { get; set; }
public Animalll()
{
Debug.Log(“base wucan”);
}
public Animalll(int id, string name)
{
this.ID = id;
this.name = name;
}
public void Eat()
{
Debug.Log(“IM Eatting!"); } public virtual void walk() { Debug.Log("base . walk"); } } //父类的访问性必须大于或等于子类 public class Dog : Animalll { public double weight { set; get; } public string bark(){return "土狗贱民别叫了!";} public Dog() { Debug.Log("dogwucan"); }//隐式调用 public Dog(double weight,int id, string name):base(id,name)//构造器的显示调用 {this.weight = weight;Debug.Log("dog有参数的构造器"); } public new void Eat() { Debug.Log("dogs eat 方法”);
}
public override void walk()
{
Debug.Log(“child`s walk”);
}
public override void fly()
{
Debug.Log(“child fly”);
}
**

static

**
}//静态的属性或方法 直接由类调用 (const定义的常量除外)其他的要new一个对象(实例化)之后调用静态的属性或方法 在自己的类里面直接写 其他地方前面用类名点出来
//静态方法只能调用静态的属性和方法 非静态方法的什么都可以调用/访问

猜你喜欢

转载自blog.csdn.net/bellainvan/article/details/107764427