为什么要在该实例之外调用另一个相同类型实例的私有方法? [英] Why can I call a private method of another instance of the same type outside of that instance?
本文介绍了为什么要在该实例之外调用另一个相同类型实例的私有方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有ObjectA,并且它有一个私有方法 GetPrice()
,并且还具有相同类型的父字段,为什么我可以调用 GetPrice()
在子实例中从父实例开始?
If I have ObjectA, and it has a private method GetPrice()
and also has a "parent" field of the same type, why am I able to call GetPrice()
on the parent instance from within the child instance?
示例:
private decimal GetPrice()
{
ObjectA parent = Parent;
if(parent != null)
{
return parent.GetPrice(); // Why is this OK?
}
return 0;
}
推荐答案
因为私有意味着无法访问到其他类型,而不是其他实例无法访问。
Because private means "not accessible to other types", not "not accessible to other instances".
这篇关于为什么要在该实例之外调用另一个相同类型实例的私有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文