ํด๋์ค๋ ๋ค๋ฅธ ํด๋์ค๋ก๋ถํฐ ์ ์ฐ์ ๋ฌผ๋ ค๋ฐ์ ์ ์๋ค.(ํ๋, ๋ฉ์๋, ํ๋กํผํฐ ๊ฐ์ ๋ฉค๋ฒ๋ค)
ํ์ ํด๋์ค๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ๋ด๋ถ์ ์ผ๋ก ๊ธฐ๋ฐ ํด๋์ค์ ์์ฑ์๋ฅผ ๋จผ์ ํธ์ถํ ๋ค์ ์์ ์ ์์ฑ์๋ฅผ ํธ์ถํ๊ณ ,
๊ฐ์ฒด๊ฐ ์๋ฉธ๋ ๋๋ ๋ฐ๋์ ์์๋ก(ํ์ํด๋์ค -> ๊ธฐ๋ฐ ํด๋์ค)์ข ๋ฃ์๋ฅผ ํธ์ถํ๋ค.
: ๊ธฐ๋ฐ ํด๋์ค ๋จผ์ ์์ฑ๋๊ณ ๋ง์ง๋ง๊น์ง ๋ฒํฐ๋ค ๊ฐ์ฅ ๋์ค์ ์ข ๋ฃ์ ํธ์ถํจ!
๋ง์ฝ ๊ธฐ๋ฐ ํด๋์ค์ ์์ฑ์๊ฐ ๋งค๊ฐ๋ณ์๋ฅผ ์ ๋ ฅ๋ฐ๋๋ก ์ ์ธ๋์ด ์๋ค๋ฉด
ํ์ ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์์ฑํ ๋ ํธ์ถ๋๋ ๊ธฐ๋ฐ ํด๋์ค์ ์์ฑ์์๋ ์ด๋ป๊ฒ ๋งค๊ฐ๋ณ์๋ฅผ ์ ๋ฌํ ์ ์์๊น.
์ด๋ด ๋ 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();
}
}
}
'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 |