본문 바로가기
CSharp/CSharp 문법

인터페이스와 추상클래스

by MonoSoft 2025. 2. 14.
728x90
반응형

 

 

 

 

namespace Property2

{

//.Net 9.0

 

 

internal class Program

{

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;

}

}

 

 

record RTransaction

{

public string From { get; set; }

public string To { get; set; }

public int Amount { get; set; }

public override string ToString()

{

return $"{From,-10}->{To,-10}:${Amount}";

}

}

 

class CTransaction

{

public string From { get; init; }

public string To { get; init; }

public int Amount { get; init; }

 

public override string ToString()

{

return $"{From,-10}->{To,-10}:${Amount}";

}

}

 

record BTransaction

{

public string From { get; init; }

public string To { get; init; }

public int Amount { get; init; }

 

public override string ToString()

{

return $"{From,-10}->{To,-10}:${Amount}";

}

}

 

 

 

 

class BirthdayInfo

{

public required string Name { get; set; }

public required DateTime Birthday { get; set; }

 

public int Age

{

get

{

return new DateTime(DateTime.Now.Subtract(Birthday).Ticks).Year;

}

}

}

 

class Transaction

{

public string From { get; init; }

public string To { get; init; }

public int Amount { get; init; }

 

public override string ToString()

{

return $"{From,-10} -> {To,-10} : ${Amount}";

}

}

 

static void Main(string[] args)

{

/*

Transaction tr1 = new Transaction { From = "Alice", To = "Bob", Amount = 100 };

 

Transaction tr2 = new Transaction { From = "Bob", To = "Charlie", Amount = 50 };

 

Transaction tr3 = new Transaction { From = "Charlie", To = "Alice", Amount = 55 };

 

Console.WriteLine(tr1);

Console.WriteLine(tr2);

Console.WriteLine(tr3);

 

 

BirthdayInfo birth = new BirthdayInfo()

{

Name = "서현", Birthday = new DateTime(1991, 6, 28)

};

 

Console.WriteLine("Nmae : {0}", birth.Name);

Console.WriteLine("Nmae : {0}", birth.Birthday.ToShortTimeString());

Console.WriteLine("Nmae : {0}", birth.Age);

 

 

RTransaction tr1 = new RTransaction

{

From = "Alice",

To = "Bob",

Amount = 100

};

 

RTransaction tr2 = new RTransaction

{

From = "Alice",

To = "Charlie",

Amount =200

};

 

Console.WriteLine(tr1);

Console.WriteLine(tr2);

 

RTransaction tr1 = new RTransaction

{

From = "Alice",

To = "Bob",

Amount = 100

};

 

RTransaction tr2 = tr1 with { To = "Charlie" };

RTransaction tr3 = tr2 with { From = "Dave",Amount=30 };

 

 

Console.WriteLine(tr1);

Console.WriteLine(tr2);

Console.WriteLine(tr3);

*/

 

/*

CTransaction trA = new CTransaction { From = "Alice", To = "Bob", Amount = 100 };

CTransaction trB = new CTransaction { From = "Alice", To = "Bob", Amount = 100 };

 

Console.WriteLine(trA);

Console.WriteLine(trB);

Console.WriteLine($"trA equals to trB : {trA.Equals(trB)}");

 

BTransaction tr1 = new BTransaction { From = "Alice", To = "Bob", Amount = 100 };

BTransaction tr2 = new BTransaction { From = "Alice", To = "Bob", Amount = 100 };

 

Console.WriteLine(tr1);

Console.WriteLine(tr2);

Console.WriteLine($"trA equals to trB : {tr1.Equals(tr2)}");

*/

 

/*

var a = new { Name = "홍길동", Age = 123 };

Console.WriteLine($"{a.Name}, Age : {a.Age}");

 

var b = new { Subject = "수학", Scores = new int[] { 1, 2, 3, 4, 5 } };

 

Console.WriteLine($"Subject:{b.Subject}, Scores:");

foreach (var score in b.Scores)

Console.WriteLine($"{score}");

 

Console.WriteLine();

*/

 

Product product_1 = new MyProduct()

{

ProductDate = new DateTime(2023, 1, 10)

};

Console.WriteLine("Product:{0},Product Date : {1}", product_1.SerialID, product_1.ProductDate);

 

Product product_2 = new MyProduct()

{

ProductDate = new DateTime(2023, 2, 3)

};

Console.WriteLine("Product:{0},Product Date : {1}", product_2.SerialID, product_2.ProductDate);

}

}

}

 

 

 

728x90
반응형

'CSharp > CSharp 문법' 카테고리의 다른 글

프로퍼티 1  (0) 2025.01.30
추상클래스  (0) 2024.12.12
클래스  (0) 2024.12.03
메소드 ( Method)  (0) 2024.08.16
코드의 흐림제어  (0) 2024.08.01

댓글