인터페이스 예제

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
using System;
 
namespace InterfaceEx
{
    interface IA
    {
        void MustBeImplement(string text);
    }
 
    interface IB
    {
        string Name { get; set; }
    }
    
    class ClassB : IA, IB
    {
        public string Name { get; set; }
        public void MustBeImplement(string name)
        {
            this.Name = name;
            Console.WriteLine("{0} implement this method!!"this.Name);
        }
    }
    class MainClass
    {
        public static void Main (string[] args)
        {
            ClassB b = new ClassB();
            b.MustBeImplement("B");
            
            //IA iaa = new IA();
            
            IA ia = new ClassB();
            ia.MustBeImplement("IA");
            
            Console.ReadKey();
        }
    }
}
cs


추상클래스 예제

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
using System;
 
namespace AbstractClassEx
{
    abstract class ClassA
    {
        private string txtA;
        protected string txtB;
 
        public void WriteTextToConsole(string text)
        {
            this.txtA = text;
            Console.WriteLine(this.txtA);
        }
        
        public abstract void MustBeImplemented(string text);
    }
    
    class classB : ClassA
    {
        public override void MustBeImplemented(string text)
        {
            this.txtB = text;
            Console.WriteLine("{0} implement this method"this.txtB);
        }
    }
 
    class MainClass
    {
        public static void Main (string[] args)
        {
            classB b = new classB();
            b.WriteTextToConsole("B");
            b.MustBeImplemented("B");
            
            //ClassA aa = new ClassA();
            
            ClassA a = new classB();
            a.WriteTextToConsole("A");
            a.MustBeImplemented("A");
            
            Console.ReadKey();
        }
    }
}
cs


+ Recent posts