zoukankan      html  css  js  c++  java
  • java基础:2.1 方法、重载、随机字符、方法抽象

    java中名叫方法,在C语言里我们叫做函数。

    每当调用一个方法时,系统会创建一个活动记录(也称为活动框架),用于保存方法中的参数和变量。活动记录置于一个内存区域中,称为调用堆栈( call stack)。调用堆栈也称为执行堆栈、运行时堆栈,或者一个机器堆栈,常简称为“堆栈”。当一个方法调用另一个方法时,调用者的活动记录保持不动,一个新的活动记录被创建用于被调用的新方法。T-个方法结束返回到调用者时,其相应的活动记录也被释放。

    一个小程序,结合运用了方法和输入重定向

    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)。歧义调用会产生一个编译错误。

    例如调用max(1,2),创建了两个方法 max(int x1,double x2)和max(double x1,int x2),就会产生歧义,引起编译报错。

    生成随机字符

    x +Math.random()8(y-x+1)  生成[x,y]之间的随机数

    在class 1中写出随机数生成函数,class 2中调用1中的函数。两个程序分别为

    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)。如果决定改变方法的实现,但只要不改变方法签名,用户的程序就不受影响。方法的实现对用户隐藏在“黑盒子” 中。

    假设要编写一个程序,显示给定年月的日历。程序提示用户输入年份和月份,然后显示该月的整个日历

    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;
    	}
    }
    

    结果如下:

  • 相关阅读:
    Educational Codeforces Round 85 D. Minimum Euler Cycle(模拟/数学/图)
    Educational Codeforces Round 85 C. Circle of Monsters(贪心)
    NOIP 2017 提高组 DAY1 T1小凯的疑惑(二元一次不定方程)
    Educational Codeforces Round 85 B. Middle Class(排序/贪心/水题)
    Educational Codeforces Round 85 A. Level Statistics(水题)
    IOS中的三大事件
    用Quartz 2D画小黄人
    strong、weak、copy、assign 在命名属性时候怎么用
    用代码生成UINavigationController 与UITabBarController相结合的简单QQ框架(部分)
    Attempting to badge the application icon but haven't received permission from the user to badge the application错误解决办法
  • 原文地址:https://www.cnblogs.com/l20902/p/10610954.html
Copyright © 2011-2022 走看看