如何将私人领域从一个班级衍生到另一个班级 [英] How Derived private field a class to another class

查看:51
本文介绍了如何将私人领域从一个班级衍生到另一个班级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从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.


这篇关于如何将私人领域从一个班级衍生到另一个班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