Download public static

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
方法(函數)
自訂方法的運用
內建的類別及方法
1
method的認識
• Java把函數稱為method
• method可用如下的語法來定義:
定義method
[public static]傳回值型態 method名稱(型態 引數1, 型態 引數2,...)
{
程式敘述 ;
return 運算式;
}
method的主體
若method沒有
傳回值,return
敘述可以省略
2
範例
import java.util.Scanner;
public class Example{
public static void main(String args[]){
int num;
Scanner input= new Scanner(System.in);
System.out.print("輸入num:");
num=input.nextInt();
System.out.println("輸出加總結果:"+sum(num));
呼叫sum method, 帶入num的值
}
int sum(int n){
傳入的引數為整數,引數名稱為n
int ans=0;
for(int i=1;i<=n;i++)
傳回值型態為整數
ans+=i;
return ans;
}
}
3
多載的概念
• 多載(overloading):
– 將功能相似的method,以相同名稱命名,編譯器會根據引
數的個數與型態,自動執行相對應的method
4
範例
//引數型態不同的函數多載
public class Example{
public static void main(String args[]){
int a=5, b[]={1,2,3,4};
show(a);
show(b);
}
void show(int i){
System.out.println("value="+i);
}
void show(int arr[]){
System.out.print("array=");
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}
5
練習
• 建立一java程式及area()方法
• 依據參數數量計算圓面積或矩形面積
6
字串類別
• String類別是放置在java.lang類別庫內
• java.lang類別庫裡所有的類別會自動載入
• 建立String物件的範例:
String str1=“abc”;
– 也可利用字元陣列來產生字串:
char str2[]={‘a’,’b’,’c’};
– 直接利用String建構元來建立字串:
String str3=new String(“abc”);
• 宣告之後無法修改內容
7
字串類別所提供的方法
• 參考網址 http://nothing.tw/JDK_API_1_6/java/lang/String.html
8
範例
public class Text1 {
public static void main(String args[]){
String str1 = "Providence University";
System.out.println(str1.charAt(5));
System.out.println(str1.indexOf("Pro"));
System.out.println(str1.length());
System.out.println(str1.substring(11));
System.out.println(str1.toLowerCase());
System.out.println(str1.toUpperCase());
}
------------Configuration: <Default> ------------
d
0
21
University
providence university
PROVIDENCE UNIVERSITY
}
9
StringBuffer類別庫
• 要修改字串,必須使用StringBuffer類別
• 下表列出常用的method:
• 可以修改字串內容
10
日期時間類別庫
• java.util.*
–
–
–
–
–
java.util.Calendar
java.util.GregorianCalendar
java.util.TimeZone
java.util.SimpleTimeZone
java.util.Date
• java.text.*
– java.text.DateFormat
– java.text.SimpleDateFormat
– java.text.DateFormatSymbols
11
java.util.Calendar
• Calendar 日曆類別
• 參考網址http://nothing.tw/JDK_API_1_6/java/util/Calendar.html
• 宣告物件取得現在的時間
Calendar today = Calendar.getInstance();
範例
import java.util.Calendar;
public class CalendarExample1{
public static void main(String args[])
{
Calendar today = Calendar.getInstance();
System.out.println("現在時區ERA:"+today.get(Calendar.ERA));
System.out.println("現在年份:"+today.get(Calendar.YEAR));
System.out.println("今天日期:"+today.get(Calendar.DATE));
System.out.println("今天是這個月的幾號:"+today.get(Calendar.DAY_OF_MONTH));
System.out.println("今天是這星期的第幾天:"+today.get(Calendar.DAY_OF_WEEK));
System.out.println("現在幾點:"+today.get(Calendar.HOUR));
}
}
java.text.DateFormat
• DateFormat是日期/時間格式化子類別的抽象類別。(設定
日期時間格式用。)
• 參考網址 http://nothing.tw/JDK_API_1_6/java/text/DateFormat.html
• 方法
–
–
–
–
getInstance()
getDateInstance()
getTimeInstance()
getDateTimeInstance()
• DateFormat.格式化樣式
–
–
–
–
FULL
LONG
MEDIUM
SHORT
範例
import java.text.DateFormat;
import java.util.Date;
public class DateFormatExample1 {
public static void main(String[] args) {
Date now = new Date();
System.out.println(" 1. " + now.toString());
System.out.println(" 2. " + DateFormat.getInstance().format(now));
System.out.println(" 3. " + DateFormat.getTimeInstance().format(now));
System.out.println(" 4. " + DateFormat.getDateTimeInstance().format(now));
System.out.println(" 5. " + DateFormat.getTimeInstance(DateFormat.SHORT).format(now));
System.out.println(" 6. " + DateFormat.getTimeInstance(DateFormat.MEDIUM).format(now));
System.out.println(" 7. " + DateFormat.getTimeInstance(DateFormat.LONG).format(now));
//續下頁
//承上頁
System.out.println(" 8. " + DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.SHORT).format(now));
System.out.println(" 9. " + DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.SHORT).format(now));
System.out.println("10. " + DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.LONG).format(now));
System.out.println("11. " + DateFormat.getDateTimeInstance(DateFormat.FULL,
DateFormat.FULL).format(now));
}
}
java.text.SimpleDateFormat
Symbol
G
y
Meaning
Era
Year
Type
Text
Number
Example
“GG” -> “AD”
“yy” -> “03″
“yyyy” -> “2003″
M
Month
Text or Number
“M” -> “7″
“M” -> “12″
“MM” -> “07″
“MMM” -> “Jul”
“MMMM” -> “December”
d
Day in month
Number
E
Day in week
Text
“d” -> “3″
“dd” -> “03″
“EEE” -> “Tue”
“EEEE” -> “Tuesday”
D
Day in year (1-365 or 1-364)
Number
F
Day of week in month (1-5)
Number
w
W
z
Week in year (1-53)
Week in month (1-5)
Time zone
Number
Number
Text
“D” -> “65″
“DDD” -> “065″
“F” -> “1″
“w” -> “7″
“W” -> “3″
“z” -> “EST”
“zzz” -> “EST”
“zzzz” -> “Eastern Standard Time”
java.text.SimpleDateFormat
Symbol
h
Meaning
Hour (1-12, AM/PM)
Type
Number
H
Hour (0-23)
Number
k
Hour (1-24)
Number
K
Hour (0-11 AM/PM)
Number
m
Minute
Number
s
Second
Number
S
a
Millisecond (0-999)
AM/PM
Number
Text
‘
”
Excape for text
Single quote
Delimiter
Literal
Example
“h” -> “3″
“hh” -> “03″
“H” -> “15″
“HH” -> “15″
“k” -> “3″
“kk” -> “03″
“K” -> “15″
“KK” -> “15″
“m” -> “7″
“m” -> “15″
“mm” -> “15″
“s” -> “15″
“ss” -> “15″
“SSS” -> “007″
“a” -> “AM”
“aa” -> “AM”
“‘hour’ h” -> “hour 9″
“ss”SSS” -> “45′876″
範例
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleExample1{
public static void main(String args[])
{
Date today = new Date();
SimpleDateFormat f1=new SimpleDateFormat("yyyy/M/d a h:m");
SimpleDateFormat f2=new SimpleDateFormat("yyyy/MMM/d H:m:s");
System.out.println(f1.format(today));
System.out.println(f2.format(today));
}
}
Related documents