2012-02-20 5 views
0

나는 FieldDesc 클래스가 있습니다.상속 된 클래스로드

public class FieldDesc { 
    public FieldDesc() { 
    } 
} 

는 또한 StandardHoursByCommunitySvcType라는 FieldDesc에서 상속 다른 클래스가 있습니다. 내 컨트롤에서

public class StandardHoursByCommunitySvcType: FieldDesc { 
    public StandardHoursByCommunitySvcType() { 
    } 
} 

내가 가진 -

FieldDesc aTable; 
aTable = new FieldDesc(); 

String TableName = "StandardHoursByCommunitySvcType"; 

내가 그 유형 StandardHoursByCommunitySvcType의 목적은 알고 aTable를 얻으려면 어떻게해야합니까? 당신이 사용할 수있는

+3

'StandardHoursByCommunitySvcType'의 대상이 아닙니다. 당신은'new FieldDesc()'로 선언했습니다. –

+1

'aTable = new StandardHoursByCommunitySvcType()'을 지정해야합니다. 그렇지 않다면 StandardHoursByCommunitySvcType – MarcinJuraszek

+0

으로 캐스팅 할 수 없습니다. 코드 예제에서 'aTable'은 그 자체로 구성되어 있기 때문에 항상 'FieldDesc'유형입니다. 여기에서 달성하려는 목표는 무엇입니까? – Bernard

답변

0

당신은 선언하려는 경우 두 클래스가있는 경우

public class FieldDesc 
{ 
    public FieldDesc() 
    { 
    } 

    public void A() 
    { 
    } 

    public virtual void V() 
    { 
     Console.WriteLine("V from FieldDesc"); 
    } 
} 

public class StandardHoursByCommunitySvcType : FieldDesc 
{ 
    public StandardHoursByCommunitySvcType() 
    { 
    } 

    public void B() 
    { 
    } 

    public overrides void V() 
    { 
     Console.WriteLine("V from StandardHoursByCommunitySvcType"); 
    } 
} 

FieldDesc fd = new StandardHoursByCommunitySvcType(); 
StandardHoursByCommunitySvcType svc = new StandardHoursByCommunitySvcType(); 

fd.A(); // OK 
fd.B(); // Fails (does not compile) 
((StandardHoursByCommunitySvcType)fd).B(); // OK 
fd.V(); // OK, prints "V from StandardHoursByCommunitySvcType" 

svc.A(); // OK 
svc.B(); // OK 
svc.V(); // OK, prints "V from StandardHoursByCommunitySvcType" 

파생 클래스는 기본 클래스의 대입 호환성을하다 할 수있다; 그러나 기본 클래스로 입력 된 변수를 통해 액세스하면 기본 클래스의 멤버 만 표시됩니다.

0

if(someObject is StandardHoursByCommunitySvcType) 
    { 
     //it means is is object of StandardHoursByCommunitySvcType type 
    } 
+0

나는 모든 입력을 주셔서 감사하지만 자식 클래스에 특정한 개체를 선언하고 싶다면 해당 자식 클래스를 인스턴스화 할 수 있습니다. 그 명세를 컨트롤에서 추상화하려고합니다. 사용할 자식 클래스를 지정하는 TableName을 사용하는 방법이 있습니까? –

2

귀하의 질문이 명확하지이를 찾을 수 연산자입니다. aTable을 StandardHoursByCommunitySvcType으로 선언하려고 시도 했습니까, 아니면 이것이 선언 된 것으로 판별하려고합니까? StandardHoursByCommunitySvcType이 FieldDesc

에서 상속으로

FieldDesc aTable; 
aTable = new StandardHoursByCommunitySvcType(); 

그만큼 일 것이다 당신이 유형을 결정하려는 경우 :

if(aTable is StandardHoursByCommunitySvcType) 
{ 
    //Do something 
}