1
JAVA笔记
2
2008
04
22
3
1
.用javac编译代码时,javac 后的文件名对大小写不敏感,如一个名为Hello.java的文件,
4
可以这样写:javac hello.java或javac HEllO.java
5
2
.用java解释代码时,java 后跟的是一个类名,而不是一个文件名.如要解释文件名为Hello.java的文件(类名为Hello是一个类名,
6
文件名要写类名相同)必须写成java Hello.
7
3
. Label prompt1;
//
Label 对象是一个可以容器中放置文本的组件
8
TextField input1;
//
TextField 对象是个允许编辑单行文本的文本组件
9
4
.java中对boolean类型赋值时只能是true或false,不能像C语言一样的赋任意不为0 的数.
10
如在用到if或while语句时,条件只能用boolean值的true或false.如:
11![]()
if
(
true
)
{
12
}
13![]()
while
(
false
)
{
14
}
15
5
.java中数组:
16
1
)可以在定义数组时值,
17
如:
18![]()
int
[] num
=
{1,2,3}
//
ok
19
或:
20![]()
int
[] num
=
new
int
[]
{1,2,3}
//
ok
21
注意:不能这么指定大小:
22![]()
int
[] num
=
new
int
[
3
]
{1,2,3}
//
error!!!
23
但不能在定义之后再赋值,(非法)
24
如:
25
int
[] num;
26![]()
num
=
{1,2,3}
//
error
27
2
)二维数组
28![]()
int
[][]num
=
new
int
[][]
{1,2,3,4,5,6}
;
//
error
29![]()
int
[][]num
=
new
int
[][]
{
{1,2,3},
{4,5,6}}
;
//
初始化赋值两行三列ok
30![]()
int
[][]num
=
new
int
[
2
][
3
]
{
{1,2,3},
{4,5,6}}
;
//
初始化指定大小两行三列error
31
6
.关于JAVA的
"
++
"
操作:
32
int
i
=
3
;
33
int
count
=
(i
++
)
+
(i
++
)
+
(i
++
);
//
这么计算:(3) +(4) +(5)
34
int
count
=
(
++
i)
+
(
++
i)
+
(
++
i);
//
这么计算:(4) +(5) +(6)
35
System.out.println(
"
i=
"
+
i);
36
System.out.println(
"
count=
"
+
count);
37
2
3
4
5
6
7
8
9
10
11

12
13

14
15
16
17
18

19
20

21
22

23
24
25
26

27
28

29



30



