본문 바로가기
프로그래밍

2_13. 추상클래스

by BlueOcean&Shark 2019. 1. 27.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace cshapExTwo16
{
    /*
     * 추상클래스 
     * - 인터페이스와 비슷하다. 하지만, 추상클래스는 구현(몸통)부를 가질 수 있다.
     * - 추상클래스는 구현을 갖을 수 있지만 인스턴스를 만들 수 없다.
     * - 사용하는 한정자는 abstract와 class를 사용한다.
     * - 클래스 선언형식
     *   abstract class 클래스명
     *   {
     *       클래스와 동일
     *   }
     * - 추상클래스는 클래스와 달리 추상메소드를 갖고 있다.
     * - 추상클래스는 접근제한자를 사용하지 않을경우 private로 설정된다.
     * - 추상메소드를 지정할 때 abstract 키워드를 사용한다.
     * - 추상메소드의 형식
     *   public abstract void A();
     * - 구현할 부분이 있는것과 구현할 부분이 없는것을 구분해서 프로그래머들에게 알려줌
     *
     */
 
    abstract class MyAbstractClass
    {
        protected void ProtectedMethod()
        {
            Console.WriteLine("추상클래스의 ProtectedMethod 호출");
        }
        public void PublicMethod()
        {
            Console.WriteLine("추상클래스의 PublicMethod 호출");
        }
        public abstract void AbstractMethod();
    }
    class MyClass : MyAbstractClass
    {
        public override void AbstractMethod() //override키워드가 무조건 들어가야됨
        {
            Console.WriteLine("클래스의 AbstractMethod 호출");
            ProtectedMethod();           
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            MyClass mc1 = new MyClass();
            mc1.AbstractMethod();
            mc1.PublicMethod();
        }
    }
}
 
cs

--------------------------
--------------------------------결과창-------------------------------------------

클래스의 AbstractMethod 호출

추상클래스의 ProtectedMethod 호출

추상클래스의 PublicMethod 호출

'프로그래밍' 카테고리의 다른 글

2_15.자동프로퍼티  (0) 2019.01.27
2_14. 프로퍼티  (0) 2019.01.27
2_12. 인터페이스  (0) 2019.01.27
2_11. Tuple(튜플)  (0) 2019.01.22
2_10. struct (구조체)  (0) 2019.01.22

댓글