分类: Java
2009-07-07 15:06:47
class SportsTicker extends Ticker {
// body of the class
}
实例变量:方法之外定义的,没有static前缀的变量。
比如:
class VolcanoRobot {
String status;
int speed;
float temperature;
}
类变量:
static int sum;
static final int maxObjects =
10;
定义方法:
class RangeLister {
int[] makeRange(int lower, int upper) {
int[] range = new int[(upper-lower) +
1];
for (int i = 0; i < range.length;
i++) {
range[i] = lower++;
}
return range;
}
public static void main(String[] arguments)
{
int[] range;
RangeLister lister = new RangeLister();
range = lister.makeRange(4, 13);
System.out.print("The array: [
");
for (int i = 0; i < range.length; i++) {
System.out.print(range[i] + "
");
}
System.out.println("]");
}
}
运行结果:The array: [ 4 5 6 7 8 9 10 11 12 13 ]
This指的是当前对象。多用于局部变量和其他变量同名的情况。类方法需要static来定义,不能使用this。变量也尽量不要和父类中的重名。
数组作为方法参数会修改实际数据:
class Passer {
void toUpperCase(String[] text) {
for (int i = 0; i < text.length; i++) {
text[i] = text[i].toUpperCase();
}
}
public static void main(String[] arguments) {
Passer passer = new Passer();
passer.toUpperCase(arguments);
for (int i = 0; i < arguments.length; i++) {
System.out.print(arguments[i] + " ");
}
System.out.println();
}
}
运行结果:
java Passer Athos Aramis Porthos
ATHOS ARAMIS PORTHOS
类方法不需要初始化就可以引用。
static void exit(int arg1) {
// body of the method
}
参数处理:
class Averager {
public static void main(String[] arguments) {
int sum = 0;
if (arguments.length > 0) {
for (int i = 0; i < arguments.length; i++) {
sum += Integer.parseInt(arguments[i]);
}
System.out.println("Sum is: " + sum);
System.out.println("Average is: " +
(float)sum / arguments.length);
}
}
}
运行结果:
java Averager 1 4 13
You should see the following output:
Sum is: 18
Average is: 6.0
方法多态:
根据参数的个数和类型来决定。和返回值没有关系。
import java.awt.Point;
class Box {
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
Box buildBox(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
return this;
}
Box buildBox(Point topLeft, Point bottomRight) {
x1 = topLeft.x;
y1 = topLeft.y;
x2 = bottomRight.x;
y2 = bottomRight.y;
return this;
}
Box buildBox(Point topLeft, int w, int h) {
x1 = topLeft.x;
y1 = topLeft.y;
x2 = (x1 + w);
y2 = (y1 + h);
return this;
}
void printBox(){
System.out.print("Box: <" + x1 + ", " + y1);
System.out.println(", " + x2 + ", " + y2 + ">");
}
public static void main(String[] arguments) {
Box rect = new Box();
System.out.println("Calling buildBox with coordinates "
+ "(25,25) and (50,50):");
rect.buildBox(25, 25, 50, 50);
rect.printBox();
System.out.println("\nCalling buildBox with points "
+ "(10,10) and (20,20):");
rect.buildBox(new Point(10, 10), new Point(20, 20));
rect.printBox();
System.out.println("\nCalling buildBox with 1 point "
+ "(10,10), width 50 and height 50:");
rect.buildBox(new Point(10, 10), 50, 50);
rect.printBox();
}
}
运行结果:
D:\java\9433-source-code\chapter5>java Box
Calling buildBox with coordinates (25,25) and (50,50):
Box: <25, 25, 50, 50>
Calling buildBox with points (10,10) and (20,20):
Box: <10, 10, 20, 20>
Calling buildBox with 1 point (10,10), width 50 and height 50:
Box: <10, 10, 60, 60>
D:\java\9433-source-code\chapter5>
构造方法:
和类同名,没有返回类型。比如:
class VolcanoRobot {
String status;
int speed;
int power;
VolcanoRobot(String in1, int in2, int in3) {
status = in1;
speed = in2;
power = in3;
}
}
VolcanoRobot vic = new VolcanoRobot(“exploring”, 5, 200);
重载后调用原来的方法:
void doMethod(String a, String b) {
// do stuff here
super.doMethod(a, b);
// do more stuff here
}
构造方法的重构:
调用:super(arg1, arg2, ...); 必须是构造方法的第一句。
import java.awt.Point;
class NamedPoint extends Point {
String name;
NamedPoint(int x, int y, String name) {
super(x,y);
this.name = name;
}
public static void main(String[] arguments) {
NamedPoint np = new NamedPoint(5, 5, "SmallPoint");
System.out.println("x is " + np.x);
System.out.println("y is " + np.y);
System.out.println("Name is " + np.name);
}
}
结束方法:
protected void finalize() throws Throwable {
super.finalize();
}
释放对象:mainPoint = null;
一般的情况不需要使用结束方法。