Day 8 方法
1. 返回值【难点】
1.1 从生活中找出什么是返回值
以餐馆吃饭为例:
烤羊排 138元一份 声明方法
付款138元 给予实际参数
厨师做好烤羊排,服务员端菜上桌 厨师完成操作,给予的返回值
1.2 代码中返回值需要使用的关键字
return :
返回
作用:
1. 结束当前方法的执行
2. 返回return之后的数据到方法之外,要求返回值的数据类型和方法声明位置所告知调用者的返回值数据类型一致【数据类型一致化】
1.3 完成一个无参数有返回值的方法
1.3.1 方法分析
需求
give me five
返回一个正整数 5
固定格式:
public static
返回值类型:
返回值为5,为int类型
方法名:
giveMeFive
形式参数列表:
这里是返回一个数据5,不需要参数
()
【方法声明】
public static int giveMeFive()
@return
在文档注释中告知调用者,当前方法的返回值是什么类型,怎么解释,什么含义
1.3.2 方法实现和文档注释
public static int giveMeFive ( ) {
return 5 ;
}
1.3.3 方法调用过程
class Demo1 {
public static void main ( String[ ] args) {
int ret = giveMeFive ( ) ;
System. out. println ( ret) ;
}
public static int giveMeFive ( ) {
return 5 ;
}
}
1.3.4 小总结
1. 返回值的关键字为 return
2. 在带有返回值的方法中,需要在文档注释中加入 @return 告知调用者返回值的类型、含义
3. 方法返回值类型和方法体内返回数据类型要求一致,如果不一致,报错【数据类型不一致】
4. 方法的返回值处理方式,由程序员自己决定
1.3.5 小练习
class Method2 {
public static void main ( String[ ] args) {
char ch = 'a' ;
System. out. println ( "方法运行之前 :" + ch) ;
ch = giveMeLetter ( ) ;
System. out. println ( "方法运行之后 :" + ch) ;
}
public static char giveMeLetter ( ) {
return 'A' ;
}
}
1.4 完成一个有参数有返回值的方法
class Method3 {
public static void main ( String[ ] args) {
int sum = 0 ;
System. out. println ( "方法运行之前 :" + sum) ;
sum = getSumOfTwoNumber ( 10 , 20 ) ;
System. out. println ( "方法运行之后 :" + sum) ;
}
public static int getSumOfTwoNumber ( int num1, int num2) {
return num1 + num2;
}
}
总结:
调用带有多参数的方法,要求传入的钱参数数据类型、个数和顺序必须和方法声明一致
1.5 练习
class Method4 {
public static void main ( String[ ] args) {
maxOfNumber ( 15 , 63 ) ;
}
public static int maxOfNumber ( int num1, int num2) {
return num1 > num2 ? num1 : num2;
}
}
class Method5 {
public static void main ( String[ ] args) {
boolean ret = isMyUpper ( 'D' ) ;
System. out. println ( ret) ;
double min = minOfNumber ( 3.5 , 4.9 ) ;
System. out. println ( min) ;
}
public static boolean isMyUpper ( char ch) {
return ch >= 'A' && ch <= 'Z' ;
}
public static double minOfNumber ( double num1, double num2) {
return num1 < num2 ? num1 : num2;
}
}
class Method6 {
public static void main ( String[ ] args) {
boolean ret = isMyNumber ( '6' ) ;
System. out. println ( ret) ;
System. out. println ( isEnglish ( 'b' ) )
}
public static boolean isMyNumber ( char ch) {
return ch >= '0' && ch <= '9' ;
}
public static boolean isEnglish ( char ch) {
return ( ch >= 'A' && ch <= 'Z' ) || ( ch >= 'a' && ch <= 'z' ) ;
}
}
class Method7 {
public static void main ( String[ ] args) {
System. out. println ( toMyLower ( 'a' ) ) ;
reverse ( 123569 ) ;
}
public static char toMyLower ( char ch) {
if ( ch >= 'A' && ch <= 'Z' ) {
ch += 'a' - 'A' ;
}
return ch;
}
public static void reverse ( int num) {
while ( num > 0 ) {
System. out. print ( num % 10 ) ;
num /= 10 ;
}
}
}
class Method8 {
public static void main ( String[ ] args) {
System. out. println ( cube ( 12 ) ) ;
System. out. println ( isLeapYear ( 2000 ) ) ;
judgePositiveNunber ( 25 ) ;
}
public static double cube ( double num) {
return num * num * num;
}
public static boolean isLeapYear ( int year) {
return ( 0 == year % 4 && 0 != year % 10 ) || 0 == year % 400 ;
}
public static void judgePositiveNunber ( int num) {
System. out. println ( num > 0 ? "YES" : "NO" ) ;
}
}
2. 局部变量【小坑】
2.1 思考代码
class Method9 {
public static void main ( String[ ] args) {
int num = 5 ;
test ( num) ;
System. out. println ( num) ;
}
public static void test ( int num) {
num = 10 ;
}
}
2.2 概念
局部变量概念:
除class大括号之外,其他大括号以内的变量都可以认为是局部变量
方法、if else、switch case、循环大括号里都算
作用域:
局部变量有且只能在当前所在大括号以内使用且有效,超出范围无法使用
生存期:
在大括号以内,和作用域一样
for ( int i = 1 ; i <= 10 ; i++ ) {
if ( i > 5 ) {
int num = 10 ;
}
System. out. println ( num) ;
}
System. out. println ( num) ;
编译报错
因为num变量定义在if 大括号以内,作用域和生存期有且只在if 分支结构大括号以内,超出范围无效
for ( int i = 1 ; i <= 10 ; i++ ) {
}
for ( int i = 1 ; i <= 10 ; i++ ) {
}
不冲突,两个for 循环中,i循环变量分别属于不同的大括号以内,不同的作用域空间
for ( int i = 1 ; i <= 10 ; i++ ) {
for ( int i = 1 ; i <= 10 ; i++ ) {
}
}
编译报错
冲突,在外层for 循环中,已经定义了循环变量i, 内层循环重新定义了循环变量,也是i,重复定义变量
2.3 总结
1. 关注局部变量的作用域和生存期范围,二者同步并且都在对应的大括号以内
2. 变量未定义不能使用,要保证代码执行原则: 自上而下、从左至右