繁体   English   中英

不能从静态方法中引用非静态方法

[英]non-static method cannot be referenced from a static method

我正在尝试打印我的 HighRights 对象,但它不会让我......一直说不能从静态方法中引用非静态方法。 我基本上希望它打印出所有添加的值(“AAA”、“AACCA”...等)。

import java.util.*; 

public class Ex8 {


    public void printHighUsers(ArrayList<SecurityRights> a){
       for(SecurityRights m: a)
        {
            if(m instanceof HighRights)
            {
                System.out.println(HighRights.getName());
            }
        }
    }

    public static void main(String [] a){
        ArrayList<SecurityRights> ma=new ArrayList<SecurityRights>();
        ma.add(new HighRights("AAA"));
        ma.add(new HighRights("AACCA"));
        ma.add(new HighRights("BB"));
        ma.add(new HighRights("AaaAA"));
        new Ex8().printHighUsers(ma);
    }
}

HighRights 类:

public class HighRights extends SecurityRights
{
    private String name;

    public HighRights(String n){
    super(true);
    this.name = n;
   }

   public String getName(){
        return name;
    }


    public static void main(String[] a){

    HighRights s= new HighRights("Lisa");
    System.out.print(s.getName() +" "+s.getSecret());                
  }

}

谢谢你。

System.out.println(HighRights.getName());

您试图静态调用getName() ,但它不是static方法。

你想要的是

System.out.println(((HighRights) m).getName());

换句话说,您需要将引用HighRightsHighRights以便getName()方法可访问并在引用上调用它,而不是静态调用它。

暂无
暂无

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

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