C#교과서 마스터하기 32. 상속으로 클래스 확장하기

min seung moon·2021년 7월 14일
0

C#

목록 보기
33/54

https://www.youtube.com/watch?v=kkKLpwowinc&list=PLO56HZSjrPTB4NxAsEP8HRk6YKBDLbp7m&index=66

1. 클래스 상속하기

  • 부모 클래스에 저의된 기능을 다시 사용, 학장 및 수정하여 자식 클래스로 만들기

01. 상속 : 부모와 자식

  • 부모 클래스와 자식 클래스
    • 부모 클래스
      • 수퍼 클래스, 베이스(base) 클래스, 기본 클래스
      • 특정 클래스에게 상속을 부여해줄 클래스
      • 공통 기능을 모아 놓음
    • 자식 클래스
      • 서브 클래스, 파생(derived) 클래스
      • 특정 형식에게 상속을 부여 받는 타입

2. 부모 클래스와 자식 클래스

  • :으로 상속
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace testProject
{
    // 자동차 관련 부모 클래스
    class Car
    {
        public void Go() => WriteLine("달리다");
    }

    // Car의 자식 클래스
    class Benz : Car
    {

    }

    class Tesla : Car
    {

    }
    class Program : Object
    {
        static void Main(string[] args)
        {
            (new Benz()).Go();
            (new Tesla()).Go();
        }
    }
}

3. Base 클래스와 Sub 클래스

using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace testProject
{
    // 열거형
    public enum CarType { EV, ICEV }

    class Car
    {
        public void Go() => WriteLine("달리다");

    }

    // Car의 자식 클래스
    class Benz : Car
    {
        public CarType Style { get; private set; }
        public Benz()
        {
            Style = CarType.ICEV;
        }
        public Benz(CarType carType)
        {
            Style = carType;
        }
    }

    class Tesla : Car
    {
        public CarType Style { get; set; }
        public Tesla()
        {
            Style = CarType.EV;
        }

        public Tesla(CarType carType)
        {
            Style = carType;
        }
    }
    class Program : Object
    {
        static void Main(string[] args)
        {
            Benz benz = new Benz();
            benz.Go();
            WriteLine(benz.Style);

            Tesla tesla = new Tesla();
            tesla.Go();
            WriteLine(tesla.Style);
        }
    }
}

4. Object 클래스 상속

  • Object Class는 시조 최상위 Class 이다
    • 기본적으로 모든 Class Object Class를 상속 받아 있다
      • class ClassName : Object

5. 부모 클래스 형식 변수에 자식 클래스의 개체 할당

6. this와 this() 그리고 base와 base()

  • this, this()는 this
  • base, base()는 super
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace testProject
{
    // 열거형
    public enum CarType { EV, ICEV }

    // 추상 클래스
    public abstract class CarBase
    {
        public abstract void Left(); // 추상 메서드, 본문X, 시그니처O => 표준(강제) => 인터페이스
        public void Back() => WriteLine("후진하다");
    }

    class Car : CarBase
    {
        public CarType Style { get; private set; }

        public void Go() => WriteLine("달리다");

        // 추상화 재정의
        public override void Left() => WriteLine("좌회전하다");

        public Car(CarType carType)
        {
            Style = carType;
        }
    }

    // Car의 자식 클래스
    class Benz : Car
    {
        
        public Benz() : this(CarType.ICEV){}
        public Benz(CarType carType) : base(carType) { }
    }

    class Tesla : Car
    {
        public Tesla() : this(CarType.EV) { }

        public Tesla(CarType carType) : base(carType) { }
    }
    class Program : Object
    {
        static void Main(string[] args)
        {
            Benz benz = new Benz();
            benz.Go();
            benz.Back();
            benz.Left();
            WriteLine(benz.Style);

            Tesla tesla = new Tesla();
            tesla.Go();
            tesla.Back();
            tesla.Left();
            WriteLine(tesla.Style);
        }
    }
}



7. 봉인 클래스(상속 봉인)

  • sealed 키워드 사용
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace testProject
{
    // 열거형
    public enum CarType { EV, ICEV }

    // 추상 클래스
    public abstract class CarBase
    {
        public abstract void Left(); // 추상 메서드, 본문X, 시그니처O => 표준(강제) => 인터페이스
        public void Back() => WriteLine("후진하다");
    }

    class Car : CarBase
    {
        public CarType Style { get; private set; }

        public void Go() => WriteLine("달리다");

        // 추상화 재정의
        public override void Left() => WriteLine("좌회전하다");

        public Car(CarType carType)
        {
            Style = carType;
        }
    }

    // Car의 자식 클래스
    class Benz : Car
    {
        
        public Benz() : this(CarType.ICEV){}
        public Benz(CarType carType) : base(carType) { }
    }

    class Tesla : Car
    {
        public Tesla() : this(CarType.EV) { }

        public Tesla(CarType carType) : base(carType) { }
    }

    // 봉인 클래스(최종)
    sealed class Future : Car
    {
        public Future() : this(CarType.EV) { }
        public Future(CarType carType) : base(carType) { }

        public new void Go()
        {
            base.Go();
            WriteLine("날다");
        }
    }

    class OtherFuture : Future
    {

    }

    class Program : Object
    {
        static void Main(string[] args)
        {
            Benz benz = new Benz();
            benz.Go();
            benz.Back();
            benz.Left();
            WriteLine(benz.Style);

            Tesla tesla = new Tesla();
            tesla.Go();
            tesla.Back();
            tesla.Left();
            WriteLine(tesla.Style);

            Future future = new Future();
            future.Go();
        }
    }
}

