简体   繁体   English

如何从嵌套类访问Extended Class的方法?

[英]How to access a method of Extended Class from nested class?

I have two nested classes inside a class with the outer class extending another class. 我在一个类中有两个嵌套类,外部类扩展另一个类。 The structure is something like this. 结构是这样的。

public class EXTENSION_CLASS
{
    public int Get_Value()
    {
        return(100);
    }
}

public class OUTER extends EXTENSION_CLASS
{
    public static class NESTED1
    {
        public void Method1()
        {
          int value=0;
          value=Get_Value();
          System.out.println("Method1: "+value);
        }
    }
    public static class NESTED2
    {
        NESTED1 Nested1_Instance=new NESTED1();
        public void Method2()
        {
            Nested1_Instance.Method1();
        }
    }
    public void run()
    {
        NESTED2 Nested2_Instance=new NESTED2();
        Nested2_Instance.Method2();
    }
    public static void main (String[] args)
    {
       OUTER New_Class=new OUTER();
       New_Class.run();
    }
}

I'm expecting the output: "Method1: 100". 我期待输出:“Method1:100”。 But, am getting the output: "OUTER.java:16: error: non-static method Get_Value() cannot be referenced from a static context value=Get_Value();". 但是,我得到输出:“OUTER.java:16:错误:非静态方法Get_Value()不能从静态上下文值= Get_Value();”引用。 How can i make this working? 我怎样才能使这个工作?

Cheers ! 干杯!

Rajesh. 拉杰什。

NESTED1NESTED2的声明中删除static修饰符,如下所示:

public class NESTED1

You're trying to make a static reference to a non-static member. 您正在尝试对非静态成员进行static引用。

This means that you're trying to access a instance member from a static member of the class. 这意味着您正在尝试从类的静态成员访问实例成员。

To fix the issue, remove the static modifier from both the NESTED1 and NESTED2 clases. 要解决此问题,请从NESTED1和NESTED2分支中删除static修饰符。

Alternately, if you do not wish to remove the static modifier, you will have to create an object of the OUTER or EXTENSION_CLASS classes and then invoke Get_Value() using the object. 或者,如果您不想删除static修饰符,则必须创建OUTEREXTENSION_CLASS类的对象,然后使用该对象调用Get_Value()。

For example: 例如:

public void Method1()
{
      int value=0;
      EXTENSION_CLASS ext = new EXTENSION_CLASS ();
      value=ext.Get_Value();
      System.out.println("Method1: "+value);
}

OR 要么

public void Method1()
{
      int value=0;
      OUTER outer = new OUTER();
      value=outer.Get_Value();
      System.out.println("Method1: "+value);
}

If you want keep the nested classes static, you will have to create an instance of OUTER in the Method1() to access Get_Value(). 如果要将嵌套类保持为静态,则必须在Method1()中创建OUTER实例以访问Get_Value()。

  public void Method1()
    {
      int value=0;
       OUTER outer = new OUTER();
       value=outer.Get_Value();
      System.out.println("Method1: "+value);
    }

The error says every thing. 错误说明了每一件事。 You can not call non-static methods from static methods / class. 您不能从静态方法/类中调用非静态方法。 To make it work, Simply add static keyword to 'Get_Value' method in EXTENSION_CLASS. 要使其工作,只需在EXTENSION_CLASS中将静态关键字添加到'Get_Value'方法即可。

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

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