μΆμν΄λμ€λ ν΄λμ€μ²λΌ ꡬνλ νλ‘νΌν°λ₯Ό κ°μ§ μ μλ ννΈ,
μΈν°νμ΄μ€μ²λΌ ꡬνλμ§ μμ νλ‘νΌν°λ κ°μ§ μ μλ€.(μΆμ νλ‘νΌν°)
μΆμ νλ‘νΌν° μμ μΈν°νμ΄μ€μ νλ‘νΌν°μ λ€λ₯Ό κ²μ΄ μλ€.(νμ ν΄λμ€κ° ν΄λΉ νλ‘νΌν°λ₯Ό ꡬννλλ‘ κ°μ )
μΆμ ν΄λμ€μ μΆμ νλ‘νΌν°λ μΈν°νμ΄μ€μ²λΌ ꡬνμ λΉμλμ κ² λ§μΌλ‘λ λ§λ€ μ μλ€.(C# μ»΄νμΌλ¬κ° μλ ꡬν νλ‘νΌν°λ‘ κ°μ£Όνκ³ κ΅¬νμ μλμΌλ‘ μ±μ λ£μΌλ―λ‘...
μΆμ νλ‘νΌν°λ λ€μκ³Ό κ°μ΄ abstract νμ μλ₯Ό μ΄μ©ν΄ μ μΈνλ€!
abstract class μΆμν΄λμ€μ΄λ¦
{
abstract λ°μ΄ν°νμ νλ‘νΌν°μ΄λ¦
{
get;
set;
}
}
μΆμ νλ‘νΌν°λ₯Ό κ°λ μΆμ ν΄λμ€μ μ΄λ₯Ό μμνλ νμ ν΄λμ€ μμ μ½λ
abstract class Product
{
private static int serial = 0;
public string SerialID //μΆμ ν΄λμ€λ ꡬνμ κ°μ§ νλ‘νΌν°μ
{
get{ return String.Format("{0:d5}", serial++); }
}
abstract public DateTime ProductDate //ꡬνμ΄ μλ μΆμ νλ‘νΌν° λͺ¨λλ₯Ό κ°μ§ μ μλ€
{
get;
set;
}
}
class MyProduct : Product
{
public override DateTime ProductDate//νμν΄λμ€λ κΈ°λ°μΆμν΄λμ€μ λͺ¨λ μΆμλ©μλνλ‘νΌν° μ¬μ μν΄μΌν¨
{
get;
set;
}
}
<μμ >
using System;
namespace PropertiesInAbstractClass
{
abstract class Product
{
private static int serial = 0;
public string SerialID
{
get { return String.Format("{0:d5}",serial++); }
}
abstract public DateTime ProductDate
{
get;
set;
}
}
class MyProduct : Product
{
public override DateTime ProductDate
{
get;
set;
}
}
class MainApp
{
static void Main(string[] args)
{
Product product_1 = new MyProduct()
{
ProductDate = new DateTime(2022,02,24)
};
Console.WriteLine($"Product:{product_1.SerialID}, Product Date : {product_1.ProductDate}");
Product product_2 = new MyProduct()
{
ProductDate = new DateTime(2022, 02, 25)
};
Console.WriteLine($"Product:{product_2.SerialID}, Product Date : {product_2.ProductDate}");
}
}
}
<μΆλ ₯>
Product:00000, Product Date : 2022-02-24 μ€μ 12:00:00
Product:00001, Product Date : 2022-02-25 μ€μ 12:00:00
'C# > μ΄κ²μ΄ C#μ΄λ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
47. λ°°μ΄μ°λ μ΄μ , λ°°μ΄ μ΄κΈ°ν λ°©λ² 3κ°μ§ (0) | 2022.02.24 |
---|---|
46. μ±ν°9 μ°μ΅λ¬Έμ (0) | 2022.02.24 |
44. μΈν°νμ΄μ€μ νλ‘νΌν° (0) | 2022.02.23 |
43. 무λͺ νμ (0) | 2022.02.23 |
42. [ν΄λμ€/λ μ½λ]λ‘ λΆλ³κ°μ²΄ ꡬννκΈ° (0) | 2022.02.23 |