JAVA定义一个Customer类,包含姓名(name)、身高(height)、体重(weight),分数(score)以及speak()方法,该方法的功能是,输出自己的相关信息。 Customer类实现Comparable接口,实现比较两个Customer对象的大小,比较规则是:身高占20%权重,体重占30%权重,分数占50%权重,以综合计算三项权重和之后的值作为判断对象大小的依据。
import java.util.Arrays;
class Customer implements Comparable {
private String name;
private float height, weight, score;
public Customer(String name, float height, float weight, float score) {
super();
this.name = name;
this.height = height;
this.weight = weight;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
public void speak() {
System.out.println("I am " + name + ",my height " + height + ",my weight " + weight + ",my score " + score);
}
public int compareTo(Customer o) {
float s1=this.height+this.weight;
float s2=o.height+o.weight;
if(s1>s2) {
return 1;
}
if(s1<s2) {
return -1;
}
return 0;
}
}
public String toString() {
return "Customer [name="+name+",height="+height+",weight="+weight+"]";
}
}
public class TestCompare {
public static void main(String[] args) {
int i;
Customer ps[] = new Customer[6];
ps[0] = new Customer("zhangsan", 170, 110, 95);
ps[1] = new Customer("lisi", 168, 120, 75);
ps[2] = new Customer("wangwu", 165, 115, 88);
ps[3] = new Customer("zhaoliu", 172, 121, 90);
ps[4] = new Customer("zhouqi", 160, 100, 85);
ps[5] = new Customer("zhengba", 166, 119, 70);
System.out.println("array sort before:");
for (i = 0; i < ps.length; i++) {
ps[i].speak();
}
Arrays.sort(ps); // call sort method
System.out.println("\narray sort after:");
for (i = 0; i < ps.length; i++) {
System.out.println(ps[i]);
}
}
}
TestCompare.java:65: error: class, interface, or enum expected
public String toString() {
^
1 error
怎么解决啊, public String toString() {
return "Customer [name="+name+",height="+height+",weight="+weight+"]";
}
}
这一段错误