Java 03

/* video 08 */

实现类的编程人员 : 尽可能的对(使用者)隐藏实现的细节,只发布(使用者)最需要的信息。
使用类的编程人员 : 不需要知道有关类如何运行的细节。

定义类时 :
常量定义在上边
方法
实例变量

随机数生成方法:(伪随机)
首先有一个数字,一般和你机器上的时间有关,然后根据着个数字生成了一个数字,假设生成的是5,然后根据5继续生成一个数字。
setSeed(1), 其中,setSeed方法是让生成数字的序列完全一致,即虽然还是随机生成,但是,第一次运行程序和第二次运行程序生成的随机“序列”完全一致。
setSeed 也就是设置了第一个数字,第一个数字一样,那么生成序列就一样。

 

/* video 09 */

String str = "hello there";

定义类时如果不给定继承类,就会自动继承 object 类
类中的实例变量声明为 private 是有好处的( 尽量 )
包是相关类的集合。

构造函数 用语指定如何创建类的新实例 :
1. 构造函数名称总是与类的名称相同
2. 构造函数不指定结果类型。
没有参数的构造函数,被称做是默认构造函数
如果定义了构造函数,系统就不会调用默认构造函数(就当不存在),所以你想用默认构造函数样子(没有参数),就需要再重新定义一个默认构造函数。
客户通过关键字 new, 类的名称及与形参列表匹配的一列实参,可以调用构造函数
this 指的是接收对象,this.counter,表示这个对象的counter实例变量,我的实例变量
所以,最好还是使用不同的名字,而介绍使用 this,在构造函数中,不要使用 counter = counter
对象传递,传递的是指针。。所谓 “引用”

local variable
instance variable
class variable ( static )

Javadoc
- comment
  start with : /**
  end   with : */
- special "atg" ( @param @result, etc;)
例如 : @param unit ... 其中 unit 就是一个参数名

 

/* video 10 */

super(name, id); // 调用父类构造函数
函数内部使用 // 注释
函数外部使用 /* 注释

 

关于 java 中 循环内部定义变量或者实例的情况

java 中与 c 不同,可以在循环中重复声明变量,或者定义实例,原因如下:

在块级作用域内,所有的定义的变量会在循环结束当前这次的时候,结束了块的作用区域

for (int i=0; i<10; i++) {

int a = 0;

a = a + i;

}

如上程序,当 i = 0 时,执行循环,定义了一个 a, 循环结束,判断是否继续执行时,此时 i = 1,所以,只有i保存了下来,所有的变量或者 实例在循环内部定义的,都将不会保存下来,当然,这主要说的是栈内的情况,而堆内的情况应该是一直保存下来。

例如:

Java 03 循环中创建实例
 1 private void drawBricks() {

 2         int x = 10;        // brick location x

 3         int y = 200;    // brick location y

 4         for (int i=1; i<=14; i++) {    //COLUMN

 5             for(int j=14; j>=i; j--) {    // ROW

 6                 GRect rect = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);

 7                 add(rect);

 8                 

 9                 x = x + BRICK_WIDTH;

10             }

11             x = 10 + i * BRICK_WIDTH / 2 ;

12             y = y - BRICK_HEIGHT;

13         }

14     }

为什么在循环中创建的 new GRect 没有被释放呢? 个人认为:因为 add 函数调用了它,在屏幕中绘画了一个图形,所以没有被释放。

Java 03
 1 import acm.graphics.*;

 2 import acm.program.*;

 3 import java.awt.*;

 4 

 5 public class ProgramHierarchy extends GraphicsProgram {    

 6     public void run() {

 7         /* You fill this in. */

 8         GRect square = new GRect(0, 0, SQUARE_SIZE, SQUARE_SIZE);

 9         square.setFilled(true);

10         add(square);

11         double dx = (getWidth() - SQUARE_SIZE) / N_STEPS;

12         double dy = (getHeight() - SQUARE_SIZE) / N_STEPS;

13         for (int i=0; i<N_STEPS; i++) {

14             square.move(dx, dy);

15             pause(PAUSE_TIME);

16         }

17     }

18     

19     /* private constants */

20     private static final int N_STEPS = 500;

21     private static final int PAUSE_TIME = 10;

22     private static final double SQUARE_SIZE = 50;

23     

24     

25 }

 

