zoukankan      html  css  js  c++  java
  • 2020年8月16日 类初始化练习

    package com.atguigu.test05.exer1;
    
    /*
     * (1)Father类的类初始化
     * ①类变量显式赋值:这里没有
     * ②静态代码块
     *         System.out.println("(1)父类的静态代码块");
     * (2)Son类的类初始化
     * ①类变量显式赋值:这里没有
     * ②静态代码块
     *         System.out.println("(4)子类的静态代码块");
     * 
     * (3)执行Father类的是实参初始化方法<init>()
     * ①非静态成员变量的显式赋值:这里没有
     * ②非静态代码块:
     *         System.out.println("(2)父类的非静态代码块");
     * ③父类的无参构造
     *         System.out.println("(3)父类的无参构造");
     * 
     * (4)执行Son类的实例初始化方法<init>()
     * ①非静态成员变量的显式赋值:这里没有
     * ②非静态代码块:
     *         System.out.println("(5)子类的非静态代码块");
     * ③子类的无参构造
     *         System.out.println("(6)子类的无参构造");
     */
    public class TestExer1 {
        public static void main(String[] args) {
            Son son = new Son();
        }
    }
    class Father{
        static{
            System.out.println("(1)父类的静态代码块");
        }
        {
            System.out.println("(2)父类的非静态代码块");
        }
        Father(){
            System.out.println("(3)父类的无参构造");
        }
    }
    class Son extends Father{
        static{
            System.out.println("(4)子类的静态代码块");
        }
        {
            System.out.println("(5)子类的非静态代码块");
        }
        Son(){
            System.out.println("(6)子类的无参构造");
        }
    }
    package com.atguigu.test05.exer2;
    
    /*
     * (1)Fu类的类初始化
     * ①类变量显式赋值:
     *         i = getNum("(1)i");
     *         public static int getNum(String str){
                print(str);
                    public static void print(String str){
                        System.out.println(str + "->" + i);            (1)i -> 0(默认值)
                    }
                return ++i;                                        i=1
            }
     * ②静态代码块
     *     static{
            print("(3)父类静态代码块");
                public static void print(String str){
                    System.out.println(str + "->" + i);              (3)父类静态代码块 -> 1
                }
        }
     * (2)Zi类的类初始化
     * ①类变量显式赋值:
     *       k = getNum("(6)k");
         *     
            public static int getNum(String str){
                print(str);
                    public static void print(String str){
                        System.out.println(str + "->" + k);        (6)k -> 0(默认值)
                    }
                return ++k;                                    k=1
            }
     * ②静态代码块
     *     static{
            print("(8)子类静态代码块");
                public static void print(String str){
                    System.out.println(str + "->" + k);        (8)子类静态代码块 -> 1
                }
        }    
     * 
     * (3)执行Fu类的是实参初始化方法<init>()
     * ①非静态成员变量的显式赋值:
     *         j = getNum("(2)j");
         *     
            public static int getNum(String str){
                print(str);
                        public static void print(String str){
                            System.out.println(str + "->" + i);  (2)j -> 1
                        }
                return ++i;                                i=2
            }
     * ②非静态代码块:
     *     {
            print("(4)父类非静态代码块,又称为构造代码块");
                    public static void print(String str){
                        System.out.println(str + "->" + i);  (4)父类非静态代码块,又称为构造代码块 -> 2
                    }
        }    
     * ③父类的无参构造
     *    Fu(){
            print("(5)父类构造器");
                    public static void print(String str){
                        System.out.println(str + "->" + i);  (5)父类构造器 -> 2
                    }
        }         
     * 
     * (4)执行Zi类的实例初始化方法<init>()
     * ①非静态成员变量的显式赋值:
     *      h = getNum("(7)h");
    
        public static int getNum(String str){
            print(str);
                 *     public static void print(String str){
                        System.out.println(str + "->" + k);   (7)h ->1
                    }
            return ++k;                                    k=2
        }
     * 
     * ②非静态代码块:
     *     {
            print("(9)子类非静态代码块,又称为构造代码块");
                public static void print(String str){
                        System.out.println(str + "->" + k);   (9)子类非静态代码块,又称为构造代码块 ->2
                }
        }    
     * ③子类的无参构造
     *     Zi(){
            print("(10)子类构造器");
                public static void print(String str){
                        System.out.println(str + "->" + k);   (10)子类构造器 ->2
                }
        }    
     */
    public class TestExer2 {
        public static void main(String[] args) {
            Zi zi = new Zi();
        }
    }
    class Fu{
        private static int i = getNum("(1)i");
        private int j = getNum("(2)j");
        static{
            print("(3)父类静态代码块");
        }
        {
            print("(4)父类非静态代码块,又称为构造代码块");
        }
        Fu(){
            print("(5)父类构造器");
        }
        public static void print(String str){
            System.out.println(str + "->" + i);
        }
        public static int getNum(String str){
            print(str);
            return ++i;
        }
    }
    class Zi extends Fu{
        private static int k = getNum("(6)k");
        private int h = getNum("(7)h");
        static{
            print("(8)子类静态代码块");
        }
        {
            print("(9)子类非静态代码块,又称为构造代码块");
        }
        Zi(){
            print("(10)子类构造器");
        }
        public static void print(String str){
            System.out.println(str + "->" + k);
        }
        public static int getNum(String str){
            print(str);
            return ++k;
        }
    }
    package com.atguigu.test05.exer3;
    
    /*
     * (1)类初始化
     * ①静态代码块
     * static{
            i = 100;//可以
            MyClass.i++;//        i=101
            System.out.println("(1)静态代码块 i=" + MyClass.i);  //(1)静态代码块 i=101
        }
        ②静态变量的显示赋值
        i = getNum("(4)i");
    
        public static int getNum(String str){
            print(str);
                public static void print(String str){
                    System.out.println(str + "->" + i);  //(4)i-》101
                }
            return ++i;                                i=102
        }
     * (2)实例初始化
     * ①非静态代码块
     * {
            j = 100;
            this.j++;            j=101
            System.out.println("(2)构造代码块j=" + this.j);   //(2)构造代码块j=101
            //构造代码块:就是非静态代码块,因为它总是和构造器一起执行,所以就把它叫做构造代码块
        }
      ②非静态变量的显式赋值
         j = getNum("(5)j");
    
        public static int getNum(String str){
            print(str);
                 public static void print(String str){
                    System.out.println(str + "->" + i);        //(5)j->102
                }
            return ++i;                                    i=103
        }
        
       ③构造器
         MyClass(){
            j = 200;
            j++;                j=201
            System.out.println("(3)构造器j=" + j);            //(3)构造器j=201
        }
     */
    public class TestExer3 {
        public static void main(String[] args) {
            MyClass obj = new MyClass();
        }
    }
    class MyClass{
        static{
            i = 100;//可以
            MyClass.i++;//stackoverflow
            System.out.println("(1)静态代码块 i=" + MyClass.i);
        }
        {
            j = 100;
            this.j++;
            System.out.println("(2)构造代码块j=" + this.j);
        }
        MyClass(){
            j = 200;
            j++;
            System.out.println("(3)构造器j=" + j);
        }
        private static int i = getNum("(4)i");
        private int j = getNum("(5)j");
        
        public static void print(String str){
            System.out.println(str + "->" + i);
        }
        public static int getNum(String str){
            print(str);
            return ++i;
        }
    }
    package com.atguigu.test05.exer3_0;
    
    /*
     * (1)以下代码是否可以编译通过
     *         不能
     * (2)如果能,运行结果是什么
     * (3)如果不能怎么改
     * 第一种修改方法:把声明挪到前面;
     * 第二种修改方法:看TestExer3
     */
    /*public class TestExer3_0 {
        public static void main(String[] args) {
            MyClass obj = new MyClass();
        }
    }
    class MyClass{
        static{
            i++;//声明i在后面
            System.out.println("(1)静态代码块 i=" + i);
        }
        {
            j = 100;
            j++;
            System.out.println("(2)构造代码块j=" + j);
        }
        MyClass(){
            j = 200;
            j++;
            System.out.println("(3)构造器j=" + j);
        }
        private static int i = getNum("(4)i");
        private int j = getNum("(5)j");
        
        public static void print(String str){
            System.out.println(str + "->" + i);
        }
        public static int getNum(String str){
            print(str);
            return ++i;
        }
    }
    */
    package com.atguigu.test05.exer4;
    
    /*
     * 对于T来说,就是完成类初始化
     * 
     * 创建对象,调用类的实例初始化<init>()或<init>(String str)
     * 
     * 
     * (1)静态变量的显式赋值
     *         k = 0;
            t1 = new T("t1");
                <init>(String str)
                    ①j = print("j");
                            public static int print(String str){
                                System.out.println((++k) + ":" + str + "  i=" + i + "  n=" + n);  1:j i=0 n=0
                                ++n;                                    n=1  k=1
                                return ++i;                                i=1
                            }
                    ②    {
                        print("构造块");
                            public static int print(String str){
                                System.out.println((++k) + ":" + str + "  i=" + i + "  n=" + n);  2:构造块 i=1 n=1
                                ++n;                                    n=2  k=2
                                return ++i;                                i=2
                            }
                    }
                    ③public T(String str){
                        System.out.println((++k) + ":" + str + "  i=" + i + "  n=" + n);      3:t1  i=2  n=2    
                        ++n;                                            n=3  k=3
                        ++i;                                            i=3
                    }
     *         t2 = new T("t2");
                <init>(String str)
                    ①j = print("j");
                            public static int print(String str){
                                System.out.println((++k) + ":" + str + "  i=" + i + "  n=" + n);  4:j i=3 n=3
                                ++n;                                    n=4  k=4
                                return ++i;                                i=4
                            }
                    ②    {
                        print("构造块");
                            public static int print(String str){
                                System.out.println((++k) + ":" + str + "  i=" + i + "  n=" + n);  5:构造块 i=4 n=4
                                ++n;                                    n=5  k=5
                                return ++i;                                i=5
                            }
                    }
                    ③public T(String str){
                        System.out.println((++k) + ":" + str + "  i=" + i + "  n=" + n);      6:t2  i=5  n=5    
                        ++n;                                            n=6  k=6
                        ++i;                                            i=6
                    }
         i = print("i");
                 public static int print(String str){
                    System.out.println((++k) + ":" + str + "  i=" + i + "  n=" + n);  7:i  i=6 n=6
                    ++n;                                    n=7  k=7
                    return ++i;                                i=7
                }
        n = 99;
     * (2)静态代码块
     *     static{
            print("静态块");
            public static int print(String str){
                    System.out.println((++k) + ":" + str + "  i=" + i + "  n=" + n);  8:静态块   i=7 n=99
                    ++n;                                    n=100  k=8
                    return ++i;                                i=8
            }
        }
     */
    public class T {
        public static int k = 0;
        public static T t1 = new T("t1");
        public static T t2 = new T("t2");
        public static int i = print("i");
        public static int n = 99;
        
        public int j = print("j");
        {
            print("构造块");
        }
    
        static{
            print("静态块");
        }
        public T(String str){
            System.out.println((++k) + ":" + str + "  i=" + i + "  n=" + n);
            ++n;
            ++i;
        }
        public static int print(String str){
            System.out.println((++k) + ":" + str + "  i=" + i + "  n=" + n);
            ++n;
            return ++i;
        }
        public static void main(String[] args) {
        
        }
    } 
    *  (1)Father类的类初始化
     * ①类变量显式赋值
     * ②静态代码块
     * (2)Son类的类初始化
     * ①类变量显式赋值
     * ②静态代码块
     * (3)执行Father类的是实例初始化方法<init>()
     * ①非静态成员变量的显式赋值:这里没有
     * ②非静态代码块:
     * ③父类的无参构造
     * (4)执行Son类的实例初始化方法<init>()
     * ①非静态成员变量的显式赋值:这里没有
     * ②非静态代码块:
     * ③子类的无参构造
  • 相关阅读:
    DNS智能解析的搭建与配置
    使用dnsmasq快速搭建内网DNS
    安装Fedora 21工作站后要做的10件事情
    MySQL + KeepAlived + LVS 单点写入主主同步高可用架构实验
    SOC-EDS之DS5安装和破解
    vs2015安装与卸载
    opencv实现的图像缩放
    基于Haar+Adaboost的人脸识别
    win10+python3.7+Anaconda3+CUDA10.0+cuDNN7.5+tensorflow_gpu1.13.1+opencv4.1.0 教程(最新)
    图片合成视频
  • 原文地址:https://www.cnblogs.com/douyunpeng/p/13514579.html
Copyright © 2011-2022 走看看