250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • MySQL
  • ใ…ฃใ„ท
  • ์ปดํฌ์ง€์…˜
  • ์œ„์ž„

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

C#/์ด๊ฒƒ์ด C#์ด๋‹ค

23. ์ƒ์†, ์ฝ”๋“œ ์žฌํ™œ์šฉ, ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค, ํŒŒ์ƒ ํด๋ž˜์Šค, base, sealed

2022. 2. 11. 10:39
728x90

ํด๋ž˜์Šค๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์œ ์‚ฐ์„ ๋ฌผ๋ ค๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.(ํ•„๋“œ, ๋ฉ”์†Œ๋“œ, ํ”„๋กœํผํ‹ฐ ๊ฐ™์€ ๋ฉค๋ฒ„๋“ค)

 

ํŒŒ์ƒ ํด๋ž˜์Šค๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋‚ด๋ถ€์ ์œผ๋กœ ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ๋จผ์ € ํ˜ธ์ถœํ•œ ๋’ค์— ์ž์‹ ์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๊ณ ,

๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ๋  ๋•Œ๋Š” ๋ฐ˜๋Œ€์˜ ์ˆœ์„œ๋กœ(ํŒŒ์ƒํด๋ž˜์Šค -> ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค)์ข…๋ฃŒ์ž๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. 

 

: ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค ๋จผ์ € ์ƒ์„ฑ๋˜๊ณ  ๋งˆ์ง€๋ง‰๊นŒ์ง€ ๋ฒ„ํ‹ฐ๋‹ค ๊ฐ€์žฅ ๋‚˜์ค‘์— ์ข…๋ฃŒ์ž ํ˜ธ์ถœํ•จ!

 

๋งŒ์•ฝ ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋„๋ก ์„ ์–ธ๋˜์–ด ์žˆ๋‹ค๋ฉด

ํŒŒ์ƒ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์—๋Š” ์–ด๋–ป๊ฒŒ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์„๊นŒ.

์ด๋Ÿด ๋•Œ base ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

this ํ‚ค์›Œ๋“œ๊ฐ€ "์ž๊ธฐ ์ž์‹ "์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค๋ฉด base๋Š” "๊ธฐ๋ฐ˜ ํด๋ž˜์Šค"๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

this๋ฅผ ํ†ตํ•ด ์ž๊ธฐ ์ž์‹ ์˜ ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ,

base ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

base()๋Š” ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์ด๋‹ค.

ํŒŒ์ƒ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์—์„œ ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋„˜๊ฒจ์ฃผ๊ธฐ ์œ„ํ•ด์„œ๋Š”...

์ด๊ฒƒ ์—ญ์‹œ this์™€ ๊ฐ™๋‹ค.

this()๊ฐ€ ์ž๊ธฐ ์ž์‹ ์˜ ์ƒ์„ฑ์ž์ธ ๊ฒƒ ์ฒ˜๋Ÿผ, base()๋Š” ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์ด๋‹ค.

base()์— ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ๋„˜๊ฒจ ํ˜ธ์ถœํ•˜๋ฉด ์›ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ Base() ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด Name ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

this() ์ž๊ธฐ ์ž์‹ ์˜ ์ƒ์„ฑ์ž์ธ ๊ฒƒ ์ฒ˜๋Ÿผ, base()๋Š” ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์ด๋‹ค.

base()์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋„˜๊ฒจ ํ˜ธ์ถœํ•˜๋ฉด

๊ธฐ๋ฐ˜ ํด๋ž˜์Šค ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class Base
    {
        protected string Name;
        public Base(string Name)
        {
            this.Name = Name;
        }
        ~Base()
        {
            WriteLine("~Base()");
        }
        protected void BaseMethod()
        {
            WriteLine("Unity");
        }
    }

    class Derived : Base 
    {
        public Derived(string Name) : base(Name)
        {
            WriteLine($"{this.Name}");
        }
        ~Derived()
        {
            WriteLine("~Derived()");
        }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            Derived derived = new Derived("1");
        }
    }
}

