Scroll indicator done
728x90

# 클래스와 객체

클래스의 구성 : 필드, 메소드

필드 - 상수 정의, 필드, 이벤트

메소드 - 메소드, 생성자, 소멸자, 프로퍼티, 인덱서, 연산자 중복

 

# 문장의 종류

infix exp, postfix exp, prefix exp


P.160 4-1 객체 생성
using System;
using System.Collections.Generic;
using System.Text;

namespace HelloWorld {
    class Fraction {
        int numerator;         // 분자
        int denominator;       // 분모
        public Fraction(int num, int denom) {    // 생성자
            numerator = num;
            denominator = denom;
        }
        public void PrintFraction() {
            Console.WriteLine(numerator + " / " + denominator);
        }
    }
    class FractionApp {
        public static void Main() {
            Fraction f = new Fraction(1, 2);
            f.PrintFraction();
        }
    }
}

P.174 4-9 메소드 중복
using System;
using System.Collections.Generic;
using System.Text;

namespace HelloWorld {
    class MethodOverloadingApp {
        void SomeThing() {
            Console.WriteLine("SomeThing() is called.");
        }
        void SomeThing(int i) {
            Console.WriteLine("SomeThing(int) is called.");
        }
        void SomeThing(int i, int j) {
            Console.WriteLine("SomeThing(int,int) is called.");
        }
        void SomeThing(double d) {
            Console.WriteLine("SomeThing(double) is called.");
        }

        public static void Main() {
            MethodOverloadingApp obj = new MethodOverloadingApp();
            obj.SomeThing();
            obj.SomeThing(526);
            obj.SomeThing(52, 526);
            obj.SomeThing(5.26);
        }
    }
}

P.26 1-4 프로퍼티
using System;

namespace HelloWorld
{
    class PropertyClass
    {
        private int privateValue;
        public int PrivateValue
        {
            get { return privateValue;  }
            set { privateValue = value;  }
        }
        public void PrintValue()
        {
            Console.WriteLine("Hidden Value + " + privateValue);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            int n;
            PropertyClass obj = new PropertyClass();
            obj.PrivateValue = 100;
            obj.PrintValue();
            n = obj.PrivateValue;
            Console.WriteLine("       Value = " + n);
        }
    }
}

P29 1-6 델리게이트 **
using System;
using System.Collections.Generic;
using System.Text;
delegate void SampleDelegate();

namespace HelloWorld {
    class DelegateClass {
        public void DelegateMethod() {
            Console.WriteLine("In the DelegateClass.DelegateMethod ..");
        }
    }
    class DelegateApp {
        public static void Main() {
            DelegateClass obj = new DelegateClass();
            SampleDelegate sd = new SampleDelegate(obj.DelegateMethod);
            sd();
        }
    
    }
}

P.189 4-20 델리게이트 객체 호출
using System;
using System.Collections.Generic;
using System.Text;
delegate void DelegateOne();
delegate void DelegateTwo(int i);

namespace HelloWorld {
    class DelegateClass {
        public void MethodA() {
            Console.WriteLine("In the DlegateClass.methodA ..");
        }
        public void MethodB(int i) {
            Console.WriteLine("DelegateClass.MethodB, i = " + i);
        }
    }
    class DelegateCallApp {
        public static void Main() {
            DelegateClass obj = new DelegateClass();
            DelegateOne d1 = new DelegateOne(obj.MethodA);
            DelegateTwo d2 = new DelegateTwo(obj.MethodB);
            d1();
            d2(10);
        }

    }
}

728x90