java notes

1. 数组

声明:  int a[];

 创建: a=new int [100];

 允许非矩形的多维数组: int a[][];  a=new int[4][];  a[1]=new int[11]; a[2]=new int [12];

a.length=4;

 

2. 构造函数

public Employee(String n, int s) {
name = n;
salary = s;
}
public Employee(String n) {
this(n, 0);
}
public Employee() {
this( " Unknown " );
}  

this 必须在构造函数的第一句。

 

3. 继承

构造函数不被继承,一个类,要么写构造函数,要么用缺省的构造函数。

(除了子构造函数外,父类构造函数也总是被访问。这将在后面详细讨论。)

与c++不同,java总是多态的:

Employee e = new Manager();
e.getDetails();

执行的是 Manager 类的 getDetails.

 

4. 调用父类构造函数

通常要定义一个带参数的构造函数,并要使用这些参数来控制一个对象的父类部分的构造。可能通过从子类构造函数的第一行调用关键字super 的手段调用一个特殊的父类构造函数作为子类初始化的一部分。要控制具体的构造函数的调用,必须给super()提供合适的参数。当不调用带参数的super 时,缺省的父类构造函数(即,带0 个参数的构造函数)被隐含地调用。在这种情况下,如果没有缺省的父类构造函数,将导致编译错误。

public class Employee {
String name;
public Employee(String n) {
name = n;
}
}
public class Manager extends Employee {
String department;
public Manager(String s, String d) {
super(s);
department = d;
}
}

// 该程序来自 《Sun培训教程中文稿》

由于super 和 this 都要占据第一行,所以不能兼得。然而父类总是先构造,所以

不会出现 super,this;     super,this 分别需要在两个构造函数里出现的情况,最多是 super this  ,这样只需要把 super写在this对应的构造函数里就行了。 

 

5. final

final 类不能被分成子类
final 方法不能被覆盖
final 变量是常数

被标记为static 或private 的方法被自动地final,

如果将引用类型(即,任何类的类型)的变量标记为final,那么该变量不能指向任何其它对象。但可能改变对象的内容,因为只有引用本身是final。

 

6. 抽象类

声明方法的存在而不去实现它的类被叫做抽象类
可以通过关键字abstract 进行标记将类声明为抽象
public abstract class Drawing {
public abstract void drawDot(int x, int y);
public void drawLine(int x1, int y1,
int x2, int y2) {
// draw using the drawDot() method repeatedly.
}
}
一个abstract 类可以包含非抽象方法和变量。

不能创建abstract 类的实例。然而可以创建一个变量,其类型是一个抽象类,并让它指向具体子类的一个实例。不能有抽象构造函数或抽象静态方法。
Abstract 类的子类为它们父类中的所有抽象方法提供实现,否则它们也是抽象类。

 

7. 接口

接口中的所有方法都是抽象的,没有一个有程序体。接口只可以定义static final 成员变量。

实现接口的语法:

public class MyApplet extends Applet implements
Runnable, MouseListener{
"…"
}

 

8. ==  vs equals()

== 和 equals() 返回 true iff 引用指向同一个对象。

但 equals() 可以被覆盖,当两个分离的对象的内容和类型相配的话,String,Date,File 类和所有其它override equals()的包装类(Integer,Double,等等)将返回真。

 

9. 内部类

1. public class MyFrame extends Frame{
2. Button myButton;
3. TextArea myTextarea;
4. public MyFrame(){
5. ………………….
6. ………………….
7. MyFrame$ButtonListener bList = new
8. MyFrame$ButtonListener(this);
9. myButton.addActionListener(bList);
10. }

11. class MyFrame$ButtonListener implements
12. ActionListener{
13. private MyFrame outerThis;
14. Myframe$ButtonListener(MyFrame outerThisArg){
15. outerThis = outerThisArg;
16. }
17.
18. public void actionPerformed(ActionEvent e) {
19. outerThis.MyTextArea.setText("buttonclicked");
20. ………………….
21. ………………….
22. }
23. public static void main(String args[]){
24. MyFrame f = new MyFrame();
25. f.setSize(300,300);
26. f.setVisible(true);
27. }
28. } // end of MyFrame

 

没有this,或者static方法中,的情况下,可以这样:

public static void main(String args[]){
MyFrame f = new MyFrame();
MyFrame.ButtonListener bList =
f.new ButtonListener();
f.setSize(50,50);
f.setVisible(true);
}

内部类可以 static, 及 abstract 及 private.

一般的类只能是 缺省访问级别或者 public, 但内部类可以 private 或protected

内部类不能有 static 成员。

 

10 包装类

基本数据类型 包装类
boolean Boolean
byte Byte
char Char
short Short
int Int
long Long
float Float
double Double

 

11.  finally

finally 语句定义一个总是执行的代码块,而不考虑异常是否被捕获。

1. try {
2. startFaucet();
3. waterLawn();
4. }
5. finally {
6. stopFaucet();
7. }

 

如果终止程序的System.exit()方法在保护码内被执行,那么,这是finally 语句不被执行的唯一情况。这就暗示,控制流程能偏离正常执行顺序,比如,如果一个return 语句被嵌入try 块内的代码中,那么,finally 块中的代码应在return 前执行。

 

12 异常

用户定义异常:

1. public class ServerTimedOutException extends Exception {
2. private String reason;
3. private int port;
4. public ServerTimedOutException (String reason,int port){
5. this.reason = reason;
6. this.port = port;
7. }
8. public String getReason() {
9. return reason;
10. }
11. public int getPort() {
12. return port;
13. }
14. }

throw new ServerTimedOutException
("Could not connect", 80);

也可能部分地处理一个异常然后也将它抛出。如:
try {
…..
…..
} catch (ServerTimedOutException e) {
System.out.println("Error caught ");
throw e;
}

 

13. Container

Container 的两个主要类型是Window 和Panel
Window 是Java.awt.Window.的对象
Panel 是Java.awt.Panel 的对象

容器不但能容纳组件,还能容纳其它容器,这一事实对于建立复杂的布局是关键的,也是基本的。

 

 

14. 布局管理

因为布局管理器负责容器里的组件的位置和大小,因此不需要总是自己去设定组件的大小或位置。如果想这样做(使用setLocation(),setSize()或setBounds()方法中的任何一种),布局管理器将覆盖你的决定。
如果必须控制组件的大小或位置,而使用标准布局管理器做不到,那就可能通过将下述方法调用发送到容器中来中止布局管理器:
setLayout(null);
做完这一步,必须对所有的组件使用setLocation(),setSize()或setBounds(),来将它们定位在容器中。
请注意,由于窗口系统和字体大小之间的不同,这种办法将导致从属于平台的布局。更好的途径是创建布局管理器的新子类。

//上一小节摘自: 《Sun培训教程中文版》

 

此条目发表在未分类分类目录。将固定链接加入收藏夹。

留下评论