[英]non-static method cannot be referenced from a static method
I'm trying to print my HighRights object but it wont let me..keeps saying non-static method cannot be referenced from a static method.我正在尝试打印我的 HighRights 对象,但它不会让我......一直说不能从静态方法中引用非静态方法。 I basically want it to print out all the values that were added ("AAA","AACCA"..etc).
我基本上希望它打印出所有添加的值(“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 class: 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());
}
}
Thank You.谢谢你。
In在
System.out.println(HighRights.getName());
you're trying to invoke getName()
statically, but it is not a static
method.您试图静态调用
getName()
,但它不是static
方法。
What you want is你想要的是
System.out.println(((HighRights) m).getName());
In other words, you need to cast your reference to HighRights
so that the getName()
method is accessible and invoke it on the reference, rather than statically.换句话说,您需要将引用
HighRights
为HighRights
以便getName()
方法可访问并在引用上调用它,而不是静态调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.