zoukankan      html  css  js  c++  java
  • 对象实例化过程

    对象实例化过程:

    1.看类是否已加载,未加载的话先初始化类。

    2.在堆内存中分配空间。

    3.初始化父类的属性

    4.初始化父类的构造方法

    5.初始化子类的属性

    6.初始化子类的构造方法

    实例:

    package com.xm.load;
    
    public class Animal {
    
        static String str = "I`m a animal!";
    
        public String str1 = "I`m 10 years old @Animal";
    
        static {
            System.out.println(str);
            System.out.println("加载父类静态代码块");
        }
    
        static void doing() {
            System.out.println("This ia a Animal!");
        }
    
        public Animal() {
            doing();
            System.out.println(str1);
            System.out.println("Animal 对象实例化");
        }
    
    
    }
    
    class Dog extends Animal{
    
        static String str = "I`m a dog!";
    
        private String str1 = "I`m 10 years old @Dog";
    
        static {
            System.out.println(str);
            System.out.println("加载子类静态代码块");
        }
    
        static void doing() {
            System.out.println("This ia a dog!");
        }
    
        public Dog() {
            doing();
            System.out.println(str1);
            System.out.println("Dog 对象实例化");
        }
    
    
        public static void main(String[] args) {
            new Dog();
        }
    }
    
    

    运行结果:

    I`m a animal!

    加载父类静态代码块

    I`m a dog!

    加载子类静态代码块

    This ia a Animal!

    I`m 10 years old @Animal

    Animal 对象实例化

    This ia a dog!

    I`m 10 years old @Dog

    Dog 对象实例化

    我们来看一下,重写过的父类方法,在加载父类时的情况。

    实例:

    package com.xm.load;
    
    public class Animal {
    
        static String str = "I`m a animal!";
    
        public String str1 = "I`m 10 years old @Animal";
    
        static {
            System.out.println(str);
            System.out.println("加载父类静态代码块");
        }
    
        static void doing() {
            System.out.println("This ia a Animal!");
        }
    
        public void todoing() {
            System.out.println(str1);
            System.out.println("加载子类的重写方法");
        }
    
        public Animal() {
            doing();
            todoing();
            System.out.println("Animal 对象实例化");
        }
    
    
    }
    
    class Dog extends Animal{
    
        static String str = "I`m a dog!";
    
        private String str1 = "I`m 10 years old @Dog";
    
        static {
            System.out.println(str);
            System.out.println("加载子类静态代码块");
        }
    
        static void doing() {
            System.out.println("This ia a dog!");
        }
    
        public void todoing() {
            System.out.println(str1);
            System.out.println("加载子类的重写方法");
        }
    
        public Dog() {
            doing();
            System.out.println("Dog 对象实例化");
        }
    
    
        public static void main(String[] args) {
            new Dog();
        }
    }
    
    

    结果:

    I`m a animal!

    加载父类静态代码块

    I`m a dog!

    加载子类静态代码块

    This ia a Animal!

    null

    加载子类的重写方法

    Animal 对象实例化

    This ia a dog!

    Dog 对象实例化

    由此可见,null代表着加载父类构造方法时,调用的todoing( )方法是子类的方法,且子类的属性的初始化过程发生在父类构造方法之后。

  • 相关阅读:
    SQL server 2008 建立新用户
    2021.3.22-刷题 (移位)
    2021.3.17刷题-分割回文串
    2021.3.16 刷题--组合总和||(一种组合下元素不可重复选取)
    2021.3.15刷题-组合总和(元素可重复选取)
    2021.3.14刷题-设计哈希映射
    2021.3.13刷题-用拉链法设计哈希集合
    2021.3.12刷题-验证二叉树的前序序列化
    2021.3.11刷题-(删除二叉搜索树中的节点)
    2021.2.28刷题 回溯-电话字母组合
  • 原文地址:https://www.cnblogs.com/TimerHotel/p/java_newObject.html
Copyright © 2011-2022 走看看