Discuss / Java / 为啥我仿照String的compareTo,写一个帮助类,省略this,不能用?

为啥我仿照String的compareTo,写一个帮助类,省略this,不能用?

Topic source
public class Main {

	public static void main(String[] args) {
		String[] array = new String[] { "apple", "Orange", "banana", "Lemon" };
		// 请使用忽略大小写排序,并改写为方法引用:
		Arrays.sort(array, helper::compare);
		System.out.println(String.join(", ", array));
	}
}



public class helper {
	private String helpp;
	
	public helper() {
		this.helpp = "aa";
	}
	public int compare(String b) {
		return this.helpp.toLowerCase().compareTo(b.toLowerCase());
	}
}


因为main是按照String排的,不是按照helper排的,改成按照helper排的,就可以了

Joker.fu_95

#3 Created at ... [Delete] [Delete and Lock User]

你这不是多此一举吗

保持热爱

#4 Created at ... [Delete] [Delete and Lock User]

方法签名不一样

花间的酒

#5 Created at ... [Delete] [Delete and Lock User]

这个帮助类的 this是 Helper类型吧,不是 String 类型

public int compare(Helper this, String b)

chen tz'u

#6 Created at ... [Delete] [Delete and Lock User]

Arrays.sort(arr,comparator) 方法的第二个参数要求是一个Comparator接口对象,help类没有实现Comparator接口,方法签名也不对。应该是compare(String s1,String s2)


  • 1

Reply