Java 学习个人笔记(二)(March 19,2013 )

Java 学习个人笔记(二)(March 19,2013 )

    – Java 学习个人笔记(二)–
    写这个个人笔记完全是出于记录自己所学,方便本人日后参考,可能很零散。这个笔记是建立在C语言编程基础上,本人学习Java只学习它与C语言不同的地方,或者我在C编程过程中很少用到的地方。所用的教材是Youtube一位达人做的视频:Java编程简明教程 by Thenewboston(Youtube)

    每次开一贴,学习Java笔记都记在这里。所写内容都是个人菜鸟级的理解,肯定有疏漏不足。

    Anyway, 您的回复讨论支持是我前进的动力!

  ================= March 19, 2013 =========================

2.1很久没有回来,开始之前,先理解“面向对象编程”的几个关键的概念:

Object is the set of a class.
Object is the fundamental Element of your program.
Your processing code and data is packed into Class, so that your program can be used repeatly , flexibly and extentable .

【类】(Class)定义了一件事物的【抽象】特点。通常来说,类定义了事物的属性state, 和它可以做到的功能(它的行为Method)。
【对象】(Object)是类的【实体反应】。类是对象的抽象, 对象是类的具体表现形式。
【方法】(Method,可看成能力)是定义一个类可以做(can)的,但不一定会(will)去做的事。
【消息传递】(Message Passing)一个对象通过接受消息、处理消息、传出消息或使用其他类的方法来实现一定功能,这叫做消息传递机制。
【继承】继承性(Inheritance)是指,在某种情况下,一个类会有“子类”。子类具备父类的所有功能特点。一个父类可以有多个子类,子类之间的功能不能互用。最重要的特征,继承机制解决的软件的重用问题

至于封装性(Encapsulation),多态性/多型(Polymorphism), 抽象性(Abstraction),请参阅:1、wikipedia面向对象程序设计。 2、面向对象程序设计的4个主要特点

When you design a class, think about the objects that
will be created from that class type. Think about:

things the object knows
things the object does

For example:

   1.Shopping Cart
   ===============
    cartContents     — Knows
   —————
    addToCart()
    removeFromCart() — does 
    checkOut()

   2.Button
   ===============
    label
    color            — Knows
   —————
    setColor()
    setLabel()
    dePress()
    unDepress()      — does
   

Thins an object knows about itself are called: 
 -> Instance variables

For instance:
    Student
   ================
    Name
    Age             —-Knows, instance variables(state)
   —————-
    sing()
    run()
    walk()
    speak()         —-does, methods (behavior)
   —————-

2.2 我想再从强调Java的手动编译开始

          download JDK(Java Dev Kit) - Java Compiler Downloaded and Installed.

  Tell your PC to look the path of the compiler, set env. variable.

  in the command line: Type: 

	 >> javac     #(java compiler)

  if you can see the usage of it. done.

2.3 开始写java代码

        首先,学会编译,正如编译C代码一样,假如现在有个java源程序文件“example.java”。用 “ javac example.java ”命令行将 example.java 文件编译成 example.class 就可以用 java 命令执行:(这些概念应该具备)

	>> java example   # launch this example.class
	   Hello world!

        其次,引入IDE(Integrated Dev. Env.)集成开发环境: Eclipse

Eclipse has a built-in java compiler. 所以它代替了手工的编译和执行过程。
        一个基本的java程序结构:

1. public class apple{                              // define class "apple"
2. public static void main(String args[]){      // define main() method
3. System.out.println("Hello Java!");
4. }
5. }

        注意 public static void main(String[] args) 是什么意思:

这是java程序的入口地址, java虚拟机运行程序的时候首先找的就是main方法. 跟C语言里面的main()函数的作用是一样的.只有有main()方法的java程序才能够被java虚拟机执行,可理解为规定的格式。

对于里面的参数及修饰符:

public:表示的这个程序的访问权限,表示的是任何的场合可以被引用,这样java虚拟机就可以找到main()方法,从而来运行java程序

static:表明方法是静态的,不依赖类的对象的,是属于类的,在类加载的时候main()方法也随着加载到内存中去: 

  • When we want that a class member should be used independently of any object of that class, we have to precede that member’s declaration with the keyword static. 当我们要独立使用一个类成员,而不是使用属于这个类的对象,我们就必须要在声明那个类成员时,在它前面放置一个static关键字。
  • When a member is declared as static, it can be accessed without creating an object of the class and without referring to the object.That static is , a static member belongs to a class as a whole and not to any one instance/object of that class. 当一个类成员被声明为static, 它就可在没有建立对象的情况下被访问,也不需要引用这个对象。这个static就是指,它是一个隶属于这个类的静态的(固定的)成员,并且不属于任何一个即时/对象.
  • The static member is shared by all objects of that class.

void: main()方法是不需要返回值的

main:约定俗成,规定的

