-- Java 学习个人笔记(二)--
写这个个人笔记完全是出于记录自己所学,方便本人日后参考,可能很零散。这个笔记是建立在C语言编程基础上,本人学习Java只学习它与C语言不同的地方,或者我在C编程过程中很少用到的地方。所用的教材是Youtube一位达人做的视频:Java编程简明教程 by Thenewboston(Youtube)
每次开一贴,学习Java笔记都记在这里。所写内容都是个人菜鸟级的理解,肯定有疏漏不足。
Anyway, 您的回复讨论支持是我前进的动力!
================= March 19, 2013 =========================
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)
----------------
------------
>> javac #(java compiler)
if you can see the usage of it. done.
首先,学会编译,正如编译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程序结构:
public class apple{ // define class "apple" public static void main(String args[]){ // define main() method System.out.println("Hello Java!"); } }
注意 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.
---------------------------------
回到《Java学习个人笔记(一)》,重新学习,完全自己理解,敲代码,完成Java程序编写需要具备的基本语法知识的复习。
//file: apple.java: import java.util.Scanner; public class apple{ public static void main(String Args[]){ Scanner scanObj = new Scanner(System.in); pear pearObj = new pear("janney",18); String name; int age; pearObj.put_method();// 1st print out pearObj.get_name(scanObj.nextLine());//get name pearObj.get_age(scanObj.nextInt());// get age pearObj.put_method();// 2nd print out } } // file: pear.java: //// File: pear.java: public class pear{ //var declaration: private String girlname; private int girlage; //constructor: public pear(String name, int age){ //public and private 就很好体现了java程序的封装性。 girlname = name; girlage = age; } // methods: public void get_age(int age){ girlage = age; } public void get_name(String name){ girlname = name; } public void put_method(){ System.out.printf("Your girl's name is: %s, age is: %d\n", girlname, girlage); } }
当你理解了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遍以上。每写一遍都会带给你新的收获!
// File: gameLauncher.java public class gameLauncher{ public static void main(String args[]){ gamePlay playObj = new gamePlay(); playObj.play(); } } class gamePlay{ // Declare three players Player p1; Player p2; Player p3; // Create three marks for record the true/false result boolean p1isright = false; boolean p2isright = false; boolean p3isright = false; // For the solution Num:int number; int number; // Method play(); starts: public void play(){ //Create the solution number: number = (int)(Math.random()*10); System.out.println("The solution num is:" + number); // Create three players: p1 = new Player(); p2 = new Player(); p3 = new Player(); // Start to judge while(true){ p1.play(); p2.play(); p3.play(); if (number == p1.number) p1isright = true; if (number == p2.number) p2isright = true; if (number == p3.number) p3isright = true; if (p1isright || p2isright || p3isright){ System.out.println("Player 1 is:" + p1isright); System.out.println("Player 2 is:" + p2isright); System.out.println("Player 3 is:" + p3isright); break; } else{ System.out.println("No right answer, try again..\n"); } } } } class Player{ int number; public void play(){ number = (int)(Math.random()*10); System.out.println("I guess " + number); } }