简体   繁体   English

子类对象如何访问父类的私有变量,就像java中一样,除了类本身可以访问私有变量之外,否?

[英]How does the subclass object access the private variable of super class,as in java no except the class itself can access the private variable?

This is the pac.java file 这是pac.java文件

package P1; 

public class pac {

    int a;
    public int b;
    private int c;
    protected int d;

    public pac(){
        a=1;
        b=2;
        c=3;
        d=4;
    }

    public void test(){
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }
}

This is other file pack1.java in different package 这是不同包中的其他文件pack1.java

package P3;

class pac1 extends P1.pac{
    public static void main(String args[]) {
        pac1 ob=new pac1();
        ob.test();
    }
}

Question: 题:
How is that possible that from the two files I have shown, the file P3\\pac1.java creates an object which access the private variable of the class P1\\pac.java ? 从我所示的两个文件中,文件P3\\pac1.java创建一个访问类P1\\pac.java的私有变量的对象?

The method " test() " is inside of the class "pac". 方法“ test()”位于类“ pac”内部。 Therefore, when you create an instance of pac, the test method belongs to this instance, and can see and use all the variables. 因此,当您创建pac实例时,测试方法属于该实例,并且可以查看和使用所有变量。

If you had done this, it would have caused an error. 如果执行此操作,则将导致错误。

 int mainC = ob.c; 

the main method (where this is happening) cannot access the variable "c" main方法(正在发生这种情况)无法访问变量“ c”

What I understand from your unalligned code is that you instantiate a public class and call a public method of it. 我从您未指定的代码中了解到的是,您实例化了一个公共类并调用了它的公共方法。 This is perfectly fine, the test() method has access to variables. 很好,test()方法可以访问变量。 You can't access them directly from another method in pac1. 您无法通过pac1中的其他方法直接访问它们。

The statement pac1 ob=new pac1(); 语句pac1 ob=new pac1(); is creating a new object of Pac1 class and it is possible because it is public . 正在创建一个Pac1类的new对象,这是可能的,因为它是public

using ob.test() you are calling a method on the object itself. 使用ob.test()可以在对象本身上调用方法。 As private int c is member of the same class in which test() is defined, it can access it. 由于private int c是定义了test()的同一类的成员,因此它可以访问它。

  • A private member variable or method is accessible throughout the class in which it is defined. 在定义它的整个类中都可以访问私有成员变量或方法。
  • You can create the object of Pac1 because the class is public . 您可以创建Pac1的对象,因为该类是public

To clarify your doubt, you cannot access the private member outside the class like this ob.c in the main method. 为了澄清您的疑问,您不能在main方法中像此类ob.c一样访问类外部的私有成员。

Consider a very simple example. 考虑一个非常简单的例子。 For a javabean, we make member as private and getter setters as public . 对于javabean,我们将member设置为private ,将getter setter设置为public We can access them using only getter and setter as they are public. 我们只能使用公开的getter和setter来访问它们。

public class SomeClass{
   private int someMember;

   public int getSomeMember(){
     return this.someMember;
   }
}

Now from outside the class,suppose in your main method, 现在从类外部,假设您的main方法中,

SomeClass someClass = new SomeClass();
someClass.someMember; // This is not possible as we are directly accessing private field
someClass.getSomeMember; // possible because `getSomeMember` is public and it belongs to `SomeClass` so can access private member `someMember`.

` `

pac1.java only didn't access the private member c of pac.java. pac1.java仅不访问pac.java的私有成员c。 It only called the public method test of pac, and that method access the member of the same class. 它仅调用pac的公共方法测试,该方法访问同一类的成员。

If I understand you correctly, you are curious how calling ob.test () accesses the private members, despite ob being an object of the derived class. 如果我理解正确,您会很好奇,尽管ob是派生类的对象,但是调用ob.test()如何访问私有成员。 The reason is that whether a method has access is determined at compile time, not run time, and is determined by the location of the method. 原因是方法是否具有访问权限是在编译时而不是运行时确定的,并由该方法的位置确定。 Your test method is declared in your pac class. 您的测试方法在pac类中声明。 So the compiler gives it access to all the private members of the pack class. 因此,编译器允许它访问pack类的所有私有成员。 Now, when it comes to executing the method, it transpires that the type of object your method is executing on is pac1. 现在,在执行该方法时,它会表明您的方法正在执行的对象类型是pac1。 That does not matter. 那不重要。

This feature of object oriented languages is not a mistake. 面向对象语言的这一特性不是错误。 It makes it possible for the writer of a class to provide limited access to private members, by providing a method that gives only that limited access. 通过提供仅提供有限访问权限的方法,该类的编写者可以对私有成员提供有限访问权限。


In Java, a subclass object cannot access private variables (or methods) of any super class . 在Java中, 子类对象无法访问任何超类的私有变量(或方法)
In your example, pac1 class only access public functionality of class pac , such as the public default constructor & the public method test() . 在您的示例中, pac1类仅访问pac类的公共功能,例如公共默认构造函数和公共方法test() Since the implemnetation of these methods is defined within the super class, both can access its private funvtionality - such as the private int c . 由于这些方法的实现是在超类中定义的,因此两者都可以访问其私有功能-例如private int c
If you override these method in subclass however, the overriding methods implementation won't be able to access class pac private functionality. 但是,如果您在子类中重写这些方法,则重写方法实现将无法访问pac私有类功能。
HTH. HTH。

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

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