Java 03 Pyramid
 1 import acm.graphics.*;

 2 import acm.program.*;

 3 import java.awt.*;

 4 

 5 public class Pyramid extends GraphicsProgram {

 6 

 7 /** Width of each brick in pixels */

 8     private static final int BRICK_WIDTH = 40;

 9 

10 /** Height of each brick in pixels */

11     private static final int BRICK_HEIGHT = 15;

12 

13 /** Number of bricks in the base of the pyramid */

14     private static final int BRICKS_IN_BASE = 10;

15     

16     public void run() {

17         /* You fill this in. */

18         for (int i=0; i<BRICKS_IN_BASE; i++) {

19             for (int j=0; j<=i; j++) {

20                 GRect rect = new GRect(START_X - (i * 0.5 * BRICK_WIDTH) + (j * BRICK_WIDTH), 

21                                        START_Y + (i * BRICK_HEIGHT), 

22                                        BRICK_WIDTH, BRICK_HEIGHT);

23                 

24                 add(rect);

25             }

26         }

27         

28     }

29     

30     /* private constants */

31     private static final int START_X = 300;

32     private static final int START_Y = 100;

33 }

 

Java 03 Chess
 1 import acm.graphics.*;

 2 import acm.program.*;

 3 import java.awt.*;

 4 

 5 public class ProgramHierarchy extends GraphicsProgram {    

 6     public void run() {

 7         /* You fill this in. */

 8         for (int i=0; i<8; i++) {

 9             for (int j=0; j<8; j++) {

10                 int dx = START_X + (j * SQARE_SIZE);

11                 int dy = START_Y + (i * SQARE_SIZE);

12                 GRect rect = new GRect(dx, dy, SQARE_SIZE, SQARE_SIZE);

13                 if ( (i+j) %2 != 0) {

14                     rect.setFilled( true );

15                     rect.setFillColor(Color.gray);

16                     add(rect);

17                     if ( !(i==3 || i==4) ) {

18                         GOval oval = new GOval(dx+2, dy+2, CIRCLE_SIZE, CIRCLE_SIZE);

19                         oval.setFilled(true);

20                         oval.setFillColor(Color.black);

21                         add(oval);

22                     }

23                 } else {

24                     add(rect);

25                 }

26                                 

27             }

28         }

29         

30     }

31     

32     /* private constants */

33     private static final int SQARE_SIZE = 20;

34     private static final int START_X = 50;

35     private static final int START_Y = 50;

36     private static final int CIRCLE_SIZE = 15;

 

Java 03 BouncingBall
 1 import acm.graphics.*;

 2 import acm.program.*;

 3 import java.awt.*;

 4 

 5 public class ProgramHierarchy extends GraphicsProgram {    

 6     public void run() {

 7         /* You fill this in. */

 8         int startX = (getWidth() - BALL_SIZE) / 2;

 9         int startY = (getHeight() - BALL_SIZE) / 2;

10         int vx = 1;

11         int vy = 1;

12         int dx = startX + vx;

13         int dy = startY + vy;

14         

15         

16         GOval oval = new GOval(startX, startY, BALL_SIZE, BALL_SIZE);

17         oval.setFilled(true);

18         oval.setFillColor(Color.blue);

19         add(oval);        

20     

21         

22         while (true) {

23                 

24             oval.move(vx, vy);

25 

26             // draw a track line.

27              GLine line = new GLine(dx + (BALL_SIZE / 2), dy + (BALL_SIZE / 2), 

28                                      dx + (BALL_SIZE / 2) + vx, dy + (BALL_SIZE / 2) + vy);

29              add(line);

30               

31             

32             // check the boundary

33             if ( (dx+BALL_SIZE >= getWidth()) || (dx <= 0) ) {

34                 vx = -vx;

35             } 

36             if ( (dy+BALL_SIZE >= getHeight()) || (dy <= 0) ) {

37                 vy = -vy;

38             } 

39 

40             

41             dx = dx + vx;

42             dy = dy + vy;

43             pause(PAUSE_TIME);

44         }

45     }

46     

47     /* private constants */

48     private static final int BALL_SIZE = 30;

49     private static final int PAUSE_TIME = 10;

50     private static final int SMALL_BALL_SIZE = 5;

51     

52     

53 }
Java 03
迷宫利用keral实现

 

你可能感兴趣的:(java)