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/p/3109515.html