C# 細節(2)

protected 修飾符,要注意,子類所繼承的protected,是屬於自己的,與其他不相干。

A 父類,B子類,C子類

不管B,C如何修改protected變量,B和C互不影響,如:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
       
        static void Main(string[] args)
        {
            B b = new B();
            C c = new C();

            //////////////////////

            不管下面誰先賦值,輸出結果不變

            c.setv("c");
            b.setv("b");

           ////////////////////////
            Console.Write(c.cv);
            Console.Write(b.bv);

        }
    }

    class A
    {
        protected string s;
    }
    class B : A
    {
        internal void setv(string a)
        {
            s = a;
        }

    
        public string bv
        {
           get{return s;}
        }
    }
    class C : A
    {
        internal void setv(string a)
        {
            s = a;
        }

        public string cv
        {
            get { return s; }
        }
    }
}

 

 

posted on 2012-02-24 10:33  Mayvar  阅读(134)  评论(0编辑  收藏  举报

导航