<์˜ˆ์ œ>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class Base
    {
        protected string Name;
        public Base(string Name) //๊ธฐ๋ฐ˜ ํด๋ž˜์Šค ์ƒ์„ฑ์ž
        {
            this.Name = Name;
            WriteLine($"{this.Name}.Base()");
        }
        ~Base()
        {
            WriteLine($"{this.Name}.~Base");
        }
        public void BaseMethod()
        {
            WriteLine($"{Name}.BaseMethod()");
        }
    }

    class Derived : Base
    {
        public Derived(string Name) : base(Name)
        {
            WriteLine($"{this.Name}.Derived()");
        }
        ~Derived()
        {
            WriteLine($"{this.Name}.~Derived()");
        }
        public void derivedMethod()
        {
            WriteLine($"{Name}.DerivedMethod()");
        }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            Base a = new Base("a");
            a.BaseMethod();

            Derived b = new Derived("b");
            b.BaseMethod();
            b.derivedMethod();

        }
    }
}

<์˜ˆ์ œ ์ถœ๋ ฅ>

a.Base()
a.BaseMethod()
b.Base()
b.Derived()
b.BaseMethod()
b.DerivedMethod()
b.~Derived()
b.~Base
a.~Base

<์—ฐ์Šต ์ฝ”๋“œ>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class Parent
    {
        protected int id;
        public Parent(int id)
        {
            this.id = id;
            WriteLine($"{this.id}.Parent()");
        }
        ~Parent()
        {
            WriteLine($"{this.id}.Parent ํ‡ด์žฅ");
        }
        public void parentMethod()
        {
            WriteLine($"{id}.parentMethod()");
        }
    }

    class Child : Parent
    { 
        public Child(int id) : base(id)
        {
            WriteLine($"{this.id}.Child()");
        }
        ~Child()
        {
            WriteLine($"{this.id}.Child ํ‡ด์žฅ");
        }
        public void childMethod()
        {
            WriteLine($"{id}.childMethod");
        }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            Parent parent = new Parent(1);
            parent.parentMethod();

            Child child = new Child(2);
            child.parentMethod();
            child.childMethod();
        }
    }
}

<์—ฐ์Šต ์ฝ”๋“œ ์ถœ๋ ฅ ๊ฒฐ๊ณผ>

1.Parent()
1.parentMethod()
2.Parent()
2.Child()
2.parentMethod()
2.childMethod
2.Child ํ‡ด์žฅ
2.Parent ํ‡ด์žฅ
1.Parent ํ‡ด์žฅ

 

 

<์‹คํ—˜ 1>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class Base
    {
        public Base()
        {
            WriteLine("Base()");
        }
        ~Base()
        {
            WriteLine("~Base()");
        }
    }

    class Derived : Base 
    {
        public Derived()
        {
            WriteLine("Derived()");
        }
        ~Derived()
        {
            WriteLine("~Derived()");
        }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            Derived derived = new Derived();
        }
    }
}

<์‹คํ—˜ 1 ์ถœ๋ ฅ ๊ฒฐ๊ณผ>

Base()
Derived()
~Derived()
~Base()

<์‹คํ—˜ 2>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class Base
    {
        public Base()
        {
            WriteLine("Base()");
        }
        ~Base()
        {
            WriteLine("~Base()");
        }
        protected void BaseMethod()
        {
            WriteLine("Unity");
        }
    }

    class Derived : Base 
    {
        public Derived()
        {
            base.BaseMethod();
            WriteLine("Derived()");
        }
        ~Derived()
        {
            WriteLine("~Derived()");
        }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            Derived derived = new Derived();
        }
    }
}

<์‹คํ—˜ 2 ์ถœ๋ ฅ ๊ฒฐ๊ณผ>

Base()
Unity
Derived()
~Derived()
~Base()

<์‹คํ—˜ 3>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class Base
    {
        protected string Name;
        public Base(string Name)
        {
            this.Name = Name;
        }
        ~Base()
        {
            WriteLine("~Base()");
        }
        protected void BaseMethod()
        {
            WriteLine("Unity");
        }
    }

    class Derived : Base 
    {
        public Derived(string Name) : base(Name)
        {
            WriteLine($"{this.Name}");
        }
        ~Derived()
        {
            WriteLine("~Derived()");
        }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            Derived derived = new Derived("1");
        }
    }
}

<์‹คํ—˜ 3 ์ถœ๋ ฅ ๊ฒฐ๊ณผ>

1
~Derived()
~Base()

<sealed>

๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ ์ž‘์„ฑ์ž๋Š” ์˜๋„ํ•˜์ง€ ์•Š์€ ์ƒ์†์ด๋‚˜ ํŒŒ์ƒ ํด๋ž˜์Šค์˜ ๊ตฌํ˜„์„ ๋ง‰๊ธฐ ์œ„ํ•ด ์ƒ์†์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋„๋ก ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. sealed ํ•œ์ •์ž๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

