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的构造方法

  • 相关阅读:
    CF1236B Alice and the List of Presents |数学
    luogu P1832 A+B Problem |背包
    CF832D Misha, Grisha and Underground |LCA
    bzoj1709[Usaco2007 Oct]Super Paintball超级弹珠*
    bzoj3314[Usaco2013 Nov]Crowded Cows*
    bzoj4300绝世好题
    bzoj2101[Usaco2010 Dec]Treasure Chest 藏宝箱*
    bzoj3437小P的牧场
    bzoj2016[Usaco2010]Chocolate Eating*
    bzoj2015[Usaco2010 Feb]Chocolate Giving*
  • 原文地址:https://www.cnblogs.com/hebiao/p/12450643.html
Copyright © 2011-2022 走看看