Java编程思想(Chapter2、4、6)
一切皆对象
用引用操纵对象
存储位置
作用域
{
int x =12;
{
int x = 96; //illegal
}
}
非法,不能隐藏。
{
String s = new String("str");
}
关于值传递还是引用传递的问题
一个方法不能修改一个基本数据类型的参数
一个方法可以改变一个对象参数的状态
一个方法不能让对象参数引用一个新的对象
初始化与清理
初始化顺序
public class Tag {
public Tag(int marker) {
System.out.println("tag"+marker);
}
}
public class Card {
Tag tag = new Tag(1);
public Card() {
System.out.println("card()");
tag3 = new Tag(33);
}
Tag tag2 = new Tag(2);
void f(){
System.out.println("f()");
}
Tag tag3 = new Tag(3);
}
public class Init01 {
public static void main(String[] args) {
Card card = new Card();
card.f();
}
}
Result
tag1
tag2
tag3
card()
tag33
f()
在此基础上,如果还有静态数据时,顺序如何?
public class Bowl {
public Bowl(int marker) {
System.out.println("bowl "+marker);
}
protected void f(int marker){
System.out.println("f "+marker);
}
}
public class Table {
static Bowl b1 = new Bowl(1);
public Table(){
System.out.println("table()");
b2.f(1);
}
void f2(int marker){
System.out.println("f2 "+marker);
}
static Bowl b2 =new Bowl(2);
}
public class Cupboard {
Bowl b3 =new Bowl(3);
static Bowl b4 = new Bowl(4);
Cupboard(){
System.out.println("cup()");
b4.f(2);
}
void f3(int marker){
System.out.println("f3 "+marker);
}
static Bowl b5 = new Bowl(5);
}
public class Test {
public static void main(String[] args) {
System.out.println("create cup in main");
new Cupboard();
System.out.println("create cup in main");
new Cupboard();
t2.f2(1);
t3.f3(1);
}
static Table t2 = new Table();
static Cupboard t3 = new Cupboard();
}
Result
bowl 1
bowl 2
table()
f 1
bowl 4
bowl 5
bowl 3
cup()
f 2
create cup in main
bowl 3
cup()
f 2
create cup in main
bowl 3
cup()
f 2
f2 1
f3 1
数组的初始化
复用类
带参数的构造器
public class C3 extends C2{
public C3(int i) {
super(i);//
System.out.println("c3");
}
public static void main(String[] args) {
new C3(1);
}
}
class C1{
public C1(int i) {
System.out.println("c1");
}
}
class C2 extends C1{
public C2(int i) {
super(i);//
System.out.println("c2");
}
}
初始化及类的加载
public class Insect {
protected int i = 9;
protected int j;
protected int x3 = print("static Insect.x3 init");
public Insect() {
System.out.println("i="+i+" ,j="+j);
j =39;
}
private static int x1 = print("static Insect.x1 init");
static int print(String s){
System.out.println(s);
return 47;
}
}
public class Beetle extends Insect{
private int k = print("Beetle.k init");
public Beetle() {
System.out.println("k="+k);
System.out.println("j="+j);
}
private static int x2 = print("static Beetle.x2 init");
public static void main(String[] args) {
System.out.println("Beetle construct");
Beetle beetle = new Beetle();
}
}
Result
static Insect.x1 init
static Beetle.x2 init
Beetle construct
static Insect.x3 init
i=9 ,j=0
Beetle.k init
k=47
j=39
可以看出,先进行类的加载,先基类再子类。static变量/块执行于类的初始化时期。new之后按照 先初始化再构造器的顺序执行。
Java编程思想(Chapter2、4、6)的更多相关文章
- JAVA编程思想(第四版)学习笔记----4.8 switch(知识点已更新)
switch语句和if-else语句不同,switch语句可以有多个可能的执行路径.在第四版java编程思想介绍switch语句的语法格式时写到: switch (integral-selector) ...
- 《Java编程思想》学习笔记(二)——类加载及执行顺序
<Java编程思想>学习笔记(二)--类加载及执行顺序 (这是很久之前写的,保存在印象笔记上,今天写在博客上.) 今天看Java编程思想,看到这样一道代码 //: OrderOfIniti ...
- #Java编程思想笔记(一)——static
Java编程思想笔记(一)--static 看<Java编程思想>已经有一段时间了,一直以来都把笔记做在印象笔记上,今天开始写博客来记录. 第一篇笔记来写static关键字. static ...
- [Java编程思想-学习笔记]第3章 操作符
3.1 更简单的打印语句 学习编程语言的通许遇到的第一个程序无非打印"Hello, world"了,然而在Java中要写成 System.out.println("He ...
- Java编程思想重点笔记(Java开发必看)
Java编程思想重点笔记(Java开发必看) Java编程思想,Java学习必读经典,不管是初学者还是大牛都值得一读,这里总结书中的重点知识,这些知识不仅经常出现在各大知名公司的笔试面试过程中,而 ...
- 《java编程思想》读书笔记(一)开篇&第五章(1)
2017 ---新篇章 今天终于找到阅读<java编程思想>这本书方法了,表示打开了一个新世界. 第一章:对象导论 内容不多但也有20页,主要是对整本书的一个概括.因为已经有过完整JAV ...
- Java编程思想——初始化与清理
PS:最近一直忙于项目开发..所以一直没有写博客..趁着空闲期间来一发.. 学习内容: 1.初始化 2.清理 1.初始化 虽然自己的Java基础还是比较良好的..但是在解读编程思想的时候还是发现了 ...
- java编程思想-复用类总结
今天继续读<java 编程思想>,读到了复用类一章,看到总结写的很好,现贴上来,给大家分享. 继承和组合都能从现有类型生成新类型.组合一般是将现有类型作为新类型底层实现的一部分来加以复用, ...
- 注解的基本盘点 -- 《Java编程思想》
注解(元数据)为我们在代码中添加信息提供了一种形式化的方法,使我们可以在之后的某一个时刻非常方便地使用这些数据. ---<Java编程思想> 其实注解可以理解为一个工具类,只要使用了这个工 ...
随机推荐
- CodeBlock 使用TextOut出错
undefined reference to `TextOutA@20'C:\Program Files (x86)\CodeBlocks\MinGW\lib这次需要的库是:libgdi32.a 1. ...
- jquery对象操作
大类 JQ方法 备注 创建元素 var $h1 = $(“<h1>< ...
- install skype4.3 in ubuntu15.04
Canonical Partners repository finally adds support for Ubuntu 15.04. Here’s how to enable the reposi ...
- linux ssh config
Host code.engineering.redhat.com HostName code.engineering.redhat.com Port 29418 User jiall ...
- 无鼠标Windows操作
1.常用tab键,方便跳转 2.打开软件方式: 1.win+1,2,3...依序打开任务栏图标.常用软件可以放在这里: 2.创建quickStart文件夹,配置路径,将所有要用到的功能都放在这里.( ...
- [转载]并发编程之Operation Queue和GCD
并发编程之Operation Queue http://www.cocoachina.com/applenews/devnews/2013/1210/7506.html 随着移动设备的更新换代,移动设 ...
- spi接口的ds1302时钟芯片控制在lcd1602上显示
spi接口的ds1302时钟芯片控制在lcd1602上显示 ...
- ORACLE10g创建表空间,角色与授权
创建基础表空间,创建用户,授权. -- CREATE TABLESPACE CREATE TABLESPACE TS_JK_LAB_BASIC DATAFILE 'D:\TOOLS\ORACLE\PR ...
- ASP.NET 4.5.256 has not been registered on the Web server. You need to manually configure your Web server for ASP.NET 4.5.256 in order for your site to run correctly
Microsoft .NET Framework 4.6安装后,用户可能会在使用Microsoft Visual Studio 创建(或打开现有项目时)网站.或Windows Azure项目时遇到下面 ...
- Centos6.6下安装MariaDB步骤,利用yum进行安装
1.在/etc/yum.repos.d/下建立MariaDB.repo文件 可以在Win下编辑好此文件,然后通过SSH远程复制过去. 2.MariaDB.repo内容要根据MariaDB官方提供的re ...