κ°μ²΄κ° μμ±λ λλ μμ±μκ° νΈμΆλκ³ μλ©Έν λλ μ’ λ£μκ° νΈμΆλλ€.
<μμ±μ>
μμ±μλ ν΄λμ€μ μ΄λ¦μ΄ κ°κ³ λ°ν νμμ΄ μλ€.
μμ±μμ μ무λ ν΄λΉ νμ(ν΄λμ€)μ κ°μ²΄λ₯Ό μμ±νλ κ² λΏμ΄κΈ° λλ¬Έμ΄λ€.
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 Cat
{
public Cat()
{
}
}
class MainApp
{
static void Main(string[] args)
{
Cat kitty = new Cat(); //kitty κ°μ²΄ μμ±
kitty.SetColor("νμμ");
kitty.SetName("ν€ν°");
kitty.Meow();
WriteLine($"{kitty.GetName()}μ μκΉμ {kitty.GetColor()}μ΄λλλ€.");
}
}
}
μ¬μ€ ν΄λμ€λ₯Ό μ μΈν λ λͺ μμ μΌλ‘ μμ±μλ₯Ό ꡬννμ§ μμλ μ»΄νμΌλ¬μμ μμ±μλ₯Ό λ§λ€μ΄μ€λ€.(κΈ°λ³Έ μμ±μ)
κ°μ²΄λ₯Ό λ€λ£¨λ€λ³΄λ©΄ κ°μ²΄λ₯Ό μμ±νλ μμ μ κ°μ²΄μ μνλ₯Ό,
λ€μ λ§ν΄ κ°μ²΄μ νλλ₯Ό μνλ κ°μΌλ‘ μ΄κΈ°ννκ³ μΆμ λκ° μλ€.
κ·Έλ΄ λ, 맀κ°λ³μλ₯Ό μ λ ₯λ°μ νλλ₯Ό μνλ κ°μΌλ‘ μ΄κΈ°νν μ μλ μ΅μ μ μ₯μκ° λ°λ‘ μμ±μμ΄λ€.
μμ±μλ μ€λ²λ‘λ©μ΄ κ°λ₯νλ€.
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 Cat
{
public Cat()
{
name = "";
color = "";
}
public Cat(string _name, string _color)
{
name = _name;
color = _color;
}
private string name; //ν΄λμ€ μμ μ μΈλ λ³μ : νλ
private string color; //ν΄λμ€ μμ μ μΈλ λ³μ : νλ
public void SetName(string newName)
{
name = newName;
}
public string GetName()
{
return name;
}
public void SetColor(string newColor)
{
color = newColor;
}
public string GetColor()
{
return color;
}
public void Meow() //λ©μλ
{
WriteLine($"{color} κ³ μμ΄ {name}κ° μΌμΉ!");
}
}
class MainApp
{
static void Main(string[] args)
{
Cat kitty = new Cat(); //kitty κ°μ²΄ μμ±
kitty.SetColor("νμμ");
kitty.SetName("ν€ν°");
kitty.Meow();
WriteLine($"{kitty.GetName()}μ μκΉμ {kitty.GetColor()}μ΄λλλ€.");
}
}
}
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 Cat
{
public Cat()
{
name = "";
color = "";
}
public Cat(string _name, string _color)
{
name = _name;
color = _color;
}
private string name; //ν΄λμ€ μμ μ μΈλ λ³μ : νλ
private string color; //ν΄λμ€ μμ μ μΈλ λ³μ : νλ
public void SetName(string newName)
{
name = newName;
}
public string GetName()
{
return name;
}
public void SetColor(string newColor)
{
color = newColor;
}
public string GetColor()
{
return color;
}
public void Meow() //λ©μλ
{
WriteLine($"{color} κ³ μμ΄ {name}κ° μΌμΉ!");
}
}
class MainApp
{
static void Main(string[] args)
{
Cat nabi = new Cat(); //nabi κ°μ²΄ μμ±
Cat neko = new Cat("neko","νμ"); //nabi κ°μ²΄ μμ±
nabi.Meow();
neko.Meow();
}
}
}
<μΆλ ₯ κ²°κ³Ό>
κ³ μμ΄ κ° μΌμΉ!
νμ κ³ μμ΄ nekoκ° μΌμΉ!
μλ μμ€μ½λλ μμ±μ μ€λ²λ‘λ©μμ΄ μΈμλ₯Ό λ°λ μμ±μλ§ μκΈ° λλ¬Έμ
μ»΄νμΌ μλ¬κ° λ¬λ€.
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 Cat
{
//public Cat()
//{
// name = "";
// color = "";
//}
public Cat(string _name, string _color)
{
name = _name;
color = _color;
}
private string name; //ν΄λμ€ μμ μ μΈλ λ³μ : νλ
private string color; //ν΄λμ€ μμ μ μΈλ λ³μ : νλ
public void SetName(string newName)
{
name = newName;
}
public string GetName()
{
return name;
}
public void SetColor(string newColor)
{
color = newColor;
}
public string GetColor()
{
return color;
}
public void Meow() //λ©μλ
{
WriteLine($"{color} κ³ μμ΄ {name}κ° μΌμΉ!");
}
}
class MainApp
{
static void Main(string[] args)
{
int a = new int();
a = 3;
string lovemessage = new string(new char[] {'I',' ', 'l', 'o', 'v','e', ' ', 'y','o','u' });
WriteLine(a);
WriteLine(lovemessage);
Cat nabi = new Cat(); //nabi κ°μ²΄ μμ±
Cat neko = new Cat("neko","νμ"); //nabi κ°μ²΄ μμ±
nabi.Meow();
neko.Meow();
Cat kitty = new Cat(); //kitty κ°μ²΄ μμ±
kitty.SetColor("νμμ");
kitty.SetName("ν€ν°");
kitty.Meow();
WriteLine($"{kitty.GetName()}μ μκΉμ {kitty.GetColor()}μ΄λλλ€.");
Cat nero = new Cat(); //nero κ°μ²΄ μμ±
nero.SetColor("κ²μμ");
nero.SetName("λ€λ‘");
nero.Meow();
WriteLine($"{nero.GetName()}μ μκΉμ {nero.GetColor()}μ΄λλλ€.");
}
}
}
μμ κ°μ΄...
<μ»΄νμΌλ¬κ° κΈ°λ³Έ μμ±μλ₯Ό μ 곡νμ§ μλ κ²½μ°>
νλ‘κ·Έλλ¨Έκ° μμ±μλ₯Ό νλλΌλ μ§μ μ μνλ©΄ C# μ»΄νμΌλ¬λ 맀κ°λ³μ μλ κΈ°λ³Έ μμ±μλ₯Ό μ 곡νμ§ μλλ€.
μΌλ°νκ² μκ°ν νμ μλ€.
νλ‘κ·Έλλ¨Έκ° μμ±μλ₯Ό μμ±νλ€λ κ²μ κ°μ²΄κ° νΉμ ν μνλ‘ μ΄κΈ°νλκΈ°λ₯Ό μνλ€λ λ»μΈλ°,
κΈ°λ³Έ μμ±μλ κ·Έλ° μλμ μκ΄μμ΄ κ°μ²΄λ₯Ό μ΄κΈ°ννλ€.
C# μ»΄νμΌλ¬λ νλ‘κ·Έλλ¨Έμ μλμ λ€λ₯΄κ² λμνλ μ½λκ° μ 곡λλ κ²μ λ°©μ§νκ³ μ νλ κ²μΌ λΏμ΄λ€.
<μ’ λ£μ>
μμ±μμ μ΄λ¦μ ν΄λμ€μ μ΄λ¦κ³Ό κ°μλ λ°λ©΄,
μ’ λ£μμ μ΄λ¦μ ν΄λμ€ μ΄λ¦ μμ ~λ₯Ό λΆμΈ κΌ΄μ΄λ€.
맀κ°λ³μλ μκ³ , νμ μλ μ¬μ©νμ§ μλλ€.
μ’ λ£μλ CLRμ κ°λΉμ§ 컬λ ν°κ° κ°μ²΄κ° μλ©Έλλ μμ μ νλ¨ν΄μ μ’ λ£μλ₯Ό νΈμΆν΄μ€λ€.
κ·Έλ¬λ μ μλ κ°κΈμ μ¬μ©νμ§ μκΈ°λ₯Ό κΆνλ€.
CLRμ κ°λΉμ§ 컬λ ν°κ° μΈμ λμν μ§ μμΈ‘ν μ μκΈ° λλ¬Έμ΄λ€.
κ²λ€κ° μ’ λ£μλ₯Ό λͺ μμ μΌλ‘ ꡬννλ©΄ κ°λΉμ§ 컬λ ν°λ ν΄λμ€μ 쑱보λ₯Ό νκ³ μ¬λΌκ°
κ°μ²΄λ‘λΆν° μμλ°μ Finalize() λ©μλλ₯Ό νΈμΆνλ€.
κ·Έλ°λ° μ΄λ¬λ©΄ μμ© νλ‘κ·Έλ¨μ μ±λ₯ μ νλ₯Ό μ΄λν νλ₯ μ΄ λλ€.
μ’ λ£μλ₯Ό ꡬννμ§ λ§μμΌ ν κ°μ₯ μ€μν μ΄μ λ
CLRμ κ°λΉμ§ 컬λ ν°λ κ΅μ₯ν λλνκ² κ°μ²΄μ μλ©Έμ μ²λ¦¬ν μ μκΈ° λλ¬Έμ΄λ€.
μμ±μ μμ±μμκ², λ·μ²λ¦¬λ κ°λΉμ§ 컬λ ν°μκ² λ§‘κΈ°λ νΈμ΄ μ’λ€κ³ μ μλ κ°μ‘°νλ€.
μ¬μ€ κ°μ²΄μ μλ©Έμ λ΄λΉνλ κ°λΉμ§ 컬λ ν°κ° μΈμ λμν μ§λ λ©°λ리λ λͺ¨λ₯Έλ€.
μΉ΄λΉμ§ 컬λ ν°λ μ°λ κΈ°μ μμ΄ μ΄λ μ μ λμ΄μΌλ§ μΌμ νκΈ° λλ¬Έμ΄λ€.
λν νλ‘κ·Έλ¨μ΄ μ’ λ£νκΈ° μ μ λ°λμ κ°λΉμ§ 컬λ ν°κ° μ°λ κΈ°λ₯Ό μμ§νλ€λ 보μ₯ λΏ μλλΌ
μ΄λ€ κ°μ²΄λ₯Ό μ΄λ€ μμλ‘ μλ©Έμν¬μ§μ λν 보μ₯ λν μκΈ° λλ¬Έμ
μλ μμ μ½λ μΆλ ₯κ²°κ³Ό μ€ μλ©Έμκ° μ€ννλ WriteLine()μ νΈμΆ μμλ 맀 μ€ν μλ§λ€ λ¬λΌμ§ μ μλ€.
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 Cat
{
public Cat()
{
name = "";
color = "";
}
~Cat()
{
WriteLine($"μ κ°, {name}");
}
public Cat(string _name, string _color)
{
name = _name;
color = _color;
}
private string name; //ν΄λμ€ μμ μ μΈλ λ³μ : νλ
private string color; //ν΄λμ€ μμ μ μΈλ λ³μ : νλ
public void SetName(string newName)
{
name = newName;
}
public string GetName()
{
return name;
}
public void SetColor(string newColor)
{
color = newColor;
}
public string GetColor()
{
return color;
}
public void Meow() //λ©μλ
{
WriteLine($"{color} κ³ μμ΄ {name}κ° μΌμΉ!");
}
}
class MainApp
{
static void Main(string[] args)
{
int a = new int();
a = 3;
string lovemessage = new string(new char[] {'I',' ', 'l', 'o', 'v','e', ' ', 'y','o','u' });
WriteLine(a);
WriteLine(lovemessage);
Cat nabi = new Cat(); //nabi κ°μ²΄ μμ±
Cat neko = new Cat("neko","νμ"); //nabi κ°μ²΄ μμ±
nabi.Meow();
neko.Meow();
Cat kitty = new Cat(); //kitty κ°μ²΄ μμ±
kitty.SetColor("νμμ");
kitty.SetName("ν€ν°");
kitty.Meow();
WriteLine($"{kitty.GetName()}μ μκΉμ {kitty.GetColor()}μ΄λλλ€.");
Cat nero = new Cat(); //nero κ°μ²΄ μμ±
nero.SetColor("κ²μμ");
nero.SetName("λ€λ‘");
nero.Meow();
WriteLine($"{nero.GetName()}μ μκΉμ {nero.GetColor()}μ΄λλλ€.");
}
}
}
μΆλ ₯κ²°κ³Ό
3
I love you
κ³ μμ΄ κ° μΌμΉ!
νμ κ³ μμ΄ nekoκ° μΌμΉ!
νμμ κ³ μμ΄ ν€ν°κ° μΌμΉ!
ν€ν°μ μκΉμ νμμμ΄λλλ€.
κ²μμ κ³ μμ΄ λ€λ‘κ° μΌμΉ!
λ€λ‘μ μκΉμ κ²μμμ΄λλλ€.
μ κ°, λ€λ‘
μ κ°, ν€ν°
μ κ°, neko
μ κ°,
'C# > μ΄κ²μ΄ C#μ΄λ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
20. μμ 볡μ¬(Shallow Copy)μ κΉμ 볡μ¬(Deep Copy) (0) | 2022.02.10 |
---|---|
19. [static]μ μ νλμ μ μ λ©μλ (0) | 2022.02.10 |
17. C#μ ν΄λμ€ (0) | 2022.02.10 |
16. κ°, μ°Έμ‘°μ μν 맀κ°λ³μ μ λ¬, refμ outμ μ°¨μ΄ (0) | 2022.02.09 |
15. λ©μλλ‘ μ½λ κ°μΆλ¦¬κΈ° + μ¬κ·νΈμΆ ν¨μμ§κΈ°(νΌλ³΄λμΉ μμ ) (0) | 2022.02.09 |