java程序设计初识

1 构造器不允许引用实例变量


2   创建SubClass对象会输出: this.x 0 == 0

public class SuperClass {
     public SuperClass() {
         this.toOverride();
     }
     
     public void toOverride() {

     }
}

class SubClass {
     private int x = 23;
     public SubClass() {
         x = 90;
     }
     public void toOverride() {
         System.out.println("this.x: " + this.x + "==" + "0");
    }
}

3 对象的Clone需要注意final 空白字段

4 判断奇偶的方法 i & 1 == 1 或 i % 2 != 0, 不可以用  i % 2 == 1! 

5 泛型说明----不要在代码中使用原始类型的两个特例:

  • 字面值常量,如List.class、String[].class和int.class是允许的,但是List<String>.class和List<?>.class错误
  • 与instanceof有关,在参数化类型或泛型而非无限制通配符上使用instanceof操作符是非法的
  • // 错误的java片段
    //! if (o instanceof List<String>) {
    //}
    //! if (o instanceof List<E>) {
    //}
    public class GenericJava {
        public static void main(String[] args) {
            Object o = null;
            if (o instanceof Set<?>) {
                
            }
            if (o instanceof Set) {
                
            }
        }
    }
    这是正确的

上面两个特例都与泛型信息在运行时擦除有关

6  泛型定义List<E>中的E为 类型参数,List<String>中String为实际类型参数,每个泛型List<E>定义一组参数化的类型如List<String>,最后是原生态类型List; Set<?>与Set可以互相赋值,但是Set<?>只能插入null,而Set可以插入任意对象








猜你喜欢

转载自blog.csdn.net/menghu07/article/details/70153864