简体   繁体   English

通过java中的静态方法访问私有变量

[英]Accessing a private variable through static method in java

Let say I have the following java classes: 假设我有以下java类:
Class A: A类:

public class A { 
    private int x; 

    public A(int x){ 
        this.x = x; 
    } 

    public static void main(String[] args) { 
        A a = new A(1); 
        B b = new B(1,2); 
        System.out.println((A)b.x);
    }
}

Class B: B级:

public class B extends A { 
    public int y; 

    public B(int x, int y){ 
        super(x); 
        this.y = y; 
    } 
}

Why does the compiler marks the access to x on this line 为什么编译器会在此行上标记对x的访问

System.out.println((A)b.x);

as an error, even though I'm trying to access x from the class in which it is defined? 作为一个错误,即使我试图从定义它的类访问x?

Is it because of: 是因为:
1. the use of polymorphism? 1.使用多态?
2. the use of a static method? 2.使用静态方法?
3. the use of the main method? 3.使用主要方法?

You need to make it ((A)b).x to properly type cast it 你需要做((A)b).x来正确输入它

Note : You are trying to type cast the property x to type A . 注意:您正在尝试将属性x类型转换为类型A That's the error! 那是错误!

int x is private therefore it can't be reached from outside of the scope of the class. int xprivate因此无法从类的范围之外到达它。 You could mark it as protected . 您可以将其标记为protected This way it will still have limited scope. 这样它的范围仍然有限。 Classes that extend A will be able to access the variable freely. 扩展A的类将能够自由地访问变量。

This is because the dot operator has precedence over the cast operator. 这是因为点运算符优先于强制运算符。 This will work, because it forces the cast operator to be applied before the dot operator: 这将起作用,因为它强制在点运算符之前应用强制转换运算符:

System.out.println(((A)b).x);

Demo on ideone. 在ideone上演示。

When you write (A)bx , the compiler try to cast bx into A , but x is an int 当您编写(A)bx ,编译器会尝试将bx转换为A ,但xint

Moreover, you don't need to cast b into A and you can't access bx because x is a private field. 此外,您不需要将b转换为A并且您无法访问bx因为x是私有字段。
You may need a getter for this, like b.getX() 你可能需要一个getter,比如b.getX()

You have follwing issues 你有以下问题

  • Compiler will show "Field not visible" Error,Because you trying to access private method of parent class 编译器将显示"Field not visible"错误,因为您尝试访问父类的私有方法
  • Syntactically . 句法上. operator has precedence over cast operator 运算符优先于强制运算符
  • And another impotent thing is that No need to cast a child object to parent to access parent specific members, Because they are already inherited to the child, Here the member you are accessing is private ,which is not inherited. 另一个无能为力的事情是, 不需要将子对象转换为父对象来访问父对象 ,因为它们已经被继承到子对象,这里您正在访问的成员是private ,不会被继承。 Even if you cast to parent you cant access private members using child object. 即使您转换为父级,也无法使用子对象访问私有成员。

Because you are trying to cast an int into A . 因为你试图将一个int转换为A You need to wrap the cast around the object and then call .x . 你需要围绕对象包裹演员然后调用.x

Your call is equivalent to (A)(bx) , when it should be ((A)b).x . 你的电话相当于(A)(bx) ,应该是((A)b).x

public static void main(String[] args) { 
    A a = new A(1); 
    B b = new B(1,2); 
    System.out.println(((A)b).x);
}

Basically, two issues exist here. 基本上,这里存在两个问题。

One being that int x is private so it cannot be accessed from the sub-class. 一个是int xprivate因此无法从子类访问它。

Now even if you change the access criteria of int x to public or protected ; 现在即使您将int x的访问标准更改为publicprotected ; the code will still not work because (A)bx will try to typecast an integer (read x ) to an object (read A ). 代码仍然无效,因为(A)bx将尝试将一个整数(读取x )类型转换为对象(读取A )。 Instead of this, you should use ((A)b).x 而不是这个,你应该使用((A)b).x

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

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