zoukankan      html  css  js  c++  java
  • 类加载顺序(2)

    package loader;
    
    public class Animal{
        public String test="AnimalField";
        public static String testStatic="AnimalStaticField";
        
        public Animal(){
            System.out.println("Animal的构造方法");
        }
        public void testA(){
            System.out.println("Animal的普通方法");
        }
        public static void testStatic(){
            System.out.println("Animal的静态方法");
        }
        
        {
            this.testA();
            System.out.println("Animal的普通块");
        }
        static{
            Animal.testStatic();
            System.out.println("Animal的静态块");
        }
        
    
    }
    package loader;
    
    public class Person extends Animal{
        public String test="PersonField";
        public static String testStatic="PersonStaticField";
        
        public Person(){
            System.out.println("Person的构造方法");
        }
        public void testP(){
            System.out.println("Person的普通方法");
        }
        public static void testStatic(){
            System.out.println("Person的静态方法");
        }
        
        {
            this.testP();
            System.out.println("Person的普通块");
        }
        
        static{
            Person.testStatic();
            System.out.println("Person的静态块");
        }
        
    
    }
    package loader;
    
    public class Test {
    
        
        /**类加载顺序
         * 二:由子类调用父类构造方法的情况
         * 1.创建子类对象(未完成)
         * 2.先调用父类的构造方法(未完成)
         * 3.加载父类的静态块>加载子类的静态块>加载父类的普通块>完成父类的构造函数>加载子类的普通块>完成子类构造函数
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Person p=new Person();
            
        }
    
    }

     输出顺序:

    Animal的静态方法
    Animal的静态块
    Person的静态方法
    Person的静态块
    Animal的普通方法
    Animal的普通块
    Animal的构造方法
    Person的普通方法
    Person的普通块
    Person的构造方法

  • 相关阅读:
    AWS EC2服务器的HTTPS负载均衡器配置过程
    大数据技术Hadoop笔试题
    网上找的hadoop面试题目及答案
    360全景图three.js
    360全景图three.js与Photo-Sphere-Viewer-master 3D全景浏览开发
    @font-face 字体
    scss语法
    6.事件
    5.回调函数
    4.querystring属性
  • 原文地址:https://www.cnblogs.com/hebiao/p/12450643.html
Copyright © 2011-2022 走看看