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( )方法是子类的方法,且子类的属性的初始化过程发生在父类构造方法之后。

  • 相关阅读:
    WeGame 上线,打造一个wegame游戏的良性游戏圈子
    H5 视频作为背景 source src改变后 循环播放的问题笔记
    windows无法安装到这个磁盘具有mbr分区表
    jquery的扩展:编写好代码封装起来供他人使用
    获取 HTML data-*属性的值( 文章列表页面,存储文章id 为读取详细页面
    max(min)-device-width和max(min)-width的区别
    git 红色标出没明白
    git-flow 的工作流程
    会计服务平台 使用条款
    若依:设置跨域配置位置和图片中框出代码
  • 原文地址:https://www.cnblogs.com/TimerHotel/p/java_newObject.html
Copyright © 2011-2022 走看看