인터페이스 5

[C#] 추상클래스와 인터페이스

[C#] 추상클래스와 인터페이스추상클래스는 인터페이스와 달리 구현을 가질 수 있다.그러나 추상클래스는 인스턴스를 생성할 수는 없다.또한 추상클래스는 추상 메소드를 가질 수 있어, 인터페이스의 역할도할 수 있다.추상 클래스 구현abstract class AbstractBase { protected void PrivateMethod() { Console.WriteLine("AbstractBase.PrivateMethod()"); } public void PublicMethod() { Console.WriteLine("AbstractBase.PublicMethod()"); } public abstract void AbstractMethod(); }상속받은 클래스에서만 접근가능한 PrivateMethod어디서든..

아카이빙/C# 2018.06.18

[C#] 여러 인터페이스의 멤버를 명시적 구현

[C#] 여러 인터페이스의 멤버를 명시적 구현명시적 인터페이스 구현을 통해 이름이 같은 각 인터페이스 멤소드를 별도로 구현할 수있다.아래 예시는 섭씨온도와 화씨온도를 구하는 예시이며, 두 인터페이스 모두 GetTemperature()라는 공통 메소드를 가진다.ICelsius, IFahrenheit 인터페이스interface ICelsius { float GetTemperature(); } ​ interface IFahrenheit { float GetTemperature(); }ICelsius, IFahrenheit을 상속받은 Temperature 클래스class Temperature : ICelsius, IFahrenheit { float temperature; ​ public Temperature(f..

아카이빙/C# 2018.06.18

[C#] 인터페이스 다중 상속

[C#] 인터페이스 다중 상속C#에서 클래스는 여러 클래스를 동시에 상속할 수 없다.대신 인터페이스는 다중 상속이 가능하다.IUpgradable, ISellable 인터페이스interface IUpgradable { void Upgrade(); } ​ interface ISellable { void Sell(); }IUpgradable, ISellable을 상속받은 Monster 클래스//Monster는 업그레이드도 할 수 있고, 팔 수도 있다. class Monster : IUpgradable, ISellable { public int Level {get; set;} public int Price {get; set;} ​ public void Upgrade() { Console.WriteLine("Up..

아카이빙/C# 2018.06.18

[C#] 인터페이스를 상속하는 인터페이스

[C#] 인터페이스를 상속하는 인터페이스인터페이스는 클래스만 상속받을 수 있는 게 아니다.기존의 인터페이스에 새로운 기능을 추가한 인터페이스를 만들고 싶을 때 인터페이스를 상속하는 인터페이스를 만들면 된다.ILogger 인터페이스interface ILogger { void WriteLog(string msg); }ILogger를 상속받은 인터페이스interface IFormattableLogger : ILogger { void WriteLog(string msg, params Object[] args); }보이지는 않지만 void WriteLog(string msg) 메소드를 포함하고 있다.ILogger를 상속받은 인터페이스를 상속받은 클래스class ConsoleLogger : IFormattableL..

아카이빙/C# 2018.06.18

[C#] 인터페이스

[C#] 인터페이스인터페이스는 클래스 또는 구조체에서 구현할 수 있는 기능에 대한 정의를 포함한다.클래스나 구조체는 인터페이스를 상속받아 인터페이스의 기능을 정의해준다.인터페이스를 상속받은 클래스나 구조체는 인터페이스의 모든 메소드를 구현해야 한다.IEquatable 인터페이스interface IEquatable { bool Equals(T obj); }인터페이스를 상속받은 클래스public class Car : IEquatable { public string Model {get; set;} public int Year {get; set;} ​ public bool Equals(Car car) { if(this.Model == car.Model && this.Year == car.Year){ return..

아카이빙/C# 2018.06.18