创建控制台应用程序,通过在类中分别定义不同的作用域的字段变量,并访问这些字段,来分析类、子类的作用域

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BClassD
{
    class Program
    {
        public class A
        {
            private int priv=20;
            protected int prot=30;
            
            public void Show()
            {
                Console.WriteLine("private={0}protected={1}", priv, prot);
                Console.WriteLine("父类内容");

            }
        }
        public class B : A
        {
            public void Shownew()
            {
                //priv = 40;
                prot = 50;
                Console.WriteLine("prot:{0}",prot);
                Console.WriteLine("子类内容");
            }
        }
        static void Main(string[] args)
        {
            B b = new B();
            b.Show();
            b.Shownew();
            b.Show();//在继承类中改变变量的值,父类中该变量的值也会改变,同一块内存
            Console.ReadKey();

        }
    }
}

avatar