sealed ํ•œ์ •์ž๋กœ ํด๋ž˜์Šค๋ฅผ ์ˆ˜์‹ํ•˜๋ฉด, ์ด ํด๋ž˜์Šค๋Š” "์ƒ์† ๋ด‰์ธ"(์ด๋Ÿฐ ํด๋ž˜์Šค๋ฅผ ๋ด‰์ธ ํด๋ž˜์Šค๋ผ๊ณ  ํ•œ๋‹ค)์ด ๋˜์–ด

์ด๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์œผ๋ ค๋Š” ์‹œ๋„๊ฐ€ ์ปดํŒŒ์ผ๋Ÿฌ๋กœ๋ถ€ํ„ฐ ๋ฐœ๊ฒฌ๋์„ ๋•Œ ์—๋Ÿฌ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค. 

 

<sealed๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ ์—๋Ÿฌ๋‚˜๋Š” ์˜ˆ์ œ>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    sealed class Base
    {
        protected string Name;
        public Base(string Name)
        {
            this.Name = Name;
        }
        ~Base()
        {
            WriteLine("~Base()");
        }
        protected void BaseMethod()
        {
            WriteLine("Unity");
        }
    }

    class Derived : Base 
    {
        public Derived(string Name) : base(Name)
        {
            WriteLine($"{this.Name}");
        }
        ~Derived()
        {
            WriteLine("~Derived()");
        }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            Derived derived = new Derived("1");
        }
    }
}

<๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์™€ ํŒŒ์ƒ ํด๋ž˜์Šค ์‚ฌ์ด์˜ ํ˜•์‹ ๋ณ€ํ™˜, ๊ทธ๋ฆฌ๊ณ  is์™€ as>

๊ฐœ์™€ ๊ณ ์–‘์ด๋Š” ์ข…์€ ๋‹ค๋ฅด์ง€๋งŒ ์ –์„ ๋จน์ธ๋‹ค๋Š” ๊ณตํ†ต์  ๋•Œ๋ฌธ์— ํฌ์œ ๋ฅ˜๋กœ ๋ถ„๋ฅ˜๋œ๋‹ค.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class Mammal
    {
        public void Nurse() { }
    }
    class Dog: Mammal
    {
        public void Bark() { }
    }
    class Cat: Mammal
    {
        public void Meow() { }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            Mammal mammal = new Mammal();
            mammal.Nurse();

            Dog dog = new Dog();
            dog.Nurse();
            dog.Bark();

            Cat cat = new Cat();
            cat.Nurse();
            cat.Meow();
        }
    }
}

๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์™€ ํŒŒ์ƒ ํด๋ž˜์Šค ์‚ฌ์ด์—์„œ๋Š” ์กฑ๋ณด๋ฅผ ์˜ค๋ฅด๋‚ด๋ฆฌ๋Š” ํ˜•์‹ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ,

ํŒŒ์ƒ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋Š” ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋กœ์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋ฅผ ํ†ตํ•ด ์ฝ”๋“œ์˜ ์ƒ์‚ฐ์„ฑ์ด ๋†’์•„์ง„๋‹ค. 

 

-๋™๋ฌผ์›์˜ ์˜ˆ์‹œ-

Mammal ํด๋ž˜์Šค์—์„œ 300๊ฐ€์ง€์˜ ํด๋ž˜์Šค๊ฐ€ ํŒŒ์ƒ๋˜์—ˆ๋‹ค๊ณ  ์น˜๊ณ ,

๋™๋ฌผ์›์˜ ์‚ฌ์œก์‚ฌ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ๋™๋ฌผ๋“ค์„ ์”ป๊ธฐ๋Š” Wash()๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž.

