动手动脑①

 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的更多相关文章

随机推荐

  1. Spring Boot + Elasticsearch 使用示例

    本文分别使用 Elasticsearch Repository 和 ElasticsearchTemplate 实现 Elasticsearch 的简单的增删改查 一.Elastic Stack El ...

  2. 第3.3节 强大的Python列表

    一. 列表切片操作补充 列表切片支持所有序列切片的方法,以倒序切片和步长大于1的情况再举例验证一下: l=[1,2,3,4,5] l[::2] #结果[1, 3, 5] l[-1::2] #结果[5] ...

  3. Python中__new__方法为什么有人说是构造方法?有什么作用?

    __new__方法是Python新式类引入的,通常用于控制生成一个新实例的过程.它是类级别的静态方法,是在创建实例对象前执行,如果自定义类中没有重写该方法,则Python自动调用父类的方法,当所有父类 ...

  4. 深入理解C#中的异步(一)——APM模式EAP模式

    深入理解C#中的异步(一)--APM模式EAP模式 目录 深入理解C#中的异步(一)--APM模式EAP模式 1 使用异步编程的原因 2 异步编程模式 2.1 APM模式 2.1.1 APM模式示例代 ...

  5. Java 中的语法糖,真甜。

    我把自己以往的文章汇总成为了 Github ,欢迎各位大佬 star https://github.com/crisxuan/bestJavaer 我们在日常开发中经常会使用到诸如泛型.自动拆箱和装箱 ...

  6. VMware虚拟机下Centos8 设置静态IP地址

    缘起 我们在平时学习Redis.Nginx等分布式微服务的组件的时候,无法避免的需要用到Linux操作系统,而Linux操作系统的主机来源差不多就三种情况: 真实物理机 阿里云等云服务器 利用虚拟机 ...

  7. Mysql为什么使用b+树,而不是b树、AVL树或红黑树?

    首先,我们应该考虑一个问题,数据库在磁盘中是怎样存储的?(答案写在下一篇文章中) b树.b+树.AVL树.红黑树的区别很大.虽然都可以提高搜索性能,但是作用方式不同. 通常文件和数据库都存储在磁盘,如 ...

  8. JavaScript:防抖与节流

    ①防抖: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  9. 数组问题:a[i][j] 和 a[j][i] 有什么区别?

    本文以一个简单的程序开头--数组赋值: int LEN = 10000; int[][] arr = new int[LEN][LEN]; for (int i = 0; i < LEN; i+ ...

  10. DG修改SYS用户密码(ORA-16810,ORA-01017)

    修改主库PROD1密码后,查看configuration状态看到以下报错: [oracle@edgzrip1-PROD1 ~]$ dgmgrl sys/oracleDGMGRL for Linux: ...