인터페이스에는 필드들을 포함할 수 없죠. 그래서 아래와 같은 코드는 컴파일시 오류가 발생되죠.
public interface IAnimal
{
public string SpeciesName;
}
하지만, 인터페이스에서는 프로퍼티(속성)를 포함할 수 있답니다. 컴파일시 문제가 없겠죠.
string SpeciesName { get; set; }
이제 아래와 같이 두개의 인터페이스에 프로퍼티를 각각 기술하고, 이를 다중 상속하여 기술하는 방법을 알아보죠.
Dog 클래스는 Animal 와 LicensedPet 두개의 인터페이스를 상속하고, 인터페이스안에 기술한 프로퍼티를 아래와 같이 기술합니다. 살펴볼 것은 구분하기 위해서 프로퍼티 작성시, "인터페이스명.프로퍼티" 순으로 기술합니다.
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 = "재우니블로그-343"; } posted by 심재운(shimpark@gmail.com) http://cafe.daum.net/aspdotnet/6Hvn/310
protected void Button1_Click(object sender, EventArgs e)
Dog Spot = new Dog();
Spot.SpeciesName = "한국견";
Spot.PetName = "진돗개";
Spot.LicenseID = "재우니블로그-343";
posted by 심재운(shimpark@gmail.com)
http://cafe.daum.net/aspdotnet/6Hvn/310
* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.