๋‹ค์Œ๊ณผ ๊ฐ™์ด 300๊ฐœ์˜ Wash()๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋กœ๋”ฉํ•ด์•ผ ํ•œ๋‹ค.

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class ZooKeeper
    {
        public void Wash(Dog dog) {/*~Wash ๋ฉ”์†Œ๋“œ ๋‚ด์šฉ~*/ }
        public void Wash(Cat cat) {/*~Wash ๋ฉ”์†Œ๋“œ ๋‚ด์šฉ~*/ }
        public void Wash(Rabbit rabbit) {/*~Wash ๋ฉ”์†Œ๋“œ ๋‚ด์šฉ~*/ }
        public void Wash(Whale whale) {/*~Wash ๋ฉ”์†Œ๋“œ ๋‚ด์šฉ~*/ }
        
        //๋“ฑ๋“ฑ... 300๊ฐœ ๋ฒ„์ „์˜ Wash() ๋ฉ”์†Œ๋“œ ์„ ์–ธ

    }
    class Mammal
    {
        public void Nurse() { }
    }
    class Dog: Mammal
    {
        public void Bark() { }
    }
    class Rabbit : Mammal
    {
        public void Jump() { }
    }
    class Whale : Mammal
    {
        public void Swim() { }
    }
    class Cat : Mammal
    {
        public void Meow() { }
    }
    class MainApp
    {
        static void Main(string[] args)
        {
            Mammal mammal = new Mammal();
            mammal.Nurse();

            mammal = new Dog();
            mammal.Nurse();

            Dog dog = (Dog)mammal;
            dog.Nurse();
            dog.Bark();

            mammal = new Cat();
            mammal.Nurse();

            Cat cat = (Cat)mammal;
            cat.Nurse();
            cat.Meow();
        }
    }
}

ํ•˜์ง€๋งŒ 300๊ฐœ์˜ ๋™๋ฌผ ํด๋ž˜์Šค๊ฐ€ ๋ชจ๋‘ Mammal ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์—

์ด๋“ค์€ ๋ชจ๋‘ Mammal๋กœ ๊ฐ„์ฃผํ•  ์ˆ˜ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋”ฑ ํ•˜๋‚˜์˜ Wash()๋ฉ”์†Œ๋“œ๋งŒ ์ค€๋น„ํ•˜๋ฉด 300๊ฐœ์˜ ๋™๋ฌผ ํด๋ž˜์Šค์— ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class ZooKeeper
    {
        public void Wash(Mammal mammal) {/*~Wash ๋ฉ”์†Œ๋“œ ๋‚ด์šฉ~*/ }


    }
    class Mammal
    {
        public void Nurse() { }
    }
    class Dog: Mammal
    {
        public void Bark() { }
    }
    class Rabbit : Mammal
    {
        public void Jump() { }
    }
    class Whale : Mammal
    {
        public void Swim() { }
    }
    class Cat : Mammal
    {
        public void Meow() { }
    }
    class MainApp
    {
        static void Main(string[] args)
        {
            Mammal mammal = new Mammal();
            mammal.Nurse();

            mammal = new Dog();
            mammal.Nurse();

            Dog dog = (Dog)mammal;
            dog.Nurse();
            dog.Bark();

            mammal = new Cat();
            mammal.Nurse();

            Cat cat = (Cat)mammal;
            cat.Nurse();
            cat.Meow();
        }
    }
}
728x90

'C# > ์ด๊ฒƒ์ด C#์ด๋‹ค' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

24. C# ํ˜•์‹ ๋ณ€ํ™˜ ์—ฐ์‚ฐ์„ ์œ„ํ•œ ์—ฐ์‚ฐ์ž, is์™€ as  (0) 2022.02.11
[์ค‘๊ฐ„์ ๊ฒ€] ์ ‘๊ทผํ•œ์ •์ž internal์—์„œ ๊ฐ™์€ ์–ด์…ˆ๋ธ”๋ฆฌ๋ž€  (0) 2022.02.11
22. ์€๋‹‰์„ฑ, ์ ‘๊ทผ ํ•œ์ •์ž  (0) 2022.02.10
21. this, this() ์ƒ์„ฑ์ž  (0) 2022.02.10
20. ์–•์€ ๋ณต์‚ฌ(Shallow Copy)์™€ ๊นŠ์€ ๋ณต์‚ฌ(Deep Copy)  (0) 2022.02.10
    'C#/์ด๊ฒƒ์ด C#์ด๋‹ค' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • 24. C# ํ˜•์‹ ๋ณ€ํ™˜ ์—ฐ์‚ฐ์„ ์œ„ํ•œ ์—ฐ์‚ฐ์ž, is์™€ as
    • [์ค‘๊ฐ„์ ๊ฒ€] ์ ‘๊ทผํ•œ์ •์ž internal์—์„œ ๊ฐ™์€ ์–ด์…ˆ๋ธ”๋ฆฌ๋ž€
    • 22. ์€๋‹‰์„ฑ, ์ ‘๊ทผ ํ•œ์ •์ž
    • 21. this, this() ์ƒ์„ฑ์ž
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”