2016-06-24 18 views
0

파생 클래스에서 기본 클래스의 필드 값을 변경할 수 있는지 질문이 있습니다. 내 경우, 나는 창문이 두 클래스 클래스를 가지고 RichTextBox 형태로, 그리고 난 RichTextBox 지우기 클래스를 파생 사용 싶습니다.파생 클래스에서 기본 클래스 필드 값 변경

초기화 RichTextBox :

 this.rtfCode.Location = new System.Drawing.Point(45, 26); 
     this.rtfCode.Name = "rtfCode"; 
     this.rtfCode.ShowSelectionMargin = true; 
     this.rtfCode.Size = new System.Drawing.Size(100, 96); 
     this.rtfCode.TabIndex = 1; 
     this.rtfCode.Text = "some text"; 

기본 클래스 :

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Console.WriteLine(this.rtfCode.Text); 
     DerivedClass f = new DerivedClass(); 
     Console.WriteLine(f.rtfCode.Text); 
    } 
} 

내 파생 클래스

class DerivedClass:Program 
{ 
    public DerivedClass() 
    { 
     base.rtfCode.Clear(); 
    } 
} 

을 내가 프로그램을 실행하고 buttonRichTextBox에 난 아직 텍스트를 보려면 누릅니다.

답변

1
Program a = new Program(); // a is an instance of Program 
Console.WriteLine(a.rtfCode.Text); 
DerivedClass f = new DerivedClass();// f is an instance of DerivedClass, which has nothing to do with a 
Console.WriteLine(a.rtfCode.Text); 

af는 동일한 인스턴스하지 않습니다. DerivedClass ...가 Program에서 파생되었다는 사실은 이것에 아무런 변화도주지 않습니다.

당신은

Console.WriteLine(f.rtfCode.Text); 
에 의해 마지막 줄을 교체해야