结构体的访问

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
      
         struct student
         {
             public int no;
             public string name;
             public char sex;
             public int score;
             public string Answer()
             {
                 string result="当前学生信息如下:";
                 result +="\n学号"+no;
                 result +="\n姓名:"+name;
                 result +="\n性别:"+sex;
                 result +="\n成绩:"+score;
                 return result;

             }
         };
       

        private void label1_Click(object sender, EventArgs e)
        {
            student stu;
            stu.no=20;
            stu.name="还阿红";
            stu.score=97;
            stu.sex='男';
            label1.Text=stu.Answer();
            label1.Text+="\n\n"+DateTime.Now;


        }
    }
}
<span style="font-family: 宋体, 'Courier 10 Pitch', Monaco;">       问题一:struct student  为什么要写在那个地方,貌似不属于任何一个里面。写在给的两个模版之间 我真的不能理解。</span><br style="font-family: 宋体, 'Courier 10 Pitch', Monaco;" /><span style="font-family: 宋体, 'Courier 10 Pitch', Monaco;">       问题二:这里的PUBLIC string Anwser 这个anwser 是什么 自己定义的? 结构体里面的方法成员? 这个C++里有吗?方法成员的作用  合理的理解是怎么样的?</span><br style="font-family: 宋体, 'Courier 10 Pitch', Monaco;" /><span style="font-family: 宋体, 'Courier 10 Pitch', Monaco;">       问题三:  方法成员里的return  又是怎么回事。没有可以不。是都要RETURN还是哪些必须要用return</span>
<span style="font-family:宋体, Courier 10 Pitch, Monaco;">       问题四:<span style="font-family: 宋体, 'Courier 10 Pitch', Monaco; background-color: rgb(227, 241, 209);">定义 public struct student与上面定义的区别 是不是定义之后就不需要一直在成员里加Public.我试着在直接改 好像出错了。为什么</span></span>
<span style="font-family:宋体, Courier 10 Pitch, Monaco;"><span style="font-family: 宋体, 'Courier 10 Pitch', Monaco; background-color: rgb(227, 241, 209);">
</span></span>
<span style="font-family:宋体, Courier 10 Pitch, Monaco;"><span style="font-family: 宋体, 'Courier 10 Pitch', Monaco; background-color: rgb(227, 241, 209);">解答:</span></span>
<span style="font-family:宋体, Courier 10 Pitch, Monaco;"><span style="font-family: 宋体, 'Courier 10 Pitch', Monaco; background-color: rgb(227, 241, 209);"><span style="font-family: 宋体, 'Courier 10 Pitch', Monaco;">1、先说第一问题,对于Struct结构体的位置定义,这个没有什么关系,它属于 partial class Form1 这个类,在C#中窗体都是类,我是这样理解的,这个结构体的定义可以说是在类中定义的结构体。一般如果在C#为了定义个实体,采用类的方式,所以不建议定义结构体。可以新建一个类。</span><br style="font-family: 宋体, 'Courier 10 Pitch', Monaco;" /><span style="font-family: 宋体, 'Courier 10 Pitch', Monaco;">2、 public string Answer()是结构体中的方法,C++里面也是有结构体定义的,其实形式跟类差不多。方法成员的作用是为了描述类的功能(废话),比如定义一个People类,人会睡觉就可以定义个方法。</span><br style="font-family: 宋体, 'Courier 10 Pitch', Monaco;" /><span style="font-family: 宋体, 'Courier 10 Pitch', Monaco;">3、return是函数Answer进行返回值,由于你的public string Answer()定义,所以函数必然会返回一个string类型的字符串,void不需要返回值。</span><br style="font-family: 宋体, 'Courier 10 Pitch', Monaco;" /><span style="font-family: 宋体, 'Courier 10 Pitch', Monaco;">4、这个Public是修饰Struct的,跟它内部成员没关系,如果不加,别的类无法访问结构体,内部的public是为了让结构体对象访问内部成员。</span>
</span></span>

posted @ 2014-12-04 15:44  静以养身 俭以养德  阅读(187)  评论(0编辑  收藏  举报