μλ°μμ ν΄λμ€μ λ©€λ² λ³μμ μ κ·Ό(κ°μ getνκ±°λ setν λ)ν λ getterμ setterλ₯Ό λ§λ λ€. μ΄λ κ² νλ μ΄μ λ λ°μ΄ν° μλμ±κ³Ό μΊ‘μνλ₯Ό μν΄μμ΄λ€. μΌλ°μ μΌλ‘ public λ©μλ μμ private νλκ° μ μΈλλ€.
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
νλ‘νΌν°
C#μμλ λμΌνκ² getterμ setter λ©μλλ₯Ό ꡬνν μ μλ€.
νμ§λ§ C#μμλ νλ‘νΌν°λΌλ κ²μ μ 곡νλ€. νλ‘νΌν°λ νλ‘νΌν°λ λ©μλκ° μλλΌ νλμ κ°μ ννλ‘ μ¬μ©λλ©°, λ΄λΆμ μΌλ‘ μλ μμ±λ getterμ setter λ©μλλ₯Ό κ°μ§κ³ μλ€.
public class Person {
private string name;
public string Name {
get { return name; }
set { name = value; }
}
}
get, setμ λ©μλλ‘ κ΅¬ννμ λλ κ°μ λΆλ¬μ¬ λλ .getName(), κ°μ μ€μ ν λλ .setName() λ‘ νμ§λ§, νλ‘νΌν°λ₯Ό μ¬μ©νλ©΄ κ°μ λΆλ¬μ¬ λλ μ€μ ν λλ λ λ€ .NameμΌλ‘ λΆλ¬μ¬ μ μλ€.
μλ ꡬν νλ‘νΌν°
μλ ꡬν νλ‘νΌν°λ‘ μ°λ¦¬λ ν¨μ¬ λ κ°λ¨νκ² μ½λλ₯Ό 지 μλ μλ€. μλ ꡬν νλ‘νΌν°λ λͺΈμ²΄λ λ€ μμ±νμ§ μκ³ λ νλ‘νΌν°λ₯Ό μ μν μ μκ² ν΄μ€λ€.
public class Person {
public string Name { get; set; }
}
μ΄λ κ² ν μ€λ‘ κΉλνκ² λνλΌ μ μλ€. νλλ₯Ό μ μΈν νμλ μκ³ , κ·Έλ₯ get; set;λ§ μ μΈν΄μ£Όλ©΄ λλ€.
μ΄κΈ°νλ μνν μ μλ€.
public class Person {
public string Name { get; set; } = "Unknown";
}
μ무κ²λ setνμ§ μμ μνμμ .NameμΌλ‘ λΆλ¬μ¨λ€λ©΄, κΈ°λ³Έκ°μΈ Unknownμ΄ λμ¬ κ²μ΄λ€.
λ§μ½ κ°μ²΄λ₯Ό μμ±νκ³ λμ κ° νλλ₯Ό μ΄κΈ°ννκ³ μΆλ€λ©΄, μλμ κ°μ΄ μ΄κΈ°νλ₯Ό νκ³ μΆμ νλ‘νΌν°λ§ μ λ ₯νλ©΄ λλ€.
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class MainApp
{
static void Main(string[] args)
{
Person person = new Person()
{
Name = "John",
// Age = 30 μΌλ‘ μ΄κΈ°ν ν΄λ λκ³ , μν΄λ λκ³
};
}
}
μ½κΈ° μ μ© νλ‘νΌν°
μ½κΈ° μ μ©μΌλ‘ μ μΈνλ λ°©λ²λ λ§€μ° κ°λ¨νλ€. κ°μ λ³κ²½ν μ μκ² νλ κ²μ΄λ―λ‘ set λμ μ initμ μ μΌλ©΄ λλ€.
public class Person {
public string Name { get; init; }
}
μΈν°νμ΄μ€, μΆμ ν΄λμ€μμλ νλ‘νΌν° μ¬μ©νκΈ°
μΈν°νμ΄μ€μμλ νλ‘νΌν°λ₯Ό ꡬνν μ μλ€.
public interface IExampleInterface {
string Property1 { get; set; }
int Property2 { get; init; }
}
public class ExampleClass : IExampleInterface {
public string Property1 { get; set; }
public int Property2 { get; init; } = 42; // μ½κΈ° μ μ© νλ‘νΌν° μ΄κΈ°ν
}
μΆμν΄λμ€μμλ νλ‘νΌν°λ₯Ό ꡬνν μ μλ€. νλ‘νΌν°λ ꡬνμ΄ μκ³ μ μΈλ§ λμ΄μκ² νκ³ μΆμΌλ©΄, abstractμ λΆμ¬μ μΆμ νλ‘νΌν°λ₯Ό λ§λ€λ©΄ λλ€. μΆμ νλ‘νΌν°λ νμ ν΄λμ€μμ λ°λμ ꡬνλμ΄μΌ νλ€.
public abstract class Vehicle {
// μΆμ νλ‘νΌν°
public abstract string Type { get; }
// μΌλ° νλ‘νΌν°
public string Model { get; set; }
// μΆμ λ©μλ
public abstract void Start();
// μΌλ° λ©μλ
public void DisplayInfo() {
Console.WriteLine($"Type: {Type}, Model: {Model}");
}
}
public class Car : Vehicle {
// μΆμ νλ‘νΌν° ꡬν
public override string Type => "Car";
// μΆμ λ©μλ ꡬν
public override void Start() {
Console.WriteLine("Car is starting.");
}
}
class Program {
static void Main(string[] args) {
Car car = new Car();
car.Model = "BMW";
car.Start();
car.DisplayInfo();
}
}
/*Console:
Car is starting.
Type: Car, Model: BMW
*/
'π₯οΈ > C#' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μ΄κ²μ΄ C#μ΄λ€] Chapter09: μ°μ΅λ¬Έμ (0) | 2023.08.20 |
---|---|
[C#] λ μ½λμ νλ‘νΌν° (0) | 2023.08.18 |
[μ΄κ²μ΄ C#μ΄λ€] Chapter08: μ°μ΅λ¬Έμ (0) | 2023.08.18 |
[C#] μΈν°νμ΄μ€μ μΆμν΄λμ€ (0) | 2023.08.18 |
[μ΄κ²μ΄ C#μ΄λ€] Chapter07: μ°μ΅λ¬Έμ (0) | 2023.08.17 |