Java 访问嵌套接口数据变量
我无法访问此代码的变量Java 访问嵌套接口数据变量,java,interface,nested,Java,Interface,Nested,我无法访问此代码的变量I: interface three{ void how(); interface two{ int i=2; void what(); } } class one implements three,two{ public void how(){ System.out.println("\nHow! i = " + i); what(); } public
I
:
interface three{
void how();
interface two{
int i=2;
void what();
}
}
class one implements three,two{
public void how(){
System.out.println("\nHow! i = " + i);
what();
}
public void what(){
System.out.println("\nWhat! i = " + i);
}
public static void main(String args[]){
one a = new one();
a.how();
a.what();
}
}
生成的错误是:
one.java:17: error: cannot find symbol
System.out.println("\nWhat! i = " + i);
symbol: variable i
location: class one
您可以将其编码如下 我把这些课分开,供你参考 three.java
public interface three{
void how();
}
public interface two{
int i=2;
void what();
}
class one implements two,three{
public void how(){
System.out.println("\nHow! i = " + i);
what();
}
public void what(){
System.out.println("\nWhat! i = " + i);
}
public static void main(String args[]){
one a = new one();
a.how();
a.what();
}
}
然后将其编译为javacThree.java
public interface three{
void how();
}
public interface two{
int i=2;
void what();
}
class one implements two,three{
public void how(){
System.out.println("\nHow! i = " + i);
what();
}
public void what(){
System.out.println("\nWhat! i = " + i);
}
public static void main(String args[]){
one a = new one();
a.how();
a.what();
}
}
之后
two.java
public interface three{
void how();
}
public interface two{
int i=2;
void what();
}
class one implements two,three{
public void how(){
System.out.println("\nHow! i = " + i);
what();
}
public void what(){
System.out.println("\nWhat! i = " + i);
}
public static void main(String args[]){
one a = new one();
a.how();
a.what();
}
}
编译为javactwo.java
public interface three{
void how();
}
public interface two{
int i=2;
void what();
}
class one implements two,three{
public void how(){
System.out.println("\nHow! i = " + i);
what();
}
public void what(){
System.out.println("\nWhat! i = " + i);
}
public static void main(String args[]){
one a = new one();
a.how();
a.what();
}
}
然后
one.java
public interface three{
void how();
}
public interface two{
int i=2;
void what();
}
class one implements two,three{
public void how(){
System.out.println("\nHow! i = " + i);
what();
}
public void what(){
System.out.println("\nWhat! i = " + i);
}
public static void main(String args[]){
one a = new one();
a.how();
a.what();
}
}
然后编译如下
javac one.java
public interface three{
void how();
}
public interface two{
int i=2;
void what();
}
class one implements two,three{
public void how(){
System.out.println("\nHow! i = " + i);
what();
}
public void what(){
System.out.println("\nWhat! i = " + i);
}
public static void main(String args[]){
one a = new one();
a.how();
a.what();
}
}
在那之后把它当作
JavaOne
然后您将获得以下输出
How! i = 2
What! i = 2
What! i = 2
在您的问题中,我理解的是接口三种方法无法访问接口二的I变量
或者您可以这样编码
three.java
public interface three{
void how();
}
public interface two{
int i=2;
void what();
}
class one implements two,three{
public void how(){
System.out.println("\nHow! i = " + i);
what();
}
public void what(){
System.out.println("\nWhat! i = " + i);
}
public static void main(String args[]){
one a = new one();
a.how();
a.what();
}
}
one.java
public interface three{
void how();
}
public interface two{
int i=2;
void what();
}
class one implements two,three{
public void how(){
System.out.println("\nHow! i = " + i);
what();
}
public void what(){
System.out.println("\nWhat! i = " + i);
}
public static void main(String args[]){
one a = new one();
a.how();
a.what();
}
}
输出如下所示
What! i = 2
How! i = 2
What! i = 2
希望这将有助于解决您的问题。您应该在外部创建接口,以便其他类可以访问它
interface three {
void how();
}
interface two {
int i = 2;
void what();
}
public class one implements three, two {
public void how() {
System.out.println("\nHow! i = " + i);
what();
}
public void what() {
System.out.println("\nWhat! i = " + i);
}
public static void main(String args[]) {
one a = new one();
a.how();
a.what();
}
}
您始终可以通过
two.i
访问它。无论如何,你的方法似乎适合我在IntelliJ中使用。此外,像这样的嵌套接口可能会很复杂,并导致问题。