λ³μλ λ³νλ λ°μ΄ν°λ₯Ό λ΄μ§λ§,
μμλ λ³μΉ μλ λ°μ΄ν°λ₯Ό λ΄λλ€.
μμλ const ν€μλλ₯Ό μ΄μ©ν΄μ μ μΈνλ€.
μ»΄νμΌλ¬λ μμμ μ§μ λ κ°μ μ€ννμΌ μμ κΈ°λ‘ν΄λλ€.
μμλ νλ‘κ·Έλ¨μ΄ μ€νλκΈ° μ λΆν° μ΄λ―Έ μ ν΄μ Έ μλ€.
κ·Έλ¦¬κ³ νλ‘κ·Έλ¨ μ€ν μ€μλ μ λ κ·Έ κ°μ λ°κΏ μ μλ€.
λ³μλ μμ λ‘κ² κ°μ λ³κ²½ν μ μλ€.
μ½κΈ° μ μ© νλλ μμμ λ³μ κ·Έ μ€κ° μ΄λκ°μ μλ€.
μ½κΈ° μ μ© νλλ, μ½κΈ°λ§ κ°λ₯ν νλλ₯Ό λ§νλ€.
μ¦, ν΄λμ€λ ꡬ쑰체μ λ©€λ²λ‘λ§ μ‘΄μ¬ν μ μμΌλ©°
μμ±μ μμμ ν λ² κ°μ μ§μ νλ©΄,
κ·Έ νλ‘λ κ°μ λ³κ²½ν μ μλ κ²μ΄ νΉμ§μ΄λ€.
μ»΄νμΌ μλ¬ :
μ€λ₯ CS0191 μ½κΈ° μ μ© νλμλ ν λΉν μ μμ΅λλ€.
λ¨, νλκ° μ μλ νμμ μμ±μ λλ μ΄κΈ°κ° μ μ© setterλ λ³μ μ΄λμ
λΌμ΄μ μμλ μμΈμ
λλ€.
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 Overriding
{
class Configuration
{
private readonly int min;
private readonly int max;
public Configuration(int v1, int v2)
{
min = v1;
max = v2;
}
public void ChangeMax(int newMax)
{
max = newMax; //μ¬κΈ°μ μ»΄νμΌ μλ¬κ° λ°μνλ€..
}
}
class MainApp
{
static void Main(string[] args)
{
Configuration c = new Configuration(100,10);
}
}
}
μλ μ½λμμμ²λΌ μ½κΈ° μ μ© νλλ μμ±μ μμμλ§ μ΄κΈ°νκ° κ°λ₯νλ€.
λ§μ½ μμ±μκ° μλ λ€λ₯Έ λ©μλμμ μ½κΈ° μ μ© νλλ₯Ό μμ νλ €λ μλκ° λ°μνλ©΄ μ»΄νμΌ μλ¬κ° μΌμ΄λλ€.
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 Overriding
{
class Configuration
{
private readonly int min;
private readonly int max;
public Configuration(int v1, int v2)
{
min = v1;
max = v2;
}
}
class MainApp
{
static void Main(string[] args)
{
Configuration c = new Configuration(100,10);
}
}
}
'C# > μ΄κ²μ΄ C#μ΄λ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
29. λΆν ν΄λμ€ (0) | 2022.02.11 |
---|---|
28. μ€μ²© ν΄λμ€ (0) | 2022.02.11 |
26. μ€λ²λΌμ΄λ© λ΄μΈνκΈ° (0) | 2022.02.11 |
25. μ€λ²λΌμ΄λ©κ³Ό λ€νμ± virtual, override (0) | 2022.02.11 |
24. C# νμ λ³ν μ°μ°μ μν μ°μ°μ, isμ as (0) | 2022.02.11 |