"๋", "์ ๋" ...
์๊ธฐ ์์ ์ ์ง์นญํ ๋ "๋"๋ผ๊ณ ํ๋ค.
this๋ ๊ฐ์ฒด๊ฐ ์์ ์ ์ง์นญํ ๋ ์ฌ์ฉํ๋ ํค์๋์ด๋ค.
๊ทธ๋ฌ๋๊น,
๊ฐ์ฒด ์ธ๋ถ์์๋ ๊ฐ์ฒด์ ํ๋๋ ๋ฉ์๋์ ์ ๊ทผํ ๋ ๊ฐ์ฒด์ ์ด๋ฆ(๋ณ์ ๋๋ ์๋ณ์)๋ฅผ ์ฌ์ฉํ๋ค๋ฉด,
๊ฐ์ฒด ๋ด๋ถ์์๋ ์์ ์ ํ๋๋ ๋ฉ์๋์ ์ ๊ทผํ ๋ this ํค์๋๋ฅผ ์ฌ์ฉํ๋ค.
this๊ฐ ๊ฐ์ฒด ์์ ์ ์ง์นญํ๋ ํค์๋์ธ ๊ฒ์ฒ๋ผ,
this()๋ ์๊ธฐ ์์ ์ ์์ฑ์๋ฅผ ์นด๋ฆฌํจ๋ค.
this()๋ ์์ฑ์์์๋ง ์ฌ์ฉ๋ ์ ์๋ค.
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 Employee
{
private string Name;
private string Position;
public void SetName(string Name)//๋งค๊ฐ๋ณ์ Name
{
this.Name = Name; //์์ ์ ํ๋ this.Name
}
public string GetName()
{
return Name;
}
public void SetPosition(string Position)
{
this.Position = Position;
}
public string GetPosition()
{
return this.Position;
}
}
class MainApp
{
static void Main(string[] args)
{
Employee pooh = new Employee();
pooh.SetName("Pooh");
pooh.SetPosition("Waiter");
WriteLine($"{pooh.GetName()} {pooh.GetPosition()}"); //Pooh Waiter
Employee tigger = new Employee();
tigger.SetName("Tigger");
tigger.SetPosition("Clenaer");
WriteLine($"{tigger.GetName()} {tigger.GetPosition()}"); //Tigger Clenaer
}
}
}
<์ถ๋ ฅ๊ฒฐ๊ณผ>
Pooh Waiter
Tigger Clenaer
this๊ฐ ๊ฐ์ฒด ์์ ์ ์ง์นญํ๋ ํค์๋์ธ ๊ฒ์ฒ๋ผ,
this()๋ ์๊ธฐ ์์ ์ ์์ฑ์๋ฅผ ์นด๋ฆฌํจ๋ค.
this()๋ ์์ฑ์์์๋ง ์ฌ์ฉ๋ ์ ์๋ค.
<์์ ์ฝ๋>
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 MyClass
{
int a, b, c;
public MyClass()
{
this.a = 5425;
WriteLine("MyClass()");
}
public MyClass(int b) : this()
{
this.b = b;
WriteLine($"MyClass({b})");
}
public MyClass(int b, int c) : this(b)
{
this.c = c;
WriteLine($"MyClass({b}, {c})");
}
public void PirntFields()
{
WriteLine($"a:{a}, b:{b}, c:{c}");
}
}
class MainApp
{
static void Main(string[] args)
{
MyClass a = new MyClass();
a.PirntFields();
WriteLine();
MyClass b = new MyClass(1);
b.PirntFields();
WriteLine();
MyClass c = new MyClass(10, 20);
c.PirntFields();
}
}
}
<์ถ๋ ฅ ๊ฒฐ๊ณผ>
MyClass()
a:5425, b:0, c:0
MyClass()
MyClass(1)
a:5425, b:1, c:0
MyClass()
MyClass(10)
MyClass(10, 20)
a:5425, b:10, c:20
this()๋ฅผ ์ด์ฉํ์ฌ ์ฝ๋๋ฅผ ๊ฐ์ ํ ์ (์ -> ํ)
-์ฝ๋ ๊ฐ์ ์ -
class MyClass
{
int a, b, c;
public MyClass()
{
this.a = 5425;
}
public MyClass(int b)
{
this.a = 5425;
this.b = b;
}
public MyClass(int b, int c)
{
this.a = 5425;
this.b = b;
this.c = c;
}
public void PirntFields()
{
WriteLine($"a:{a}, b:{b}, c:{c}");
}
}
-์ฝ๋ ๊ฐ์ ํ-
class MyClass
{
int a, b, c;
public MyClass()
{
this.a = 5425;
}
public MyClass(int b) : this() //this()๋ MyClass()๋ฅผ ํธ์ถํ๋ค.
{
this.b = b;
}
public MyClass(int b, int c) : this(b) //this(int)๋ MyClass(int)๋ฅผ ํธ์ถํ๋ค.
{
this.c = c;
}
}
<์คํ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 MyClass
{
int a, b, c;
public MyClass()
{
this.a = 5425;
}
public MyClass(int b) : this() //this()๋ MyClass()๋ฅผ ํธ์ถํ๋ค.
{
this.b = b;
}
public MyClass(int b, int c) : this(b) //this(int)๋ MyClass(int)๋ฅผ ํธ์ถํ๋ค.
{
this.c = c;
}
public void PirntFields()
{
WriteLine($"a:{a}, b:{b}, c:{c}");
}
}
class MainApp
{
static void Main(string[] args)
{
MyClass a = new MyClass();
a.PirntFields(); //a:5425, b:0, c:0
WriteLine();
MyClass b = new MyClass(1);
b.PirntFields(); //a:5425, b:1, c:0
WriteLine();
MyClass c = new MyClass(10, 20);
c.PirntFields(); //a:5425, b:10, c:20
}
}
}
<์คํ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 MyClass
{
int a, b, c;
public MyClass()
{
this.a = 5425;
}
public MyClass(int b)
{
this.b = b;
}
public MyClass(int b, int c) : this(b)
{
this.c = c;
}
public void PirntFields()
{
WriteLine($"a:{a}, b:{b}, c:{c}");
}
}
class MainApp
{
static void Main(string[] args)
{
MyClass a = new MyClass();
a.PirntFields(); //a:5425, b:0, c:0
WriteLine();
MyClass b = new MyClass(1);
b.PirntFields(); //a:0, b:1, c:0
WriteLine();
MyClass c = new MyClass(10, 20);
c.PirntFields(); //a:0, b:10, c:20
}
}
}
<์คํ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 MyClass
{
int a, b, c;
public MyClass()
{
this.a = 5425;
}
public MyClass(int b)
{
this.b = b;
}
public MyClass(int b, int c) : this()
{
this.c = c;
}
public void PirntFields()
{
WriteLine($"a:{a}, b:{b}, c:{c}");
}
}
class MainApp
{
static void Main(string[] args)
{
MyClass a = new MyClass();
a.PirntFields(); //a: 5425, b: 0, c: 0
WriteLine();
MyClass b = new MyClass(1);
b.PirntFields(); //a: 0, b: 1, c: 0
WriteLine();
MyClass c = new MyClass(10, 20);
c.PirntFields(); //a: 5425, b: 0, c: 20
}
}
}
<์ฐ์ต>
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 MyClass
{
private int a, b, c;
public MyClass()
{
this.a = 9876;
}
public MyClass(int b) : this()
{
this.b = b;
}
public MyClass(int b, int c) : this(b)
{
this.c = c;
}
public void PrintStatus()
{
WriteLine($"a: {a}, b : {b}, c : {c}");
}
}
class MainApp
{
static void Main(string[] args)
{
MyClass myClass1 = new MyClass();
myClass1.PrintStatus(); //a: 9876, b : 0, c : 0
MyClass myClass2 = new MyClass(10,99);
myClass2.PrintStatus(); //a: 9876, b : 10, c : 99
MyClass myClass3 = new MyClass(101, 9999);
myClass3.PrintStatus(); //a: 9876, b : 101, c : 9999
}
}
}
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
23. ์์, ์ฝ๋ ์ฌํ์ฉ, ๊ธฐ๋ฐ ํด๋์ค, ํ์ ํด๋์ค, base, sealed (0) | 2022.02.11 |
---|---|
22. ์๋์ฑ, ์ ๊ทผ ํ์ ์ (0) | 2022.02.10 |
20. ์์ ๋ณต์ฌ(Shallow Copy)์ ๊น์ ๋ณต์ฌ(Deep Copy) (0) | 2022.02.10 |
19. [static]์ ์ ํ๋์ ์ ์ ๋ฉ์๋ (0) | 2022.02.10 |
18. ๊ฐ์ฒด์ ์ถ๊ณผ ์ฃฝ์(์์ฑ์,์ข ๋ฃ์) + ์์ฑ์ ์ค๋ฒ๋ก๋ฉ (0) | 2022.02.10 |