31
32
33
34
35
36
37
1
JAVA笔记
2
2008
04
24
3![]()
4
1
.补码:
5
已知一个负数的补码,将其转换为十进制数,步骤:
6
1
、先对各位取反;
7
2
、将其转换为十进制数;
8
3
、加上负号,再减去1。
9
例如:
10
11111010
,最高位为1,是负数,先对各位取反得00000101,转换为十进制数得5,加上负号得
-
5
,再减1得
-
6
。
11
2
.左移,右移,无符号右移.
12
int
j
=
8
;
13
int
f
=
j
<<
2
;
//
左移.每左移一位相当于乘以2,这里乘以4
14
int
g
=
j
>>
2
;
//
右移,每右移一位相当于除以2,这里除以4
15
int
h
=
j
>>>
2
;
//
无符号右移,每右移一位相当于除以2,这里除以4
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
JAVA笔记
2![]()
3
2008
04
26
4![]()
5
1
.特殊变量this的使用:
6
关于实例方法和实例数据成员的进一步说明
7
一个类所有的实例(对象)调用的成员方法在内存中只有一份拷贝,
8
尽管在内存中可能有多个对象,而数据成员在类的每个对象所在内存中都存在着一份拷贝。
9
this变量允许相同的实例方法为不同的对象工作。每当调用一个实例方法时,
10
this变量将被设置成引用该实例方法的特定的类对象。方法的代码接着会与this所代表的对象的特定数据建立关联。
11![]()
12
1
)this代表对象本身,当类的成员变量与类成员方法的局部变量同名时,可以用this来区分.
13
2
)
this
还可以简化构造函数的调用
14
class
Point
15![]()
{
16
int x,y; //类的成员变量
17
//
18
Point ()//构造方法主要为类的对象定义初始化状态
19![]()
{
20
this(1,2); //通过this简化构造函数的调用。
21
}
22
Point(int a ,int b)//重载构造方法
23![]()
{
24
//initialization phase初始化段
25
x = a;
26
y = b;
27
}
28
29
//termination phase终止段
30
void output()
31![]()
{
32
System.out.println("x ="+x);
33
System.out.println("y ="+y);
34
}
35
36
//void output(int x ,int y) //this变量代表对象本身
37
//{ //当类中有两个同名变量,一个属于类(类的成员变量),
38
// //而另一个属于某个特定的方法(方法中的局部变量),使用this区分成员变量和局部变量。
39
// this.x = x;
40
// this.y = y;
41
//}
42
43
//processing phase处理段
44
public static void main(String[] args)// main method
45![]()
{
46
Point pt1;
47
pt1 = new Point();
48
//pt1.putput(4,5);
49
pt1.output();
50
51
}
52
}
53![]()
54
2
. 类方法(静态方法)和类变量(静态变量)
55![]()
1
)
class
Point
{
56
.
57
static int a;//定义一个类变量
58![]()
stactic void output()
{}//定义一个类方法
59

60
Point.output();//调用一个类方法
61
}
62
2
)类方法只能引用类变量,不能引用非类变量,也不能调用非类方法
63
3
)类变量可以被非类方法引用,类方法也可以通过对象调用
64
3
.用final声明常量:
65
1
)用final声明的常量的同时必须对他进行初始化;
66
2
)非静态常量也可以在类的构造方法中初始化,
67
但如果类中有多个重载构造方法则必须在
*
每个没有调用已经初始化final常量方法
*
中都进行初始化.
68
3
)
**
如果把这个常量声明为静态常量时(
static
final
PI
=
3.1415926
)则必须在声明时初始化
!
69![]()
70
4
.关于继承extends[
**
构造方法和析构方法不能被继承
**
]
71
1
)JAVA是不允许多继承的.
72
2
)在子类中定义一个与父类中同名,同返回类型,同参数个数和类型的成员方法称为方法的覆盖.即
**
子类的方法把父类的方法覆盖掉了
**!!
73
在子类中的同名变量亦是如此,此时父类的同名变更将在子类中被隐藏起来
!
74
3
)如果要访问父类中被隐藏的成员变量或调用被覆盖的方法可以用Super.
75
4
)每个子类构造方法的第一条语句,都是隐含地调用super();
76
**
如果子类和父类中都有构造方法,则必须通过super()显示调用父类的构造方法
!**
77
5
.关于多态性:
78
1
)通过覆盖父类的方法来实现,在运行时根据传递的对象引用,来调用相应的方法。
79
2
)当我们把子类对象的引用传给声明为父类的一个对象变量,
80
如果子类有的方法就调用子类的方法,如果子类没有的方法就调用父类的方法
!
81
6
.可以用instanceof来验证某个对象是否是某个类的实例
82
83
class
Animal
84![]()
{
85
int weight,height;
86
Animal(int weight,int height)//Animal类的构造方法
87![]()
{
88
System.out.println("Animal construct");
89
}
90
void eat()
91![]()
{
92
System.out.println("Animal eat");
93
}
94
95
void breathe()
96![]()
{
97
System.out.println("Animal breathe");
98
}
99
}
100![]()
101
class
Fish
extends
Animal
//
Fish类继承了Animal类
102![]()
{
103
int height;
104
Fish()//Fish类的构造方法
105![]()
{
106
super(30,40);//显示调用父类中带参数的构造方法
107
System.out.println("Fish construct");
108
}
109
void breathe()//在了类中重写breathe方法
110![]()
{
111
//super.breathe();//用super可以调用父类被覆盖的方法
112
//super.height = 40;//用super可以访问父类被隐藏的成员变量
113
System.out.println("Fish bubble");
114
}
115
}
116![]()
117
class
Intergration
118![]()
{
119
public static void main(String[] args)
120![]()
{
121
//Animal an = new Animal();
122
Fish fh = new Fish();
123
//
124
//an.breathe();
125
//fh.height = 30;
126
//fh.breathe();
127
Animal an=new Animal(30,40);
128
Fish fh=new Fish();
129
an=fh;//将Fish的对象fh的引用赋给Animal的对象an后,an也变成了Fish的一个实例!
130![]()
/**//*if(an instanceof Animal)
131
{
132
System.out.println("an is animal's instance");
133
}
134
if(fh instanceof Fish)
135
{
136
System.out.println("fh is fish's instance");
137
}*/
138
if(an instanceof Fish)//验证an是否是Fish的一个实例
139![]()
{
140
System.out.println("an is fish's instance");
141
}
142
else
143![]()
{
144
System.out.println("an isn't fish's instance");
145
}
146
if(fh instanceof Animal)
147![]()
{
148
System.out.println("fh is animal's instance");
149
}
150
else
151![]()
{
152
System.out.println("fh isn't animal's instance");
153
}
154
}
155
}
156![]()
157
7
.用面向对象的方法求:
158
求一个长方形的周长和面积。
159
以面向对象的程序设计方式思考:
160
1
、一个长方形可以看成一个长方形对象。
161
2
、一个长方形对象有两个状态(长和宽)和两个行为(求周长和求面积)。
162
3
、将所有长方形的共性抽取出来,设计一个长方形类。
163
4
、通过长方形对象的行为,就可以求出某个具体的长方形对象的周长和面积。
164![]()
165
//
create a Rectangle class
166
//
declaration l and w variables for length and width
167
//
create perimeter method and area to count
168
//
creat main method
169
//
declaration object Rectal1 and Recral2
170
//
intialization variable l and w by object Rectal1 and Recral2
171
//
print the value
172
173
class
Rectangle
174![]()
{
175
int l,w;
176
177
//processing phase
178
int perimeter()
179![]()
{
180
return 2*(l+w);
181
}
182
183
int area()
184![]()
{
185
return l*w;
186
}
187
188
public static void main(String[] args)
189![]()
{
190
Rectangle Recta1 = new Rectangle();
191
Rectangle Recta2 = new Rectangle();
192
193
//initialization phase
194
Recta1.l = 10;
195
Recta1.w = 30;
196
Recta2.l = 30;
197
Recta2.w = 20;
198
199
//termination phase
200
System.out.println("Recta1's Perimeter is: \t" + Recta1.perimeter());
201
System.out.println("Recta1's Area is: \t" + Recta1.area());
202
System.out.println("Recta2's Perimeter is: \t" + Recta2.perimeter());
203
System.out.println("Recta2's Area is: \t" + Recta2.area());
204
}
205
}
206
2
3
4
5
6
7
8
9
10
11
12
13
14
15

