简体   繁体   English

OOP-谁可以访问公共,私有,受保护的变量?

[英]OOP - Who can access to public, private, protected variable?

I'm new to OOP. 我是OOP的新手。 Look at following pseudo code: 看下面的伪代码:

Class Test{
   public String a;
   protected String b;
   private String c;
   public void aa(){}
   protected void bb(){}
   private void cc(){}
   Class Test2{
      private void dd(){}
   }
}
Class Test3 extends Test{
   private void ee(){}
}
Class Test4{
   private void ff(){}
}

Can a , b and c access into aa() , bb() and cc() ? abc可以访问aa()bb()cc()吗? Can a , b and c access into the class Test2 and dd() ? abc可以访问类Test2dd()吗? Is true that only a and b can access into the class Test3 and ee() ? 是否只有ab可以访问Test3ee() Is true that only a can access into the class Test4 and ff() ? 是否只有a可以访问Test4ff()

For the first question 对于第一个问题
"Can aa() access a,b,c of class Test" : Yes it can access member of its outer class. “ aa()是否可以访问Test类的a,b,c” :是的,它可以访问其外部类的成员。 Test2 is an inner class and an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields Test2是一个内部类,并且内部类与其封闭类的实例相关联,并且可以直接访问该对象的方法和字段

for second question 第二个问题
"Is true that only a and b can access into the class Test3" : Yes a,b can be accessible inside class Test3. “是的,只有a和b可以访问Test3类” :是的,a,b可以在Test3类中进行访问。 Subclass can access Public and Protected members of its base class. 子类可以访问其基类的Public和Protected成员。

for third one 对于第三
"Is true that only a can access into the class Test4?" “是真的,只有a可以访问Test4类吗?” : Yes, only 'a' can be accessed inside class Test4 if you make an object of class Test and access it by using dot(.) operator. :是的,如果您创建Test类的对象并使用dot(。)运算符进行访问,则只能在Test4类中访问“ a”。

  • private - Only the class protected 私有-仅受保护的类

  • Protected Those that inherit 受保护的人

  • Public the world 公开世界

What you're talking about are called access-modifiers in java. 您在说什么,在Java中称为访问修饰符 You have mentioned three of them but there are in total 4 types of access modifiers : 您已经提到了其中的三个,但是总共有四种类型的访问修饰符:

  1. private - They are not visible to anybody except to members of the class they reside in. 私人-他们所在的类的成员对其他任何人都不可见。
  2. protected - private + Visible to sub-classes as well. 保护-私有+子类别也可见。
  3. default - private + other classes in the same package. 默认-同一包中的private +其他类。
  4. public - Visible to the entire universe. 公共-对整个宇宙可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM