如何将私人领域从一个班级衍生到另一个班级

编程入门 行业动态 更新时间:2024-10-25 12:26:06
本文介绍了如何将私人领域从一个班级衍生到另一个班级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我从Button等类继承时,Button的属性如何工作? 虽然私有字段Button不能继承到My Class,但是属性需要私有字段才能返回值(例如Location属性)?

When I have Inheritance from a Class such as Button, how do the properties of Button work? While private fields Button Not Inherits to My Class, but properties need to private field to return value(Such as Location Property)?

public class MyClass:Button { MyClass() { this.Location = new System.Drawing.Point(134, 34); } } ---------------------------------------------------------------------- ///a property of Button Class(.NET Source Code ) public Point Location { get { return new Point(this.x, this.y); //x is a private field } set { this.SetBounds(value.X, value.Y, this.width, this.height, BoundsSpecified.Location); } }

推荐答案

破译您的问题..... 完成. 您的意思是您不能继承基类的私有字段吗?如果是这样,您就不能.私有并不意味着要被其他类继承.如果要继承字段,请将其更改为protected或添加一个获取私有字段值的公共属性.阅读有关访问修饰符的更多信息. Deciphering your question..... DONE. Did you mean you can''t inherit private fields of your base class? If so, you can''t. Private are''nt meant to be inherited by other classes. If you want your field to be inherited, change them to protected or add a public property that gets the value of your private field. Read more about access modifiers.

阅读本页: 访问修饰符 [ ^ ] Read this page: Access Modifiers[^]

您不能.那就是私人的意思.如果要进行属性覆盖,则应使用base. PropertyName 并将该值用于进一步处理(在本示例中,请执行Point loc = base.Location; return new Point(loc.X, loc.Y);). 另外,我确定您知道这一点,但是您发布的示例完全没有意义,因为这几乎已经是Control.Location所做的. You can''t. That''s what private means. If you''re doing a property override you should use base.PropertyName and use that value for further processing (in this example, do Point loc = base.Location; return new Point(loc.X, loc.Y);). Also, I''m sure you know this but the example you posted is entirely pointless as that''s pretty much what Control.Location does already.

更多推荐

如何将私人领域从一个班级衍生到另一个班级

本文发布于:2023-10-31 05:51:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545199.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:班级   如何将   私人   领域

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!