首页 > 编程语言 >Java 静态代码块、代码块、构造方法和多态继承的代码执行顺序

Java 静态代码块、代码块、构造方法和多态继承的代码执行顺序

时间:2023-10-29 12:56:07浏览次数:45  
标签:code Parent 构造方法 代码 System 代码执行 Child println out

  • 测试代码
import lombok.Getter;

public class ExecutionOrder {
    {
        System.out.println("ExecutionOrder code 0");
    }
    static {
        System.out.println("ExecutionOrder static code");
    }
    {
        System.out.println("ExecutionOrder code 2");
    }
    public ExecutionOrder() {
        System.out.println("ExecutionOrder construction");
    }
    public static void main(String[] args) {
        AbsBase child = new Child();
        child.func();
        System.out.println("result: k = " + child.getK());
        System.out.println("result: i, j, k = " + child.i + " - " + child.j + " - " + child.k);
    }

}

abstract class AbsBase {
    int i = 1;
    int j = 2;
    @Getter
    int k;
    {
        System.out.println("AbsBase code 0");
    }
    static {
        System.out.println("AbsBase static code");
    }
    {
        System.out.println("AbsBase code 2");
    }
    public AbsBase() {
        System.out.println("AbsBase construction");
    }
    abstract void func();
}

class Parent extends AbsBase {
    int i = 3;
    int j = 4;
    @Getter
    int k = -1;
    {
        System.out.println("Parent code 0");
    }
    static {
        System.out.println("Parent static code");
    }
    {
        System.out.println("Parent code 1");
    }
    Parent() {
        System.out.println("Parent construction");
    }
    @Override
    public void func() {
        System.out.println("Parent this.getK(): " + this.getK());
        System.out.println("Parent super.getK(): " + super.getK());
        System.out.println("Parent.func(): " + i + " - " + j + " - " + k);
    }
}

class Child extends Parent {
    int i = 5;
    int j = 6;
    @Getter
    int k = -2;
    {
        System.out.println("Child code 0");
    }
    static {
        System.out.println("Child static code");
    }
    {
        System.out.println("Child code 1");
    }
    Child() {
        System.out.println("Child construction");
    }
    @Override
    public void func() {
        System.out.println("Child this.getK(): " + this.getK());
        System.out.println("Child super.getK(): " + super.getK());
        System.out.println("Child.func(): " + i + " - " + j + " - " + k);
    }
}
  • 执行结果
ExecutionOrder static code
AbsBase static code
Parent static code
Child static code
AbsBase code 0
AbsBase code 2
AbsBase construction
Parent code 0
Parent code 1
Parent construction
Child code 0
Child code 1
Child construction
Child this.getK(): -2
Child super.getK(): -1
Child.func(): 5 - 6 - -2
result: k = -2
result: i, j, k = 1 - 2 - 0

Process finished with exit code 0

标签:code,Parent,构造方法,代码,System,代码执行,Child,println,out
From: https://www.cnblogs.com/AardWolf/p/17795752.html

相关文章

  • 无代码平台的表单平台 JAVA开源项目 毕业设计
    https://gf.bilibili.com/item/detail/1104045029为了帮助小白入门Java,博主录制了本项目配套的《项目手把手启动教程》,希望能给同学们带来帮助。一、摘要基于Vue+SpringBoot+MySQL的无代码平台的表单平台,包括了系统数据中心模块,用来存放管理系统通用的模块,另外分别设计了动态类型......
  • 安信可小安派AiPi 代码下载
    安信可小安派AiPi代码下载笔记记录AiPi代码下载(直接使用命令行操作,仅需要Type-C接口线即可)在完成环境搭建,和代码编写前提下,使用Type-C接口线下载代码,当然可以自己使用usb-ttl串口线下载程序,但是感觉麻烦,没有直接一根线舒服。以大佬的基于小安派AiPi-Eye-S1的小霸王工程代码为......
  • JavaScript代码,鼠标放上去显示一张图片
     <!DOCTYPEhtml> <html>  <head>    <metacharset="utf-8">    <title>FirstC</title>     </head>  <body>    <h1 >helloworld</h1>    <inputtype=&q......
  • 代码随想录第四天 | 24. 两两交换链表中的节点 19.删除链表的倒数第N个节点 面试题
    question1:SwapNodesinPairshttps://leetcode.cn/problems/swap-nodes-in-pairs/IwasalittleconfusedatfirstbecauseI'mthinkingwhethershouldIcreatanewhead,butsoonIcameupwiththeideaofcreatpre=Noneandwithan'if-els......
  • 学习笔记7+代码
    一、苏格拉底挑战二、遇见的问题三、实践和代码代码:#include<stdio.h>#include<pthread.h>//线程函数,接受一个void*参数,返回一个void*指针void*thread_function(void*arg){intthread_arg=*((int*)arg);printf("Threadreceivedargument......
  • #深度学习复现Github项目代码流程详细过程
    背景要求:已安装好anaconda及pycharm,这两个的安装可从网上学习安装,教程很多。第一步,在Github上下载项目代码因为第一次运行代码,找一些比较多运行成功的例子来练习,这次我找的是Github上的pix2pixGAN项目的源码,具体路径如下:https://github.com/junyanz/pytorch-CycleGAN-and-pix2......
  • 手把手教你用python一键抢12306火车票(附代码)
    哈喽,哈喽~,一年一度的抢火车票大战正式拉开序幕…然饿大多数人碰到的是这种情况:当你满心期待摩拳擦掌准备抢票的时候,你会发现一票难求!想回趟家真难!那么作为程序猿的你,当然要用程序猿的方式来抢票!下面分享用python来抢票!欢迎关注公众号:每日推荐系列,提供各种免费资源!也可以咨询代码问......
  • vscode 创建代码片段
    1vscode左下角设置>命令面板2输入snippets3选择新建全局代码片段文件4输入文件名称xxx.json(例如:vue-setup-less.json)5设置模板并保存快捷生成模板地址点击跳转......
  • 外卖小程序:技术实现与关键代码
    在当今数字化的餐饮市场中,外卖小程序扮演着重要的角色。这些应用程序通过技术实现点餐、支付和配送,为用户提供方便快捷的服务。下面我们来深入探讨构建外卖小程序的关键技术要点和代码示例。1.前端开发前端是用户与应用程序交互的接口,关键技术包括HTML、CSS和JavaScript。<!--......
  • 构建外卖小程序:技术要点和实际代码
    1.前端开发前端开发涉及用户界面设计和用户交互。HTML、CSS和JavaScript是构建外卖小程序界面的主要技术。<!--HTML结构示例--><header><h1>外卖小程序</h1><!--其他导航元素--></header><main><!--菜单显示--><divclass="menu">......