InterfaceDesignExample.cs 593 B

123456789101112131415161718192021222324252627282930313233
  1. using UnityEngine;
  2. namespace FrameWorkDesign
  3. {
  4. public interface ICanSayHello
  5. {
  6. void SayHello();
  7. void SayOther();
  8. }
  9. public class InterfaceDesignExample : MonoBehaviour, ICanSayHello
  10. {
  11. //接口隐式实现
  12. public void SayHello()
  13. {
  14. Debug.Log("Hello");
  15. }
  16. //接口的显式实现
  17. void ICanSayHello.SayOther()
  18. {
  19. Debug.Log("Other");
  20. }
  21. private void Start()
  22. {
  23. this.SayHello();
  24. (this as ICanSayHello).SayOther();
  25. }
  26. }
  27. }