static与静态类、内部类与外部类

发布时间 2024-01-06 07:22:48作者: 加固文明幻景

引子

早上想试试java写点题,写重载排序比较接口的时候突然报了一个错误。

No enclosing instance of type demo is accessible. Must qualify the allocation with an enclosing instance of type demo (e.g. x.new A() where x is an instance of demo).

从这个报错可以看出,似乎我的重载接口需要新建一个对象(实例)才能用。

分析

public class Main {
	static final int mod = (int) (1e9 + 7);
    public static void solve() throws IOException {
    	int n = in.nextInt();
    	Integer[] a = new Integer[n];
    	Integer[] b = new Integer[n];
    	for (Integer x : a) x = in.nextInt();
    	for (Integer x : b) x = in.nextInt();
    	Arrays.sort(a, new cmp());
    	
    }
    
    public static void main(String[] args) throws IOException {
       int t = in.nextInt();
       while(t-- > 0) solve();
       out.flush();
       out.close();
    }
    
    class cmp implements Comparator<Integer> {
		@Override
		public int compare(Integer o1, Integer o2) {
			return o2.compareTo(o1);
		}
    }
}

cmp是内部类!

原来是因为 cmp 的位置放错了,我在 MAIN 类的内部声明了一个 cmp 类,且这个类是非静态类(不伴随类一起出现,必须有对象才能出现)。所以报了这个错误。

修正

根据问题分析,显然有三种思路。

对象调用

直接创造一个Main对象来调用new cmp()方法。

public class Main {
	static final int mod = (int) (1e9 + 7);
    public static void solve() throws IOException {
    	int n = in.nextInt();
    	Integer[] a = new Integer[n];
    	Integer[] b = new Integer[n];
    	for (Integer x : a) x = in.nextInt();
    	for (Integer x : b) x = in.nextInt();
        Main t = new Main();
    	Arrays.sort(a, t.new cmp());
    }
    
    public static void main(String[] args) throws IOException {
       int t = in.nextInt();
       while(t-- > 0) solve();
       out.flush();
       out.close();
    }
    
    class cmp implements Comparator<Integer> {
		@Override
		public int compare(Integer o1, Integer o2) {
			return o2.compareTo(o1);
		}
    }
}

改静态类

直接把 cmp 类改成静态类,这样就是随类生成能直接被类使用了。

public class Main {
	static final int mod = (int) (1e9 + 7);
    public static void solve() throws IOException {
    	int n = in.nextInt();
    	Integer[] a = new Integer[n];
    	Integer[] b = new Integer[n];
    	for (Integer x : a) x = in.nextInt();
    	for (Integer x : b) x = in.nextInt();
    	Arrays.sort(a, new cmp());
    	
    }
    
    public static void main(String[] args) throws IOException {
       int t = in.nextInt();
       while(t-- > 0) solve();
       out.flush();
       out.close();
    }
    
    static class cmp implements Comparator<Integer> {
		@Override
		public int compare(Integer o1, Integer o2) {
			return o2.compareTo(o1);
		}
    }
}

改外部类

没法直接 new 是因为 cmp 是内部类,改成外部类即可。

public class Main {
	static final int mod = (int) (1e9 + 7);
    public static void solve() throws IOException {
    	int n = in.nextInt();
    	Integer[] a = new Integer[n];
    	Integer[] b = new Integer[n];
    	for (Integer x : a) x = in.nextInt();
    	for (Integer x : b) x = in.nextInt();
    	Arrays.sort(a, new cmp());
    	
    }
    
    public static void main(String[] args) throws IOException {
       int t = in.nextInt();
       while(t-- > 0) solve();
       out.flush();
       out.close();
    } 
}
class cmp implements Comparator<Integer> {
		@Override
	public int compare(Integer o1, Integer o2) {
		return o2.compareTo(o1);
    }
}