091
[C#] TQC+ 문제 CS_605: 본문
728x90
🔍C#: CS_605.
Staff 클래스를 상속받고 인터페이스 Ilogin과 Ilogout을 구현하시오.
1. 문제 정답
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
namespace CSD06
{
class CSD06
{
static void Main(string[] args)
{
try
{
string str = Console.ReadLine();
string[] data = str.Split(' ');
if (data.Length != 3) { throw new Exception(); }
Manager mm = new Manager(data[0].ToString(), data[1].ToString(), data[2].ToString());
mm.login();
mm.logout();
Console.Write("{0} Login {1},Logout {2}",mm.name,mm.logintime.ToString("yyyy/MM/dd HH:mm:ss"),mm.logouttime.ToString("yyyy/MM/dd HH:mm:ss"));
}
catch
{
Console.Write("error");
}
}
}
public interface Ilogin
{
bool login();
}
public interface Ilogout
{
bool logout();
}
public class Staff
{
public string name = "";
public DateTime logintime ;
public DateTime logouttime;
public string workdate;
public string worktime;
}
public class Manager : Staff, Ilogin, Ilogout {
public Manager(string name, string workdate, string worktime){
this.name = name;
this.workdate = workdate;
this.worktime = worktime;
}
public bool login(){
string date = workdate + " " + worktime;
logintime = DateTime.Parse(date);
return true;
}
public bool logout(){
logouttime = logintime.AddSeconds(2);
return true;
}
}
}
2. 정리
- 사실 인터페이스 내부 함수가 bool 출력값을 뱉지만 실질적으로는 사용하지 않고 있어서 변경만 해도 됩니다.
- C#에서는 클래스의 다중상속이 되지 않지만, 인터페이스를 다중 구현하는 것은 가능합니다. 인터페이스를 구현할 때는 내부에 선언되어있는 모든 멤버를 전부 다 구현해줘야 오류가 나지 않습니다.
Q. 추상 클래스와 인터페이스의 차이가 무엇인가요?
A. 추상 클래스의 경우, 추상 메서드로 선언된 메서드 만을 의무적으로 구현하면 되지만, 인터페이스의 경우 전부 다 구현해야합니다. 하지만 상속의 경우에는 단일 상속만 가능하지만, 인터페이스의 경우 다중 구현이 가능합니다.
728x90
'Programming Language > C#' 카테고리의 다른 글
[C#] TQC+ 문제 CS_607: (0) | 2025.06.15 |
---|---|
[C#] TQC+ 문제 CS_606: (0) | 2025.06.15 |
[C#] TQC+ 문제 CS_604: (0) | 2025.06.15 |
[C#] TQC+ 문제 CS_603: (0) | 2025.05.27 |
[C#] TQC+ 문제 CS_602: (0) | 2025.05.27 |