1. ν΄λμ€μ κ°μ²΄, μΈμ€ν΄μ€λ μλ‘ μ΄λ€ μ μ΄ λ€λ₯Έκ°μ?
ν΄λμ€λ κ°μ²΄λ₯Ό λ§λ€κΈ° μν μ€κ³λ, κ°μ²΄λ ν΄λμ€λ₯Ό μ€μ²΄νν κ²μΌλ‘ μΈμ€ν΄μ€μ ν μ’
λ₯μ΄λ€.
μ) "μλμ°¨"λΌλ ν΄λμ€κ° μκ³ , μ΄ ν΄λμ€λ μλμ°¨μ μμ±(μμ, λΈλλ λ±)κ³Ό λμ(κ°μ, μ λ λ±)μ μ μνλ€. μ΄ ν΄λμ€λ₯Ό κΈ°λ°μΌλ‘ "λΈλ£¨ ν μν μλμ°¨"μ "λ λ νλ μλμ°¨"μ κ°μ κ°μ²΄λ₯Ό μμ±ν μ μλ€. μ΄λ κ² μμ±λ κ°μ²΄λ€μ ν΄λμ€μ μΈμ€ν΄μ€μ΄λ©°, ν΄λμ€μ μ μλ μμ±κ³Ό λμμ κ°μ§κ² λλ€.
2. λ€μ μ½λμμ μ€λ₯λ₯Ό μ°Ύκ³ , μ€λ₯μ μμΈμ μ€λͺ νμΈμ.
class A
{
}
class B: A
{
}
class C
{
public static void Main()
{
A a = new A();
B b = new B();
A c = new B();
B d = new A();
}
}
- Bλ Aλ₯Ό μμλ°κ³ μλ ν΄λμ€μ΄λ€. μ¦, Aκ° λΆλͺ¨ ν΄λμ€, Bκ° μμ ν΄λμ€μ΄λ€.
- A a = new A(); : A ν΄λμ€μ κ°μ²΄ aλ₯Ό μμ±
- B b = new B(); : B ν΄λμ€μ κ°μ²΄ bλ₯Ό μμ±.
- A c = new B(); : B ν΄λμ€μ κ°μ²΄λ₯Ό A νμ λ³μμ ν λΉν¨. => λ€νμ±μ 보μ¬μ€. c λ³μλ B ν΄λμ€μ κ°μ²΄λ₯Ό μ°Έμ‘°ν μ μμ§λ§, λ³μ νμ μ Aμ΄λ€.
- B d = new A(); : A ν΄λμ€μ κ°μ²΄λ₯Ό B νμ λ³μμ ν λΉνλ €κ³ νλ©΄ μ»΄νμΌ μ€λ₯κ° λ°μνλ€. μλνλ©΄ Aλ Bμ λΆλͺ¨ν΄λμ€μ΄κΈ° λλ¬Έμ΄λ€.(λ μμμ)
3. this ν€μλμ base ν€μλμ λν΄ μ€λͺ νμΈμ.
this ν€μλ: μκΈ° μμ μ κ°λ¦¬ν€λ κ²
base ν€μλ: λΆλͺ¨ ν΄λμ€μ μμ±μλ λ©€λ²μ μ κ·Όν λ λΆλͺ¨ ν΄λμ€λ₯Ό κ°λ¦¬ν€λ κ²
4. ꡬ쑰체μ λν λ€μ μ€λͺ
μ€ νλ¦° κ²μ λͺ¨λ μ°ΎμΌμΈμ.
β struct ν€μλλ₯Ό μ΄μ©νμ¬ μ μΈνλ€.
①볡μ¬ν λ μμ 볡μ¬κ° μ΄λ£¨μ΄μ§λ€.
β’ μ°Έμ‘° νμμ΄λ€.
β£ λ©μλλ₯Ό κ°μ§ μ μλ€.
- 2λ²: structλ κΉμ 볡μ¬κ° μ΄λ£¨μ΄μ§λ€.
- 3λ²: structλ κ° νμ, classλ μ°Έμ‘° νμ
5. λ€μ μ½λλ₯Ό μ»΄νμΌ λ° μ€νμ΄ κ°λ₯νλλ‘ μμ νμΈμ.
using System;
namespace ReadonlyMethod
{
struct ACSetting
{
public double currentInCelsius;
public double target;
public readonly double GetFahrenheit()
{
target = currentInCelsius * 1.8 + 32;
return target;
}
}
class MainApp
{
static void Main(string[] args)
{
ACSetting acs;
acs.currentInCelsius = 25;
acs.target = 25;
Console.WriteLine($"{acs.GetFahrenheit()}");
Console.WriteLine($"{acs.target}");
}
}
}
μ μ½λλ μ½κΈ°μ μ©μΈ targetμλ ν λΉν μ μλ€λ μλ¬κ° λλ€. targetμ 건λλ¦¬μ§ μκ³ λ°λ‘ Fahrenheitλ‘ λ³νν κ²μ 리ν΄νλ©΄ λλ€.
μμ λ μ½λ
using System;
namespace ReadonlyMethod
{
struct ACSetting
{
public double currentInCelsius;
public double target;
public readonly double GetFahrenheit()
{
return currentInCelsius * 1.8 + 32;
}
}
class MainApp
{
static void Main(string[] args)
{
ACSetting acs;
acs.currentInCelsius = 25;
acs.target = 25;
Console.WriteLine($"{acs.GetFahrenheit()}");
Console.WriteLine($"{acs.target}");
}
}
}
6. λ€νμ±μ 무μμ΄λ©°, μ€λ²λΌμ΄λ©κ³Ό λ¬΄μ¨ κ΄κ³κ° μλμ§ μ€λͺ νμΈμ.
λ€νμ±μ νλμ κΈ°λ₯μ λ€μν λ°©μμΌλ‘ μ¬μ©νλ κ²μ λ§νλ€. μ€λ²λΌμ΄λ©μ λ€νμ±μ ν ννλ‘, νμ ν΄λμ€μμ μμ ν΄λμ€μ λ©μλλ₯Ό μ΄λ¦κ³Ό 맀κ°λ³μ νμ κ³Ό κ°μλ κ°μ§λ§ λ€λ₯Έ λμμ νλλ‘ νλ κ²μ΄λ€.
7. λ€μ μ½λμμ switch μμ μ κ±°νκ³ switch λ¬ΈμΌλ‘ λμΌν κΈ°λ₯μ μμ±νμΈμ.
private static double GetDiscountRate(object client)
{
return client switch
{
("νμ", int n) when n < 18 => 0.2, //νμ & 18μΈ λ―Έλ§
("νμ", _) => 0.1, //νμ & 18μΈ μ΄μ
("μΌλ°", int n) when n < 18 => 0.1, //μΌλ° & 18μΈ λ―Έλ§
("μΌλ°", _) => 0.05, //μΌλ° & 18μΈ μ΄μ
_ => 0,
};
}
μμ λ μ½λ
private static double GetDiscountRate(object client)
{
switch (client)
{
case ("νμ", int n) when n < 18:
return 0.2; // νμ & 18μΈ λ―Έλ§
case ("νμ", _):
return 0.1; // νμ & 18μΈ μ΄μ
case ("μΌλ°", int n) when n < 18:
return 0.1; // μΌλ° & 18μΈ λ―Έλ§
case ("μΌλ°", _):
return 0.05; // μΌλ° & 18μΈ μ΄μ
default:
return 0;
}
}
'π₯οΈ > C#' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μ΄κ²μ΄ C#μ΄λ€] Chapter08: μ°μ΅λ¬Έμ (0) | 2023.08.18 |
---|---|
[C#] μΈν°νμ΄μ€μ μΆμν΄λμ€ (0) | 2023.08.18 |
[C#] κΉμλ³΅μ¬ μμ볡μ¬, readonly, ꡬ쑰체, νν (0) | 2023.08.17 |
[C#] λΆν ν΄λμ€, νμ₯ λ©μλ (0) | 2023.08.17 |
[C#] μμ, λ€νμ±, μ€λ²λΌμ΄λ© (0) | 2023.08.17 |