java基础复习(抽象类和接口)

来源:互联网 发布:淘宝 夜鹰 编辑:程序博客网 时间:2024/05/22 13:20
  • 接口中的数据域只能是public static final,方法只能是public abstract
    由于这个原因,这些修饰也可以忽略。

数据域只能是static final的原因:
stackoverflow上:

An interface can’t have behavior or state because it is intended to specify only an interaction contract, no implementation details. No behavior is enforced by not allowing method/constructor bodies or static/instance initializing blocks. No state is enforced by only allowing static final fields.Therefore, the Class can have a state (static state), but the instance state is not inferred by the interface.

  • CompareTo方法和equals方法

强烈建议CompareTo应该与equals保持一致。对于两个对象o1和o2, 应该确保以下同时成立:
o1.equals(o2)为true
o1.compareTo(o2)==0

The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C. Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false

stackoverflow上有人提了一个问题,与这个有点关系,
Java, Why it is implied that objects are equal if compareTo() returns 0?

问题:

import java.util.Comparator;import java.util.HashSet;import java.util.Objects;import java.util.Set;import java.util.TreeSet;public class Person implements Comparable<Person> {private final String name;private int height;public Person(String name,        int height) {    this.name = name;    this.height = height;}public int getHeight() {    return height;}public void setHeight(int height) {    this.height = height;}public String getName() {    return name;}@Overridepublic int compareTo(Person o) {    return Integer.compare(height, o.height);}public boolean equals(Object obj) {    if (obj == null) {        return false;    }    if (getClass() != obj.getClass()) {        return false;    }    final Person other = (Person) obj;    if (!Objects.equals(this.name, other.name)) {        return false;    }    return true;}public int hashCode() {    int hash = 5;    hash = 13 * hash + Objects.hashCode(this.name);    return hash;}public String toString() {    return "Person{" + name + ", height = " + height + '}';}public static class PComparator1 implements Comparator<Person> {    @Override    public int compare(Person o1,            Person o2) {        return o1.compareTo(o2);    }}public static class PComparator2 implements Comparator<Person> {    @Override    public int compare(Person o1,            Person o2) {        int r = Integer.compare(o1.height, o2.height);        return r == 0 ? o1.name.compareTo(o2.name) : r;    }}public static void test(Set<Person> ps) {    ps.add(new Person("Ann", 150));    ps.add(new Person("Jane", 150));    ps.add(new Person("John", 180));    System.out.println(ps.getClass().getName());    for (Person p : ps) {        System.out.println(" " + p);    }}public static void main(String[] args) {    test(new HashSet<Person>());    test(new TreeSet<Person>());    test(new TreeSet<>(new PComparator1()));    test(new TreeSet<>(new PComparator2()));}}

result:

java.util.HashSet Person{Ann, height = 150} Person{John, height = 180} Person{Jane, height = 150}java.util.TreeSet Person{Ann, height = 150} Person{John, height = 180}java.util.TreeSet Person{Ann, height = 150} Person{John, height = 180}java.util.TreeSet Person{Ann, height = 150} Person{Jane, height = 150} Person{John, height = 180}

别人的解答,总结一下有下面几点
1、首先,HashSet会根据hash code排序,
而TreeSet则是与equals、hash code没有关系,
它只会按照class本身的compare来决定
2、第一点即解释了问题的输出结果
3、由上面的例子更能提醒我们,最好是使
(x.compareTo(y)==0) == (x.equals(y))
否则可能会出现一些意想不到的错误。

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 上校夫人 上校鸡块 上校逼婚 与上校同枕 上校的小娇妻 上校的小夫人 上校是什么级别 随身空间上校夫人 腹黑总裁上校妻 冷上校诱宠火爆妻 上校他体力太好 冷情总裁的上校夫人 叶可岚和上校完整篇 腹黑上校前妻好甜想吃 上校闪婚宠妻无限 上校相当于公务员什么级别 上校的临时新娘 上校夫人月七儿 上梁吉利话顺口溜大全 上梁不正下梁歪 狱中上梁王书 濠梁之上 上梁 上梁对联 淮上喜会梁州故人 上梁贺词 上梁不下下梁歪 上梁不正下梁歪打一生肖 上梁不正下梁歪下一句 农村老家上梁顺口溜10字 农村上梁吉言顺口溜 梁非凡用红酒上顾烟 鲁班上梁四言八句 农村上梁简单的吉利话 上梁四言八句大全 上梁不正下梁歪是什么意思 上梁吉言好词好句 上翻梁钢筋构造示意图 上梁不正by秦三见 连续梁