인터페이스에는 필드들을 포함할 수 없죠. 그래서 아래와 같은 코드는 컴파일시 오류가 발생되죠.
public interface IAnimal
{public string SpeciesName;
}
하지만, 인터페이스에서는 프로퍼티(속성)를 포함할 수 있답니다. 컴파일시 문제가 없겠죠.
public interface IAnimal
{string SpeciesName { get; set; }
}
이제 아래와 같이 두개의 인터페이스에 프로퍼티를 각각 기술하고, 이를 다중 상속하여 기술하는 방법을 알아보죠.
Dog 클래스는 Animal 와 LicensedPet 두개의 인터페이스를 상속하고, 인터페이스안에 기술한 프로퍼티를 아래와 같이
기술합니다. 살펴볼 것은 구분하기 위해서 프로퍼티 작성시, "인터페이스명.프로퍼티" 순으로 기술합니다.
public interface IAnimal
{string SpeciesName { get; set; }
}
public interface ILicensedPet
{string LicenceID { get; set; }
string PetName { get; set; }
}
public class Dog : IAnimal, ILicensedPet
{public string SpeciesName { get; set; }
public string PetName { get; set; }
public string LicenseID { get; set; }
string IAnimal.SpeciesName
{get { return this.SpeciesName; }
set { this.SpeciesName = value; }
}
string ILicensedPet.PetName
{get{return this.PetName;}
set{this.PetName = value;}
}
string ILicensedPet.LicenceID
{get { return this.LicenseID; }
set { this.LicenseID = value; }
}
}
//위의 Dog 클래스를 인스턴스 하여 각 속성에 값을 할당하는 모습을 보여주고 있습니다.
protected void Button1_Click(object sender, EventArgs e)
{ Dog Spot = new Dog();
Spot.SpeciesName = "한국견"; Spot.PetName = "진돗개"; Spot.LicenseID = "korea-343"; }
감사합니다.
다음검색