简体   繁体   English

如何在课外访问私有数据成员?

[英]How to access private data member outside the class?

I am trying to access the private data member of inner class outside the outer class. 我正在尝试在外部类之外访问内部类的私有数据成员。

Please help me? 请帮我?

You don't - that's the whole point of it being private. 您不需要-这就是私有化的全部意义。

The inner class can expose the data via a property, of course: 内部类当然可以通过属性公开数据:

public class Outer {
    public class Inner {
        private final String name;

        public Inner(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }
}

public class Other {
    public void foo() {
        Outer outer = new Outer();
        Outer.Inner inner = outer.new Inner("Foo");
        // Can't access inner.name here...
        System.out.println(inner.getName()); // But can call getName
    }
}

... but if the inner class wants to keep it private, then you shouldn't try to access it. ...但是,如果内部类希望将其保持私有,那么您不应该尝试访问它。

Create public getter setter methods inside the inner class for private variables. 在内部类中为私有变量创建公共getter setter方法。 Then create an object and call them to access private data. 然后创建一个对象并调用它们以访问私有数据。 You can't directly access private data. 您不能直接访问私人数据。

you cant access a private data. 您无法访问私人数据。 If ýou have other thoughts of accessing it use public getter method returning that private data. 如果您有其他访问想法,请使用公共获取方法返回该私有数据。

you can access private data member using getter method ex 您可以使用getter方法ex访问私有数据成员

package pack;

import java.io.ObjectInputStream.GetField;

public class abc {
    private int num = 2;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

}

class otherClass {
    public static void main(String[] args) {
        abc obj = new abc();
        System.out.println(obj.getNum());
    }
}

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

相关问题 Java如何访问类的私有静态成员 - Java How to access private static member of class java匿名类可以访问外部类的私有成员。 为什么此代码无法访问私有数据成员? - java anoymous class can access private member of outer class. Why this code cannot access private data member? 如何访问私有包类的公共成员? - How can I access public member of private package class? 关于访问外部类的私有成员 - Regarding access to private member of outer class 为什么私有数据成员在子类中继承 - Why Private data member inherited in child class 类方法如何访问同一类另一个实例的私有成员? - How can a class method access a private member of another instance of the same class? 创建自定义视图:如何扩展类并访问基类的私有成员变量? - Creating a custom view: how can I extend a class and access the base class's private member variables? 如何在方法外部访问已在其他类中实例化的类A的成员变量? - How can I access the member variable of class A outside the method, that has been instantiated in other class? 如何从班级获取私人会员的信息? - How to get info from class to private member? 如何防止在同一个 class 中直接访问 getter/setter 之外的私有成员? - How to prevent direct access to private members outside of getters/setters in the same class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM