作者chrishappy11 ()
看板C_Sharp
标题[问题] 一些继承的变数问题
时间Sun Sep 9 09:10:15 2012
对不起.
小弟是Visual C#新手
目前小弟有一些困难想请教各位高手
现在我有一个公开的类别A,它有变数m_A
public:
bool m_A=false;
然後我有个类别B,有变数m_B且它公开继承类别A
所以我写成
public class B: A
{
public bool m_B=false;
}
然後我在form1有宣告一个物件叫做object1_
那object1_物件我宣告它是B类别的物件
B object1_=new B();
我想在form1修改A与B类别内的变数
object1_.m_A=true;
object1_.m_B=true;
而在form2也宣告物件object2_读取m_A与m_B
B object2_=new B();
bool openA=false;
bool openB=false;
openA=object2.m_A;
openB=object2.m_B;
但是我真正执行後,发现变数m_A与m_B根本就是"false"啊
也就是说,他根本没有修改到继承里的变数
请各位高手...我该用什麽方法才能改变呢~?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 219.71.102.63
1F:推 chchwy:就说要贴能重现错误的完整code了 你贴的部分都没问题 09/09 09:35
2F:→ chchwy:错的是你没贴出来的其他部分 09/09 09:35
3F:→ chchwy:继承的变数就是这样设值没有错 有设了就应该会变true 09/09 09:36
对不起,为了要简化说明,我的程式码会与例子有出入
↓这是类别Parameter
public class Parameter
{
public string filepath;
public bool open_bin = false; //contorl open file as bin map
public bool open_distribution = false; //contorl open file as distribution data
}
public class Function:Parameter
{
public bool binmap = false; //contorl function of map enabled
public bool distribution = false; //contorl function of distribution enable
public bool ampi_test = false; //contorl cp test as ampi
public bool apce = false; //contorl cp test as apce
//==============================================================================
//↓这是form2上button click
string filename = fileListBox1.FileName;
Parameter file=new Parameter();
file.filepath = filename;
file.open_bin = true;
Function mfunction = new Function();
mfunction.binmap = true;
mfunction.ampi_test=true;
//==============================================================================
//↓这是主程式form1 上button click
Function mfunction = new Function();
Parameter file = new Parameter();
if (file.open_bin == true)
{
if (mfunction.binmap == true)
{
string filename = file.filepath;
System.IO.StreamReader machinefile = new System.IO.StreamReader(filename);
}
else
{
MessageBox.Show("The Function have mistake,Please Check your data and datalog file!", "Caution", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Please Open File", "Caution", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
理论上来说,我form2按下button後 open_bin要=true,但我实际执行後 open_bin确还是
false
※ 编辑: chrishappy11 来自: 182.234.232.171 (09/09 09:46)
※ 编辑: chrishappy11 来自: 182.234.232.171 (09/09 09:47)
※ 编辑: chrishappy11 来自: 182.234.232.171 (09/09 09:54)
4F:→ XFantasyX:你那两个button_click里面的Parameter跟Function是不同 09/09 10:05
5F:→ XFantasyX:的物件 去google一下"变数生命周期" 就会了解了 09/09 10:06