2012年7月30日 星期一
JAVA 類別變數 & 類別方法
類別變數
類別變數可以解釋為共同變數,當一個程式用同一個class產生2個物件(A物件,B物件),當A物件改變了類別變數,B的類別變數也會跟著改變.
類別方法
類別方法是可以不用使用new一個物件,就可以使用類別方法.
class CCircle // 定義類別CCircle
{
private int num=0; // 設定num為「類別變數」
private static double pi=3.14; // 設定pi為「類別變數」
private double radius;
public CCircle(double r){ // CCircle建構元
radius=r;
num++; // 當CCircle()建構元被呼叫時,num便加1
}
public void show(){
System.out.println("area="+pi* radius*radius);
}
public static void count(){ // count() method,用來顯示目前物件建立的個數 「 類別方法 」
System.out.println(num+" object(s) created");
}
}
public class app8_8
{
public static void main(String args[])
{
CCircle.count();
CCircle cir1=new CCircle(1.0);
cir1.count(); // 用cir1物件呼叫count() method
CCircle cir2=new CCircle(2.0);
cir1.count(); // 用cir1物件呼叫count() method
cir2.count(); // 改用cir2物件呼叫count() method
System.out.println("Math.abs(- 9.9)= "+Math.abs(-9.9));
}
}
{
private int num=0; // 設定num為「類別變數」
private static double pi=3.14; // 設定pi為「類別變數」
private double radius;
public CCircle(double r){ // CCircle建構元
radius=r;
num++; // 當CCircle()建構元被呼叫時,num便加1
}
public void show(){
System.out.println("area="+pi*
}
public static void count(){ // count() method,用來顯示目前物件建立的個數 「 類別方法 」
System.out.println(num+" object(s) created");
}
}
public class app8_8
{
public static void main(String args[])
{
CCircle.count();
CCircle cir1=new CCircle(1.0);
cir1.count(); // 用cir1物件呼叫count() method
CCircle cir2=new CCircle(2.0);
cir1.count(); // 用cir1物件呼叫count() method
cir2.count(); // 改用cir2物件呼叫count() method
System.out.println("Math.abs(-
}
}
2012年7月13日 星期五
JAVA 建盤輸入
import java.io.*;
public class app2_1
{
public static void main(String args[]) throws IOException
{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入數字 ");
String S1 = b.readLine();
System.out.println("輸入數字為 " + S1);
}
}
java.io
Class BufferedReader
java.lang.Objectjava.io.Reader
java.io.BufferedReader
- Direct Known Subclasses:
- LineNumberReader
StringreadLine()
Read a line of text.readLine
public String readLine() throws IOException
- Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
-
- Returns:
- A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
- Throws:
IOException- If an I/O error occurs
2012年7月11日 星期三
JAVA 命名規則
變數或方法從小寫字母開始
EX : myVariable
類別從大寫字母開始
EX : ShirtTest
常數識別字都常使用大寫字母和分離的字母加底線
EX : SALES_TAX
EX : myVariable
類別從大寫字母開始
EX : ShirtTest
常數識別字都常使用大寫字母和分離的字母加底線
EX : SALES_TAX
JAVA 資料型態
分類
|
保留字
|
名稱
|
Byte
|
有效範圍
|
整
數
|
byte
|
位元組
|
1
|
-128~127
|
short
|
智整數
|
2
|
-32,768~32,767
| |
int
|
整數
|
4
|
-2,147,483,648~2,147,483,647
| |
long
|
長整數
|
8
|
-9,223,372,036,854,775,808~
9,223,672,036,854,775,807
| |
浮
點
|
float
|
浮點數
|
4
|
負值-3.402823E38~-1.401298E-45
正值1.401298E-45~3.402823E38
|
double
|
倍精數
|
8
|
負值-1.797693134E3.8~4.9406564584124E-324
正值4.94.6564584E-324~1.797693134862E308
| |
其
他
|
char
|
字元
|
2
|
\u0000~\Uffff
|
boolean
|
布林值
|
2
|
true,false
|
Integer.parseInt (字串轉整數)
Integer.parseInt : 為字串轉整數
java.lang
java.lang
Class Integer
java.lang.Objectjava.lang.Number
java.lang.Integer
- All Implemented Interfaces:
- Serializable, Comparable<Integer>
Method Summary
Example:
public class app1_4
{
public static void main(String args[])
{
int n1 = Integer.parseInt(args[0]);
int n2 = Integer.parseInt(args[1]);
System.out.println("n1 + n2 = " + (n1 + n2));
}
}
訂閱:
意見 (Atom)