8. 추상 클래스

using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace testProject
{
    // 열거형
    public enum CarType { EV, ICEV }

    // 추상 클래스
    public abstract class CarBase
    {
        public abstract void Left(); // 추상 메서드, 본문X, 시그니처O => 표준(강제) => 인터페이스
        public void Back() => WriteLine("후진하다");
    }

    class Car : CarBase
    {
        public void Go() => WriteLine("달리다");

        // 추상화 재정의
        public override void Left() => WriteLine("좌회전하다");
    }

    // Car의 자식 클래스
    class Benz : Car
    {
        public CarType Style { get; private set; }
        public Benz()
        {
            Style = CarType.ICEV;
        }
        public Benz(CarType carType)
        {
            Style = carType;
        }
    }

    class Tesla : Car
    {
        public CarType Style { get; set; }
        public Tesla()
        {
            Style = CarType.EV;
        }

        public Tesla(CarType carType)
        {
            Style = carType;
        }
    }
    class Program : Object
    {
        static void Main(string[] args)
        {
            Benz benz = new Benz();
            benz.Go();
            benz.Back();
            benz.Left();
            WriteLine(benz.Style);

            Tesla tesla = new Tesla();
            tesla.Go();
            tesla.Back();
            tesla.Left();
            WriteLine(tesla.Style);
        }
    }
}

9. 자식 클래스에만 멤버 상속하기

  • protected 키워드 사용
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace testProject
{
    // 열거형
    public enum CarType { EV, ICEV }

    // 추상 클래스
    public abstract class CarBase
    {
        public abstract void Left(); // 추상 메서드, 본문X, 시그니처O => 표준(강제) => 인터페이스
        public void Back() => WriteLine("후진하다");
        protected string LeftMessage { get; private set; } = "좌회전하다";
    }

    class Car : CarBase
    {
        public CarType Style { get; private set; }

        public void Go() => WriteLine("달리다");

        // 추상화 재정의
        public override void Left() => WriteLine(LeftMessage);

        public Car(CarType carType)
        {
            Style = carType;
        }
    }

    // Car의 자식 클래스
    class Benz : Car
    {
        
        public Benz() : this(CarType.ICEV){}
        public Benz(CarType carType) : base(carType) { }
    }

    class Tesla : Car
    {
        public Tesla() : this(CarType.EV) { }

        public Tesla(CarType carType) : base(carType) { }
    }

    // 봉인 클래스(최종)
    sealed class Future : Car
    {
        public Future() : this(CarType.EV) { }
        public Future(CarType carType) : base(carType) { }

        public new void Go()
        {
            base.Go();
            WriteLine("날다");
        }
    }

    class Program : Object
    {
        static void Main(string[] args)
        {
            Benz benz = new Benz();
            benz.Go();
            benz.Back();
            benz.Left();
            WriteLine(benz.Style);

            Tesla tesla = new Tesla();
            tesla.Go();
            tesla.Back();
            tesla.Left();
            WriteLine(tesla.Style);

            Future future = new Future();
            future.Go();
        }
    }
}

10. 기본 클래스의 멤버 숨기기

11. 부모의 메서드 재정의(new)

  • 부모에게 상속 받은 메서드를 재정의 할 경우 new 키워드 사용
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace testProject
{
    // 열거형
    public enum CarType { EV, ICEV }

    // 추상 클래스
    public abstract class CarBase
    {
        public abstract void Left(); // 추상 메서드, 본문X, 시그니처O => 표준(강제) => 인터페이스
        public void Back() => WriteLine("후진하다");
    }

    class Car : CarBase
    {
        public CarType Style { get; private set; }

        public void Go() => WriteLine("달리다");

        // 추상화 재정의
        public override void Left() => WriteLine("좌회전하다");

        public Car(CarType carType)
        {
            Style = carType;
        }
    }

    // Car의 자식 클래스
    class Benz : Car
    {
        
        public Benz() : this(CarType.ICEV){}
        public Benz(CarType carType) : base(carType) { }
    }

    class Tesla : Car
    {
        public Tesla() : this(CarType.EV) { }

        public Tesla(CarType carType) : base(carType) { }
    }

    class Future : Car
    {
        public Future() : this(CarType.EV) { }
        public Future(CarType carType) : base(carType) { }

        public new void Go()
        {
            base.Go();
            WriteLine("날다");
        }
    }
    class Program : Object
    {
        static void Main(string[] args)
        {
            Benz benz = new Benz();
            benz.Go();
            benz.Back();
            benz.Left();
            WriteLine(benz.Style);

            Tesla tesla = new Tesla();
            tesla.Go();
            tesla.Back();
            tesla.Left();
            WriteLine(tesla.Style);

            Future future = new Future();
            future.Go();
        }
    }
}


profile
아직까지는 코린이!

0개의 댓글