Head First Java 十大遗珠之憾

2016-08-10  本文已影响0人  促集

十大遗珠之憾

10 位操作

9 不变性

8 断言

7 块区域

6 链接的调用

StringBuffer sb = new StringBuffer(“spring”);
sb = sb.delete(3,6).insert(2,”umme”).deleteCharAt(1);
System.out.println(“sb = “ + sb);
// result is sb = summer
等同于:
sb = sb.delete(3,6);
sb = sb.insert(2,”umme”);
sb = sb.deleteCharAt(1);

5 Anonymous和Static Nested Classes

public class FooOuter {
    static class BarInner {
        void sayIt() {
            System.out.println(“method of a static inner class”);
        }
    }
}

class Test {
    public static void main(String[] args) {
        FooOuter.BarInner foo = new FooOuter.BarInner();
        foo.sayIt();
    }
}
import java.awt.event.*;
import javax.swing.*;
public class TestAnon {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JButton button = new JButton(“click”);
        frame.getContentPane().add(button);
        // button.addActionListener(quitListener);
        //此处的ActionListener其实是一个接口,而我们不能创建接口的实例!但这种情况的语法就是这样
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                System.exit(0);
            }
        }) 
    }
}

4 存取权限和存取修饰符(谁可以看到什么)

3 String和StringBuilder的方法

2 多维数组

1 枚举(又称为枚举类型或Enum)

public class HfjEnum {
    enum Names {
        JERRY(“lead guitar”) {
            public String sings() {
                return“plaintively”;
            }
        },
        BOBBY(“rhythm guitar”) {
            public String sings() {
                return“hoarsely”;
            }
        },
        PHIL(“bass”);

        private String instrument;

        Names(String instrument) {
            this.instrument = instrument;
        }

        public String getInstrument() {
            return this.instrument;
        }

        public String sings() {
            return“occasionally”;
        }
    }

    public static void main(String[] args) {
        for (Names n : Names.values()) {
            System.out.print(n);
            System.out.print(“, instrument:“+n.getInstrument());
            System.out.println(“, sings:“+n.sings());
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读