您的位置:首页 > 其它

private和public关键字关于对象的访问权限问题

2013-05-31 00:23 531 查看
1.private关键字的访问权限是类访问权限,如果加了static关键字,则只能通过类来进行访问,否则只能通过类的对象进行访问。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test
{
public partial class Form2 : Form
{
public string s1;
private string s2;
private static string s3;
public Form2()
{
InitializeComponent();
}

private void buttonSend_Click(object sender, EventArgs e)
{
s1 = textBox1.Text;
Form2 f2 = new Form2();
f2.s2 = "ab";//通过f2可以访问的成员是s1和s2,无法访问s3
Form2.s3 = "abc";//只能通过类Form2来访问s3
}

private void Form2_Load(object sender, EventArgs e)
{

}
}
}


2.public关键字是类型和类型成员的访问修饰符。 公共访问是允许的最高访问级别, 对访问公共成员没有限制。若没有加static关键字,则可以在类外通过对象进行访问,若加了static关键字,也还是只能通过类来进行访问。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
//f1.Owner = this;
f2.Show();
f2.s1 = "ab";
Form2.s11 = "a";
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: