JavaDailyReports10_07
动手动脑①

1 package test_1;
2
3 public class Test {
4
5 public static void main(String[] args) {
6 // TODO 自动生成的方法存根
7 Foo obj1=new Foo();
8 Foo obj2=new Foo();
9 System.out.println(obj1==obj2);
10 }
11 }
12 class Foo{
13 int value=100;
14 }

动手动脑②:类字段的初始化顺序:
1 package test_1;
2
3 public class Test {
4
5 public static void main(String[] args) {
6 // TODO 自动生成的方法存根
7 InitializeBlockClass obj=new InitializeBlockClass();
8 System.out.println(obj.getField());
9
10 obj=new InitializeBlockClass(300);
11 System.out.println(obj.getField());
12 }
13 }
14 class InitializeBlockClass{
15
16 {
17 setField(200); //代码块定义值是200
18 }
19 private int field=100; //定义初始值是 100
20 InitializeBlockClass(int value) { //传入参数是300
21 this.setField(value);
22 }
23 InitializeBlockClass() {
24 }
25 public int getField() {
26 return field;
27 }
28 public void setField(int field) {
29 this.field = field;
30 }
31 }

1 package test_1;
2
3 public class Test {
4
5 public static void main(String[] args) {
6 // TODO 自动生成的方法存根
7 InitializeBlockClass obj=new InitializeBlockClass();
8 System.out.println(obj.getField());
9
10 obj=new InitializeBlockClass(300);
11 System.out.println(obj.getField());
12 }
13 }
14 class InitializeBlockClass{
15 private int field=100; //定义初始值是 100
16 {
17 setField(200); //代码块定义值是200
18 }
19
20 InitializeBlockClass(int value) { //传入参数是300
21 this.setField(value);
22 }
23 InitializeBlockClass() {
24 }
25 public int getField() {
26 return field;
27 }
28 public void setField(int field) {
29 this.field = field;
30 }
31 }

1 package test_1;
2
3 class Root
4 {
5 static
6 {
7 System.out.println("Root的静态初始化块");
8 }
9
10 {
11 System.out.println("Root的普通初始化块");
12 }
13
14 public Root()
15 {
16 System.out.println("Root的无参数的构造器");
17 }
18 }
19 class Mid extends Root
20 {
21 static
22 {
23 System.out.println("Mid的静态初始化块");
24 }
25 {
26 System.out.println("Mid的普通初始化块");
27 }
28 public Mid()
29 {
30 System.out.println("Mid的无参数的构造器");
31 }
32 public Mid(String msg)
33 {
34 //通过this调用同一类中重载的构造器
35 this();
36 System.out.println("Mid的带参数构造器,其参数值:" + msg);
37 }
38 }
39 class Leaf extends Mid
40 {
41 static
42 {
43 System.out.println("Leaf的静态初始化块");
44 }
45 {
46 System.out.println("Leaf的普通初始化块");
47 }
48 public Leaf()
49 {
50 //通过super调用父类中有一个字符串参数的构造器
51 super("Java初始化顺序演示");
52 System.out.println("执行Leaf的构造器");
53 }
54
55 }
56
57 public class TestStaticInitializeBlock
58 {
59 public static void main(String[] args)
60 {
61 new Leaf();
62
63
64 }
65 }
规律总结:
一个父类后继承两个子类,调用顺序为:总顺序先static代码块后普通代码块 最后构造代码块,先父类后子类,先无参构造,后带参构造!
*****************************************************
动手动脑④:

1 package test_1;
2
3 public class StaticStudy {
4 static int staticnum=1;
5 private int num;
6 public static void display() {
7 StaticStudy test=new StaticStudy();
8 System.out.println("在静态方法中输出静态变量:"+test.staticnum);
9 System.out.println("在静态方法中输出非静态变量:"+test.num);
10 }
11 public int getNum() {
12 return num;
13 }
14 public void setNum(int num) {
15 this.num = num;
16 }
17 public StaticStudy(int num) {
18 this.num = num;
19 }
20 public StaticStudy() {
21
22 }
23 public static void main(String[] args) {
24 // TODO 自动生成的方法存根
25 StaticStudy test0=new StaticStudy(100);
26 test0.display();
27 }
28
29 }

//只需要在静态变量里实例化一个本类的对象,通过这个对象取用本对象的数据成员!
动手动脑⑤:
Java中的包装类
1 package test_1;
2
3 public class StrangeIntegerBehavior
4 {
5 public static void main(String[] args)
6 {
7 Integer i1=100;
8
9 Integer j1=100;
10
11 System.out.println(i1==j1);
12
13 Integer i2=129;
14
15 Integer j2=129;
16
17 System.out.println(i2==j2);
18
19 }
20 }


-128 --- 127 范围内是相等的,超出范围一定会重新生成对象!所以地址值就会不同!
JavaDailyReports10_07的更多相关文章
随机推荐
- Spring Cloud Alibaba 初体验(四) Sentinel
一.Sentinel 下载与运行 本文使用 Sentinel 1.7.1:https://github.com/alibaba/Sentinel/releases 使用自定义端口 8089 运行 Se ...
- python多进程通讯踩坑记
# 错误代码如下 from multiprocessing import Process from queue import Queue # 正确代码应该是这样,Process和Queue都来自mul ...
- keil/MDK代码配色
个人配色方案,仅供参考.
- 第8.25节 Python风格的__getattribute__属性访问方法语法释义及使用
一. 引言 在<第8.13节 Python类中内置方法__repr__详解>老猿介绍了在命令行方式直接输入"对象"就可以调用repr内置函数或__repr__方法查看对 ...
- PyQt(Python+Qt)学习随笔:树型部件QTreeWidget中判断项是否首列跨所有列展示的isFirstItemColumnSpanned方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在前面<PyQt(Python+Qt)学习随笔:QTreeWidgetItem项是否首列跨所有 ...
- PyQt(Python+Qt)学习随笔:QListWidget的addItem方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在QListWidget对象中,增加一个项的方法是调用addItem方法,addItem方法有2种重 ...
- Pentaho Report Designer 入门教程(一)
PentahoReport Designer 入门教程 采用Pentaho Report Designer5.1版本,也是最新的版本. 一. 安装和介绍 介绍部分内容略,首先安装jdk,并 ...
- PHP代码审计分段讲解(13)
代码审计分段讲解之29题,代码如下: <?php require("config.php"); $table = $_GET['table']?$_GET['table']: ...
- Ambari HDP集群搭建全攻略
世界上最快的捷径,就是脚踏实地,本文已收录[架构技术专栏]关注这个喜欢分享的地方. 最近因为工作上需要重新用Ambari搭了一套Hadoop集群,就把搭建的过程记录了下来,也希望给有同样需求的小伙伴们 ...
- 什么时候使用transition?什么时候使用animation?
不同点: 1. 触发条件不同.transition通常和hover等事件配合使用,由事件触发.animation则和gif动态图差不多,立即播放. 2. 循环. animation可以设定循环次数. ...
