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

1.private关键字的访问权限是类访问权限,如果加了static关键字,则只能通过类来进行访问,否则只能通过类的对象进行访问。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace Test
11 {
12 public partial class Form2 : Form
13 {
14 public string s1; 
15 private string s2;
16 private static string s3;
17 public Form2()
18 {
19 InitializeComponent();
20 }
21 
22 private void buttonSend_Click(object sender, EventArgs e)
23 {
24 s1 = textBox1.Text;
25 Form2 f2 = new Form2();
26 f2.s2 = "ab";//通过f2可以访问的成员是s1和s2,无法访问s3
27 Form2.s3 = "abc";//只能通过类Form2来访问s3
28 }
29 
30 private void Form2_Load(object sender, EventArgs e)
31 {
32 
33 }
34 }
35 }

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

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace Test
11 {
12     public partial class Form1 : Form
13     {
14         
15         public Form1()
16         {
17             InitializeComponent();
18         }
19 
20         private void button1_Click(object sender, EventArgs e)
21         {
22             Form2 f2 = new Form2();
23             //f1.Owner = this;
24             f2.Show();
25             f2.s1 = "ab";
26             Form2.s11 = "a";
27         }
28     }
29 }

 

转载于:https://www.cnblogs.com/iammackong/archive/2013/05/31/3109515.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值