Constructor 要取得跟該 class一樣的名字, 主要是在用 class 產生 object 時, initialize一些變數用
ex.
public class MyCat {
private catName;
public MyCat (String name) // constructor
{
catName = name;
}
}
of course multiple inputs are acceptable
public class MyCat {
private catName;
int catAge;
public MyCat (String name, int age) // constructor
{
catName = name;
catAge = age;
}
}
Tuesday, October 9, 2012
Primitive Types vs. Reference Types
boolean, byte, char, short, int, long, float and double - Primitive Types
A primitive-type variable can store exactly one value of its declared type at a time. For
example, an int variable can store one whole number (such as 7) at a time. When another
value is assigned to that variable, its initial value is replaced.
其他的都是 Reference Types (String, 所有其他的class)
A primitive-type variable can store exactly one value of its declared type at a time. For
example, an int variable can store one whole number (such as 7) at a time. When another
value is assigned to that variable, its initial value is replaced.
其他的都是 Reference Types (String, 所有其他的class)
Variables vs. Local Variable
variables 如果沒有先initialize的話, 會被令為0 (int, float, long), 或是null (String), 或是false (boolean)
local variable 是在method裡面的variable, 不能被其他的class使用
local variable 一定要initialize不然會有compile error
local variable 是在method裡面的variable, 不能被其他的class使用
local variable 一定要initialize不然會有compile error
UML
UML表示法
ClassName
--------------------------------------------
+ variableName : data type // + means a public variable
- variableName : data type // - means a private variable
--------------------------------------------
«constructor» ClassName ( inputName : String )
+ mehodName( attributeName : data type ) // + means a public method
+ mehodName( ) // the method might not need input
+ mehodName( ) : String // the method might return a type such as String, if not written, then it returns void
- mehodName( attributeName : data type ) // - means a private method
ClassName
--------------------------------------------
+ variableName : data type // + means a public variable
- variableName : data type // - means a private variable
--------------------------------------------
«constructor» ClassName ( inputName : String )
+ mehodName( attributeName : data type ) // + means a public method
+ mehodName( ) // the method might not need input
+ mehodName( ) : String // the method might return a type such as String, if not written, then it returns void
- mehodName( attributeName : data type ) // - means a private method
import小常識
記得要用JavaSE內建的class之前, 要先進行import的動作
ex.
import java.util.Scanner;
不然每次要用到Scanner的時候都需要另外在前面列出是哪個package的
java.util.Scanner input = new java.util.Scanner( System.in ); //麻煩
但是只有一個package是自動幫你import的, 那就是java.lang
裡面有包含System, String等等class, 所以不用另外再import囉!
ex.
import java.util.Scanner;
不然每次要用到Scanner的時候都需要另外在前面列出是哪個package的
java.util.Scanner input = new java.util.Scanner( System.in ); //麻煩
但是只有一個package是自動幫你import的, 那就是java.lang
裡面有包含System, String等等class, 所以不用另外再import囉!
Java Index
J
JOptionPane.showMessageDialog( null, "Hello World" ); // static method, 跳出對話視窗
JOptionPane.showInputDialog( "What is your name?" ); // static method, 跳出輸入視窗, 回傳輸入字串
S
Scanner
|| import java.util.Scanner;
|| Sacnner input= new Scanner(System.in); //input是抓鍵盤輸入的值
|| int number1 = input.nextInt() //nextInt(): 取得下一個input的Integer
|| String name = input.nextLine(); //nextLine(): 取得下一個input的字串
String.format( "%s, %d", name, number ); // static method, 回傳一個經過format的字串
System.out.printf( "%s\n%s\n", "Welcome to", "Java Programming!" ); // static method 印出字, 數字等等
System.out.println( "Hello World!" ); // static method 印出一行字"Hello World!"
JOptionPane.showMessageDialog( null, "Hello World" ); // static method, 跳出對話視窗
JOptionPane.showInputDialog( "What is your name?" ); // static method, 跳出輸入視窗, 回傳輸入字串
S
Scanner
|| import java.util.Scanner;
|| Sacnner input= new Scanner(System.in); //input是抓鍵盤輸入的值
|| int number1 = input.nextInt() //nextInt(): 取得下一個input的Integer
|| String name = input.nextLine(); //nextLine(): 取得下一個input的字串
String.format( "%s, %d", name, number ); // static method, 回傳一個經過format的字串
System.out.printf( "%s\n%s\n", "Welcome to", "Java Programming!" ); // static method 印出字, 數字等等
System.out.println( "Hello World!" ); // static method 印出一行字"Hello World!"
Wednesday, July 4, 2012
Java How to Program Ch3-3
Java How to Program Ch3-3
(1)
一個java檔案只能有一個public class, 在一個檔案定義兩個以上的public class會產生syntax error
(2)
A static method is special because it can be called without first creating an object of the class in
which the method is declared.
Static method 不需要創建Object就可以使用囉! 例如要執行主要工作的method "main", 在定義的時候就要用:
public static void main(String arg[]) { }
(3)
class instance creation expression 就是 new, e.x. Random r = new Random();
(4)
Random r = new Random();
以這個例子來說, Random後面的"( )"叫做constructor(建構子), 用在Object剛創造出來時, 去初始化該Object的變數(如果此class需要初始化變數的話...像這個例子就沒有初始化任何參數)
(5)
在java檔案被compile且execute的時候, main這個method會被自動執行, 不需要programmer去寫程式call它
(6)
By convention, method names begin with a lowercase first letter and all subsequent words in the name begin with a capital letter.
ex. methodNumberOne
(7)
javac *.java -> 將該folder下所有的.java檔進行compile!!
javac Alice.java Bob.java Candy.java ->complie這三個java檔案
(8)
What is UML
UML is a graphical language used by programmers to represent object-oriented systems in a standardized manner. 在思考一個龐大程式架構時十分有用
in UML, + displayMessage( ) ->+號的意思是該method是public!
Subscribe to:
Posts (Atom)