属性:访问指定

发布于 2024-12-12 04:19:59 字数 301 浏览 0 评论 0原文

当我参考一本书时,我得到以下陈述:

当一个数据类型或方法被定义为 public 时,其他对象可以直接访问它。当数据类型或方法被定义为 private 时,只有特定的对象可以访问它。

现在这真的很令人困惑。 Public 和 Private 是访问说明符,仅定义属性或方法的范围。

为什么对象与访问说明符混合在一起?对象是否必须对 public 、 private 或 protected 做任何事情,除了以下事实:如果某件事被定义为 public 那么对象也将能够访问,无论范围如何

While I was refering to a book , I got the following statements:

When a data type or method is defined as public , Other Objects can directly access it. When a data type or method is defined as private , only the specific object can access it.

Now this is really confusing. Public and Private are Access Specifiers which only define the scope of a attribute or method.

Why object is mixed with access specifiers? Does object has to do any thing with public , private or protected apart from the fact that if some thing is defined as public then objects too will be able to access irespective of the scope

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天气好吗我好吗 2024-12-19 04:19:59

这不是范围问题,而是访问限制修饰符。如果将函数声明为私有,则意味着只有该类可以调用该函数。

民众:
任何人都可以调用这些函数
私人的:
只有那个类和反射引擎
受保护:
仅该类及其派生成员
内部的:
中的所有班级公开

向该集会

public class A
{
    public int x;
    protected int y;
    private int z;
}

public class B : A
{
    public int CallPro()
    {
        return y;
    }

    public int CallPriv()
    {
        return z; //error 
    }

}

static void Main()
{
    A oa;
    oa.x; //Fine
    oa.y; //Error
    oa.z; //Error


}

This is not a scope question but an access limitation modifier. If you declare a function as private that means only that class can call that function.

Public:
Any one can call these functions
Private:
Only that class and the refection engine
Protected:
Only that class and its derived member
Internal:
Public to all the classes in that assembly

A small ex

public class A
{
    public int x;
    protected int y;
    private int z;
}

public class B : A
{
    public int CallPro()
    {
        return y;
    }

    public int CallPriv()
    {
        return z; //error 
    }

}

static void Main()
{
    A oa;
    oa.x; //Fine
    oa.y; //Error
    oa.z; //Error


}
葬花如无物 2024-12-19 04:19:59

公共和私有是仅定义范围的访问说明符
属性或方法的。

这定义了对象的行为。因此,访问说明符对于对象很重要。想象一下,如果您有一个外观类型的对象,您不想公开操作的所有细节,而是希望向消费者公开一个简单的公共接口(例如,Save() 方法)。这就是为什么您必须考虑对象的说明符。

public class CustomerFacade()
{
public bool Save(Customer c)
{
    VerifyCustomer(c);
// lots of other steps which the caller does not need to know
    SaveorUpdateCustomer(c);
    }
    private void VerifyCustomer(Customer c)
    {
    }
    private void SaveorUpdateCustomer(Customer c)
    {
    }
    }

public class CustomerController()
{
public bool Save(Customer c)
{
return new CustomerFacade().Save(c);
}
}

Public and Private are Access Specifiers which only define the scope
of a attribute or method.

And that defines the behaviour of the object. Hence the access specifiers are important for the object. Imagine if you have a facade kind of object, you don't want to expose all the details of the operation, rather you want a simple public interface (eg, Save() method) to be exposed for the consumers. That's why you have to consider the specifiers for objects.

public class CustomerFacade()
{
public bool Save(Customer c)
{
    VerifyCustomer(c);
// lots of other steps which the caller does not need to know
    SaveorUpdateCustomer(c);
    }
    private void VerifyCustomer(Customer c)
    {
    }
    private void SaveorUpdateCustomer(Customer c)
    {
    }
    }

public class CustomerController()
{
public bool Save(Customer c)
{
return new CustomerFacade().Save(c);
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文