C#교과서 마스터하기 31. Class 기타, 부분 클래스, 메서드 체이닝, 불변 형식, 변환연산자

min seung moon·2021년 7월 14일
0

C#

목록 보기
32/54

https://www.youtube.com/watch?v=3RNjm-5jYD4&list=PLO56HZSjrPTB4NxAsEP8HRk6YKBDLbp7m&index=65

1. 부분 클래스

  • 동일한 이름의 클래스를 하나 이상 두고 개발할 떼 partial 키워드 사용
  • FirsetDevloper.cs, SecondDevloper.cs 하나의 클래스를 나눠서 작성
  • FirsetDevloper.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace testProject
{
    public partial class Hello
    {
        public void Hi() => Console.WriteLine("FirstDeveloper.cs");
    }
}
  • SecondDevloper.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace testProject
{
    public partial class Hello
    {
        public void Bye() => Console.WriteLine("SecondDevleloper.cs");
    }
}
  • Program.cs
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace testProject
{
    // 이벤트 구독자(Subscriber)
    class Program
    {
        static void Main(string[] args)
        {
            Hello hello = new Hello();
            hello.Hi(); // FirsetDeveloper.cs
            hello.Bye(); // SecondDeveloper.cs
        }


    }
}

2. 함수형 프로그래밍 스타일 : 메서드 체이닝

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

namespace testProject
{
    class Point
    {
        // readonly 필드
        public readonly int x;
        public readonly int y;

        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        public Point MoveBy(int dx, int dy)
        {
            return new Point(x + dx, y + dy);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Point point = new Point(0, 0);
            WriteLine(point.x);
            WriteLine(point.y);
            // WriteLine(point.x = 10);

            // 메서드 체이닝
            Point newPoint = point.MoveBy(100, 100).MoveBy(50, 50);
            WriteLine(newPoint.x);
            WriteLine(newPoint.y);

        }
    }
}

3. 불변 형식(Immutable)

  • 개체의 상태는 생성 후 변경되지 않아야 프로그래밍 부작용을 줄임
  • public한 필드는 readonly 또는 set에 private 적용
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace testProject
{
    class Circle
    {
        public int Radius { get; private set; } = 0;
        public Circle(int radius) => Radius = radius;
        public Circle MakeNew(int radius) => new Circle(radius);
    }
    class Program
    {
        static void Main(string[] args)
        {
            // [1] 생성자를 통해서 반지름 10인 Circle 개체 생성
            Circle circle = new Circle(10);
            WriteLine($"Radius : {circle.Radius} - {circle.GetHashCode()}");

            // [2] 메서드를 통해서 반지름 20인 Circle 개체 새롭게 생성
            circle = circle.MakeNew(20);
            WriteLine($"Radius : {circle.Radius} - {circle.GetHashCode()}");

        }
    }
}


4. 변환연산자 구현

  • public static implicit operator Car(string name) => new Car(name);
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace testProject
{
    class Car
    {
        public string Name { get; private set; }
        public Car(string name) => Name = name;

        // 생성자에게 재 전송
        public static implicit operator Car(string name) => new Car(name);
    }
    class Program
    {
        static void Main(string[] args)
        {
            Car car1 = new Car("first Car");
            WriteLine(car1.Name);

            Car car2 = "Second Car";
            WriteLine(car2.Name);

        }
    }
}

profile
아직까지는 코린이!

0개의 댓글