Наследование в C#: переопределение класса
Есть базовый класс — AudioPlayer и два наследуемых от него: SoundPlayer и MusicPlayer.
AudioPlayer:
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class AudioPlayer : Singleton<AudioPlayer>
{
protected AudioSource _source;
protected partial void Awake()
{
_source = GetComponent<AudioSource>();
}
}
SoundPlayer:
using UnityEngine;
public class SoundPlayer : AudioPlayer
{
private void Awake()
{
Debug.Log("AWAKE!");
}
}
Как сделать чтобы метод Awake() остался таким какой он есть в AudioPlayer'е, но при этом я мог объявить его в наследуемых классах. Тем самым переопределив их, НО не изменив инструкции из метода в базовом классе.
Ответы (1 шт):
Автор решения: Yaroslav
→ Ссылка
public class Parent {
protected AudioSource source;
public Parent (int a, float b) {
}
protected virtual void Awake() {
source = GetComponent<AudioSource>();
}
}
public class Child: Parent {
public Child (int a, float b, double c, string c) base:(a, b) {
}
public Child (int a, float b, double c) this:(a, b, c, "") {
}
protected override void Awake() {
base.Awake();
}
}