【发布时间】:2020-07-15 01:57:14
【问题描述】:
实际上,我正在开发一个项目,在该项目中我必须访问另一个类的方法的变量,并且在我的项目中面临类似的情况,我无法返回该值。请任何人解决这个问题。
package com.company;
class aaa {
int num1;
public void val() { // data type can't be changed
num1 = 100;
}
}
class bbb {
public void Values() {
int num = 100;
}
}
public class Main extends bbb {
public static void main(String[] args) {
// write your code here
aaa obj1 = new aaa();
bbb abj2 = new bbb();
System.out.println(obj1.num1); // wants to print value 100 here but
//without returning value from the function
}
}
【问题讨论】:
-
仅供参考:您可能想查看this。
-
这根本不可能。方法的变量仅在方法运行时存在。即使该方法在调用堆栈上,您也无法访问该变量。你到底想达到什么目的?
标签: java variables inheritance multiple-inheritance