16
17
18
19

20
21
22
23

24
25
26
27
28
29
30
31

32
33
34
35
36
37
38
39
40
41
42
43
44
45

46
47
48
49
50
51
52
53
54
55

56

57
58

59

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

85
86
87

88
89
90
91

92
93
94
95
96

97
98
99
100
101
102

103
104
105

106
107
108
109
110

111
112
113
114
115
116
117
118

119
120

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

140
141
142
143

144
145
146
147

148
149
150
151

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

175
176
177
178
179

180
181
182
183
184

185
186
187
188
189

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
1
JAVA笔记
2
2008
04
29
3![]()
4
1
.
package
(包)
5
1
)package语气必须是程序的第一条语气(除了注释语气和空白语气)
6
2
)package的包名指的是一个文件夹或路径,也就是说,类名在package所在的目录下,在解释执行时必须标明类所在的路径.
7
如:
8
package
cn.mybole;
9![]()
10
class
Test
11![]()
{
12
public static void main(String[] args)
13![]()
{
14
System.out.println("package test!");
15
}
16
}
17
18
//
执行:java cn.mybole.Test
19
3
)可以在编译时用
"
-d .
"
(
-
d 和.之间有一个空格,.和
*
.java之间有一个空格)参数来在当前目录下生成目录 cn
/
mybole
/
Test.
class
,
20
即将
*
.class文件在生成的目录下生成.
21
如: javac
-
d . Test.java
//
在当前目录一生成包
22
javac
-
d E:\javalesson\Test.java
//
在指定目录(E:\javalesson)一生成包
23
4
)用import关键字导入一个包(其中,java.lang包是自动导入的):
24
1
.
import
java.io.File
//
导入java.io包中的File类
25
2
.
import
java.io.
*
//
导入java.io包中的所有类
26
这种导入所有类的方法虽然方便,但是:
27
1
.会消耗机器的大量内存
28
2
.当远程加载时会因加载到本地的类过多而造成明显的延时.
29
5
)导入一个包中的类后可以在当前类中用所导入的类直接定义变量
30
如: File f;
//
用已经导入的类定义变量
31
java.io.File f
//
当没有用import导入包时也可以通过完整的类名定义
32
6
)如果要使用的类在同一个包中,无需用import语气就可以相互引用.
33![]()
34
2
.类的说明符
35
1
)从外部软件包中访问非同一个包的一个类
--
public类
36
1
.缺省的类是不能被外部软件包访问的.
37
2
.只有声明为public的类才能通过import(或在当前类中以完整类名)被外部软件包访问.
38
如:
39
//
Test2.java
40
41
//
package cn.mybole;
42
package
com.winsunlight;
//
com.winsunlight包
43
44
public
class
Test2
//
Test类是public类.
45![]()
{
46
47
}
48
//
Test.java
49
50
package
cn.mybole;
51
52
//
import java.io.File;
53
//
import java.io.*;
54
import
com.winsunlight.Test2;
//
导入com.winsunlight包中的Test2类
55
class
Test
56![]()
{
57
public static void main(String[] args)
58![]()
{
59
//File f;
60
//java.io.File f;
61
//System.out.println("package test!");
62
Test2 t2 = new Test2(); //访问com.winsunlight包中的Test2类
63
}
64
}
65
2
)final类(最终类)不能被其他类所继承,其方法也不能被覆盖.
66
1
.
public
final
class
Test2
//
用public和final修饰的类可以用import引用,亦不能被继承.
2
3
4
5
6
7
8
9
10
11

12
13

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

46
47
48
49
50
51
52
53
54
55
56

57
58

59
60
61
62
63
64
65
66