여기에 OO 문제가 있습니다. 공통 속성과 특정 속성을 가진 두 개의 세션이 있습니다. 기본 클래스를 만들고 모든 공통 속성/메서드를 캡슐화했습니다. 두 개의 세션에는 Ranges라는 공통 유형이 있으며, 세션에는 공통 속성과 특정 속성이 있습니다. 그래서 나는이 경우에 슈퍼 타입으로 프로그램하고 런타임에 인스턴스를 만들 수 있다고 생각했다.C에서 슈퍼 유형 프로그래밍 #
public class Level
{
private readonly Ranges _range;
public Level(Ranges range)
{
_range = range;
}
public Ranges Range
{
get { return _range; }
}
public void CommonMethod()
{
throw new NotImplementedException();
}
public int CommonProperty;
}
public class ALevel : Level
{
public ALevel()
: base(new ARange())
{
}
public int ALevelProperty;
}
public class BLevel : Level
{
public BLevel()
: base(new BRange())
{
}
public int BLevelProperty;
}
public class Ranges
{
public int CommonRangeProperty;
}
public class ARange : Ranges
{
public int ARangeProperty;
public ARange()
{
}
}
public class BRange : Ranges
{
public int BRangeProperty;
}
public class ASession
{
public ASession()
{
Level = new ALevel();
}
public ALevel Level { get; set; }
}
public class BSession
{
public BSession()
{
Level = new BLevel();
}
public BLevel Level { get; set; }
}
세션 개체를 만들 때 특정 세션 범위 속성을 포함하지 않습니다. 기본 클래스의 속성에만 액세스 할 수 있습니다. aSession.Level.Range.CommonRangeProperty = 1; aSession의 특정 속성 aSession.Level.Range.ARangeProperty에 액세스 할 수 없습니다.
여기에 뭔가 잘못입니까?
public class Test
{
public static void Main(string[] args)
{
ASession aSession = new ASession();
aSession.Level.Range.CommonRangeProperty = 1;
//Not able to see ARangeProperty
}
}
와우! Ur 솔루션은 간단하면서도 강력했습니다. 고맙습니다! 하지만 코드 문제를 설명해 주시겠습니까? – sundar
@sundar 첫 번째 문장에서 "학급 수준이 범위의 범위를 설정합니다 (특정 ARange 또는 BRange가 아닙니다)"-이 시점이 명확한 지 설명하려고했습니다. –
하지만 Ranges는 여기에서 수퍼 유형이므로 파생 유형을 할당 할 수 있어야합니다. – sundar