String args[] :从控制台接收参数:

  • If this is in reference to the “String[] args” passed as a parameter to the “public void static main()” method, then it refers to the list of arguments passed to the class when it is called. eg: When the following command is executed " » apple xxx yyy " then in the main method of the apple.java, the first parameter of args i.e. args[0] would have “xxx” and the second parameter of args i.e. args[1] would have “yyy”. Referring to the 3rd or any other higher element would throw a “IndexOutOfBounds” exception.

2.4 从头开始温习

回到《Java学习个人笔记(一)》,重新学习,完全自己理解,敲代码,完成Java程序编写需要具备的基本语法知识的复习。

2.4.1 Constructor

Constructor的使用,用于创建对class_Obj的使用时,同时设置class_Obj的初始参数。比如一个班的学生的姓名,年龄等。

  • ·理解Constructor的作用,出现的位置。它用于在初始化obj时设置参数,所以应该在非主class中。
  • ·Constructor的声明形式:它有点像声明一个method,但是没有返回类型。constructor的名称与class名须一致。Constructor体内多是赋值符。
  • ·副class结构:从本例可以看出,一个副class的结构大致为: class名-> class private variable 声明 -> Constructor声明 -> methods声明 (熟悉基本结构很重要)
  • ·Constructor在主class中被使用时的语法 pear pearObj = new pear(“Janney”,18); 。要注意。
1. //file: apple.java:
2. import java.util.Scanner;
3. 
4. public class apple{
5. public static void main(String Args[]){
6. Scanner scanObj = new Scanner(System.in);
7. pear 	pearObj = new pear("janney",18);
8. String name;
9. int age;
10. 
11. pearObj.put_method();// 1st print out
12. 
13. pearObj.get_name(scanObj.nextLine());//get name
14. pearObj.get_age(scanObj.nextInt());// get age
15. 
16. pearObj.put_method();// 2nd print out
17. }
18. }
19. 
20. // file: pear.java:
21. File: pear.java:
22. public class pear{
23. //var declaration:
24. private String girlname;
25. private int girlage;
26. //constructor:
27. public pear(String name, int age){  //public and private 就很好体现了java程序的封装性。
28. girlname = name;
29. girlage  = age;
30. }
31. // methods:
32. public void get_age(int age){
33. girlage = age;
34. }
35. public void get_name(String name){
36. girlname = name;
37. }
38. public void put_method(){
39. System.out.printf("Your girl's name is: %s, age is: %d\n", girlname, girlage);
40. }
41. }
2.4.2  Java中this关键字,public /private概念

当你理解了Constructor,并在声明Constructor时犯了“private pear(String name, int age)” 这样的错误以后,就该思考private / public的特点了。同时,在constructor 体内,变量名如“girlname”、“girlage”和声明时的入口变量如“name”/“age”之间能否相同的疑惑,也就引出了this的概念。

参见视频38: 视频:Java编程简明教程 by Thenewboston(Youtube)

===================== 2013年6月29日12:39:36 ====================

Write / Recite the Sample Code for Expressing the OOP programming method rather than POP programming method:

This is a Number Guessing Game, which contains three classes: gameLauncher, gamePlay, and Player

gameLauncher launch the game, game Play create the solution number and Player objects, and do the guessing and judging activity after while. Player class describe the properties of each player. It contains the instance variable - Num guessed and the method of guessing.

Here is the Java source code for that simple number guessing game(Refer to the book of: Head First Java 2nd Edition):

这个代码对于理解 OOP 的对象和class的基本概念,建立感性认识,加深理解,非常有帮助。默写5遍以上。每写一遍都会带给你新的收获!

1. // File: gameLauncher.java
2. 
3. public class gameLauncher{
4. public static void main(String args[]){
5. gamePlay playObj = new gamePlay();
6. playObj.play();
7. }
8. 
9. }
10. 
11. class gamePlay{
12. // Declare three players
13. Player p1;
14. Player p2;
15. Player p3;
16. // Create three marks for record the true/false result
17. 
18. boolean p1isright = false;
19. boolean p2isright = false;
20. boolean p3isright = false;
21. 
22. // For the solution Num:int number;
23. int number;
24. 
25. // Method play(); starts:
26. public void play(){
27. //Create the solution number:
28. number = (int)(Math.random()*10);
29. System.out.println("The solution num is:" + number);
30. // Create three players:
31. p1 = new Player();
32. p2 = new Player();
33. p3 = new Player();
34. 
35. // Start to judge
36. while(true){
37. p1.play();
38. p2.play();
39. p3.play();
40. 
41. if (number == p1.number) p1isright = true;
42. if (number == p2.number) p2isright = true;
43. if (number == p3.number) p3isright = true;
44. 
45. if (p1isright || p2isright || p3isright){
46. System.out.println("Player 1 is:" + p1isright);
47. System.out.println("Player 2 is:" + p2isright);
48. System.out.println("Player 3 is:" + p3isright);
49. break;
50. }
51. else{
52. System.out.println("No right answer, try again..\n");
53. }
54. }
55. }
56. }
57. 
58. class Player{
59. int number;
60. public void play(){
61. number = (int)(Math.random()*10);
62. System.out.println("I guess " + number);
63. }
64. }
Published At
comments powered by Disqus