本文讲述了protected c#参考!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:
protected 关键字是一个成员访问修饰符。 受保护成员在其所在的类中可由派生类实例访问。
class A { protected int x = 123; } class B : A { static void Main() { A a = new A(); B b = new B(); // Error CS1540, because x can only be accessed by // classes derived from A. // a.x = 10; // OK, because this class derives from A. b.x = 10; } }
语句a.x = 10生成错误,因为它是在静态方法 Main 中生成的,而不是类 B 的实例。
结构成员无法受保护,因为无法继承结构。
此示例中,DerivedPoint类派生自Point。 因此,可以从派生类直接访问基类的受保护成员。
class Point { protected int x; protected int y; } class DerivedPoint: Point { static void Main() { DerivedPoint dpoint = new DerivedPoint(); // Direct access to protected members: dpoint.x = 10; dpoint.y = 15; Console.WriteLine("x = {0}, y = {1}", dpoint.x, dpoint.y); } } // Output: x = 10, y = 15
更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。
想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!