第五章 初始化与清理

2013年6月15日 星期六 16时30分20秒


5.1 用构造器确保初始化
      public class Rock {

                public Rock() {
                        super();
                }

        }

5.2 方法重载
        5.2.1 区分重载方法
                通过参数类型来区分

        5.2.2 设计基本类型的重载
                如果传入的实际参数类型较大,就得通过类型转换来执行窄化转换。
                如果传入的数据类型(实际参数类型)小于方法中声明的形式参数类型,实际数据类型就会被提升。
       
        5.2.3 以返回值区分重载方法
                根据返回值来区分重载方法是行不通的。

5.3 默认构造器
        如果已经定义了一个构造器,编译器就不会帮你自动创建默认构造器了。

5.4 this关键字
        5.4.1 在构造器中调用构造器
       
        5.4.2 static含义
5.5 清理,终结处理和垃圾回收
        5.5.1 垃圾回收只与内存有关
        5.5.2 你必须实施清理
        5.5.3 终结条件
        5.5.4 垃圾回收器如何工作
                垃圾回收器的原理分类: 1.引用计数
                        “标记--扫清”,“停止--复制”
                JIT(Java In Time )“即时”编译技术
                Lazy evaluation 惰性评估
                Java HotSpot技术
5.6 成员初始化
        5.6.1 指定初始化

5.7 构造初始化
        5.7.1 初始化顺序
        5.7.2 静态数据的初始化
        5.7.3 显式的静态初始化 (静态子句 或 静态块)
                        public class Spoon {
                        static int i;
                        static int a;
                        static{
                                i=47;
                                a=56;
                        }
                     }
        5.7.4 非静态实例初始化
               
5.8 数组初始化
                        class other{
                        public static void main(String[] args){
                                for(String s:args){
                                System.out.print(s+" ");
                                }
                        }
                     }

                     public class DynamicArray {
                        public static void main(String[] args){
                        other.main(new String[]{"a","b","c"});
                        }
                     }

        5.8.1 可变参数列表
                        package chapter5;

                     class A{}

                     public class VarArg {
                        //before SE5
                        /*static void printArry(Object[] args){
                          for(Object b:args){
                          System.out.print(b+" ");
                        }
                        System.out.println();       
                       }*/
       
                        //after SE5
                       static void printArry(Object... args){
                        for(Object b:args){
                                System.out.print(b+" ");
                        }
                        System.out.println();       
                        }
       
                        public static void main(String[] args){
                        //VarArg.printArry(new String[]{"one","two","three"});
                        //VarArg.printArry(new Object[]{new A(),new A(),new A()});
                        VarArg.printArry("one","two","three");
                        }
                }
       
5.9 枚举类型
        枚举和switch的结合使用
       package chapter5;
        public class SimpleEnum {
        public enum Spiciness{
                NOT,MILD,MEDIUM,HOT,FLAMING
        }
       
        Spiciness degree;
        public SimpleEnum(Spiciness degree){
                this.degree=degree;
        }

        public void describe(){
                System.out.println("This SimpleEnum is ");
                switch(degree){
                case NOT: System.out.println("This SimpleEnum is "+degree);break;
                case MILD: System.out.println("This SimpleEnum is "+degree);break;
                case MEDIUM: System.out.println("This SimpleEnum is "+degree);break;
                case HOT: System.out.println("This SimpleEnum is "+degree);break;
                case FLAMING: System.out.println("This SimpleEnum is "+degree);break;
                default: System.out.println("This SimpleEnum is default");
                }
        }
       
        public static void main(String[] args){
                Spiciness Hot=Spiciness.HOT;
                /*System.out.print(Hot);
                for(Spiciness s: Spiciness.values()){
                        System.out.println(s+" "+s.ordinal());
                }*/
                SimpleEnum s= new SimpleEnum(Spiciness.HOT),
                                   s1=new SimpleEnum(Spiciness.MEDIUM),
                                   s3=new SimpleEnum(Spiciness.FLAMING);
                s.describe();
                s1.describe();
                s3.describe();
               
        }
       
        }

5.10 总结

你可能感兴趣的:(初始化,构造器)