Protected微妙之出

来源:互联网 发布:java微服务框架 api 编辑:程序博客网 时间:2024/04/28 00:41

Protected说了那么多,估计还是有很多人分不清楚怎么用,下面给一个demo 

    public class LearnProtected    {        public string GetName()        {            Rat rat = new Rat();            rat.crow();//正常调用            Cat cat = new Cat();//类里面没有重写就没有办法让对象调用            return cat.crow(); //报错了,没有该方法了        }    }    public class Animal    {        protected string crow(string name)        {            return name;        }    }    public class Cat:Animal    {             }    public class Rat:Animal    {        public string crow()        {            string result = "";            result= crow("Rat"); //在类里面可以调用父类的Protcted方法            return result;        }    }


0 0
原创粉丝点击