java基礎:2.1 方法、重載、隨機字符、方法抽象

java中名叫方法,在C語言裏咱們叫作函數。java

每當調用一個方法時,系統會建立一個活動記錄(也稱爲活動框架),用於保存方法中的參數和變量。活動記錄置於一個內存區域中,稱爲調用堆棧( call stack)。調用堆棧也稱爲執行堆棧、運行時堆棧,或者一個機器堆棧,常簡稱爲「堆棧」。當一個方法調用另外一個方法時,調用者的活動記錄保持不動,一個新的活動記錄被建立用於被調用的新方法。T-個方法結束返回到調用者時,其相應的活動記錄也被釋放。git

一個小程序,結合運用了方法和輸入重定向小程序

import java.util.Scanner;
public class A {
public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
		float core = input.nextFloat();
		while(core != 0) {
			System.out.println("the grade is :" + getGrade(core));
		core = input.nextFloat();
		}
	}
	
	public static char getGrade(float x) {
		if(x>=90)
			return 'A';
		else if(x>=80)
			return 'B';
		else return 'C';
    }
}

 

重載方法

重栽方法使得你可使用一樣的名字來定義不一樣方法,只要它們的簽名是不一樣的。框架

調用方法,Java編譯器尋找最精確匹配的方法。但有時調用一個方法時,會有兩個或更多可能的匹配,編譯器沒法判斷哪一個是
最精確的匹配。這稱爲歧義調用( ambiguous invocation)。歧義調用會產生一個編譯錯誤。dom

例如調用max(1,2),建立了兩個方法 max(int x1,double x2)和max(double x1,int x2),就會產生歧義,引發編譯報錯。函數

 

生成隨機字符

x +Math.random()8(y-x+1)  生成[x,y]之間的隨機數spa

在class 1中寫出隨機數生成函數,class 2中調用1中的函數。兩個程序分別爲3d

public class RandomCharacter {
	public static char getRandomCharacter(char chl, char ch2){
		return(char)(chl + Math.random()*(ch2 - chl + 1));}
	
	public static char getRandomLowerCharacter(){
		return getRandomCharacter('a','z');
	}
	public static char getRandomUpperCharacter(){
		return getRandomCharacter('A','Z');
	}
	public static char getRandomDigitCharacter(){
		return getRandomCharacter('0','9');
	}
	public static char getRandomCharacter(){
		return getRandomCharacter('\u0000','\uFFFF');
	}
}

 

public class TextRandomCharacter {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		final int number = 160;
		final int number_line = 20;
		for (int i=1;i<=number;i++) {
			char ch = RandomCharacter.getRandomLowerCharacter();
			if(i%number_line==0)
				System.out.println(ch);
			else System.out.print(ch);
			
		}
	}

}

方法抽象

方法抽象( method abstraction) 是經過將方法的使用和它的實現分離來實現的。用戶在不知道方法是如何實現的狀況下,就可使用方法。方法的實現細節封裝在方法內,對使用該方法的用戶來講是隱藏的。這就稱爲信息隱藏( information hiding) 或封裝(encapsulation)。若是決定改變方法的實現,但只要不改變方法簽名,用戶的程序就不受影響。方法的實現對用戶隱藏在「黑盒子」 中。code

假設要編寫一個程序,顯示給定年月的日曆。程序提示用戶輸入年份和月份,而後顯示該月的整個日曆orm

package basic_practice_001;

import java.util.Scanner;

public class PrintCalendar {

	public static void main(String[] args) {
		// TODO Ao-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the year(such as:1800,...,2018,...):");
		int year = input.nextInt();
		System.out.print("Enter the month(such as:1,2,...12):");
		int month = input.nextInt();
		printTitle(year,month);
	}
	
	
	public static void printTitle(int year,int month) {
		printMonthTitle(year,month);
		printMonthBody(year,month);
	}
	
	public static void printMonthTitle(int year,int month) {
		System.out.println("      " + getMonthName(month) + "    " +year);	
		System.out.println("------------------------------------------");	
		System.out.println("   Sun  Mon  Tue  Wed  Thu  Fri  Sat");
	}
	
	public static void printMonthBody(int year,int month) {
		int StartDay = getStartDay(year,month);  // 周幾開始
		int A_Month_day =getMonthDays(year,month);
		for(int i=StartDay;i>0;i--) 
			if(i==7) break;
			else System.out.print("     ");
		for(int i=1;i<=A_Month_day;i++) {
			System.out.printf("%5d",i);
			if((i+StartDay)%7 == 0) System.out.println();
		}
	}
	
	
	public static String getMonthName(int month) {
		String month_name = " ";
		switch(month) {
			case 1: month_name += "January";break;
			case 2: month_name += "Febrary";break;
			case 3: month_name += "March";break;
			case 4: month_name += "April";break;
			case 5: month_name += "May";break;
			case 6: month_name += "June";break;
			case 7: month_name += "July";break;
			case 8: month_name += "August";break;
			case 9: month_name += "September";break;
			case 10: month_name += "Octorber";break;
			case 11: month_name += "November";break;
			case 12: month_name += "December";break;
		}
		return month_name;
	}
	
	
	public static int getStartDay(int year,int month) {
		int totalDay = totalDay(year,month);
		int startDay_of_week = StartDayOfWweek(totalDay);
		return startDay_of_week;
	}
	
	public static int totalDay(int year,int month) {
		int sum=0;
		for(int i=1800;i<year;i++) {     // 1800年1月1日 週三
			if ( i%400==0 || (i%4==0 && i%100!=0) )
				sum += 366;
			else sum+=365;
		}
		for(int i=1;i<month;i++) {
			sum += getMonthDays(year,i);
		}
		return sum;	
	}
	

	public static int StartDayOfWweek(int totalDay) {
		final int week_1800_1_1 = 3;
		return (week_1800_1_1 + totalDay)%7;
	}
	
	public static int getMonthDays(int year,int month) {
		int i=month;  // 廢話,但懶得改後面If裏面的變量名稱了,因此加了這一句
		int A_Month_day;
		if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12) A_Month_day=31;
		else if(i==4 || i==6 || i==9 || i==11) A_Month_day=30;
			 else if(i==2 && ( year%400==0 || (year%4==0 && year%100!=0)))
				 	A_Month_day=29;
		else A_Month_day=28;	
	return  A_Month_day;
	}
}

結果以下:

相關文章
相關標籤/搜索