본문 바로가기
C#

Interface

by Mostlove 2023. 7. 10.
728x90
반응형

namespace CSharp1
{
    internal class Program
    {

        abstract class Monster
        {
            public abstract void Shout();
        }
        interface IFlyable
        {
            void Fly();
        }
        interface IRunable
        {
            void Run();
        }

        /*        class SkeletonOrc : Orc, Skeleton//다중 상속 불가
       {

       }*/
        class FlyableOrc : Orc, IFlyable, IRunable//interface는 다중으로 가능
        {
            public void Fly()
            {

            }

            public void Run()
            {
                
            }
        }

        class Orc : Monster
        {
            public override void Shout() { Console.WriteLine("그아아아!"); }//없으면 에러 출력
        }
        class Skeleton : Monster
        {
            public override void Shout() { Console.WriteLine("크아아아!!"); }

        }
        static void DoFly(IFlyable flyable)
        {
            flyable.Fly();
        }
        static void Main(string[] args)
        {
            IFlyable orc = new FlyableOrc();
            DoFly(orc);
        }
    }
}

반응형

'C#' 카테고리의 다른 글

Delegate(대리자)  (0) 2023.07.10
Property(프로퍼티)  (0) 2023.07.10
Generic  (0) 2023.07.10
Dictionary  (0) 2023.07.09
List  (0) 2023.07.09