java5:方法(method)

来源:互联网 发布:阿里云ecs 共享改独享 编辑:程序博客网 时间:2024/06/17 18:37
1 定义方法: Defining a Method
The syntax for defining a method is as follows:
modifier returnValueType methodName(list of parameters) {
// Method body;

}

The method header specifies the  modifiers, return value type, method name, and
parameters of the method.The variables defined in the method header are known as  formal parameters or simply
parameters. A parameter is like a placeholder. When a method is invoked, you pass a value to the
parameter. This value is referred to as an actual parameter or argument.

定义一个方法的语法 :

 修饰符  返回值 函数名(参数列表){

函数体

}

方法中参数列表的参数我们叫 形式参数 formal parameters,当一个方法被调用时,你穿进去的参数叫实际参数 actual parameters 或者 直接叫参数 argument

我们平时看到public static void main(String [] args); 的main函数也跟其他的函数没什么区别,只是main函数由JVM调用。public static 就是函数修饰符,public主要是权限修饰,static是表明该函数是一个类函数,也就是我们可以直接用 类名.方法名 来调用,这些在后面都会详细出来讲。

2 函数调用栈 (call stacks)

Each time a method is invoked, the system stores parameters and variables in an area of mem-
ory known as a stack, which stores elements in last-in, first-out fashion. When a method calls
another method, the caller’s stack space is kept intact, and new space is created to handle the
new method call. When a method finishes its work and returns to its caller, its associated
space is released.

当我们调用一个函数时候,系统会主动将调用者caller的参数及变量都存放在栈里边,当该函数调用其他函数时候,该函数的栈保持干净(没有修改的意思),然后会开辟一个新栈来处理新的函数调用,调用结束后该新开辟的栈自动销毁。

public class TestMax{public static void main(String [] args){int i=5;int j=2;int k=max(i,j);Sysmtem.out.println(k):}public static int max(int n1,int n2){int result;if(n1>=n2) result=n1;else result=n2;return result;}}

例如这个例子,我们来看栈的变换过程:


明白了调用栈,对我们理解栈是怎么调用的以及参数怎么传递等是非常有用的,可以以这样的调用栈的形式理解我们常用的那个在做两个数交换的栈的情况,以及为什么如果直接传入了两个int 结果并不影响原函数。

下面来看一个例子:

输入一个十进制的数,然后将她转为16进制并输出:

我的解法:

import java.util.Scanner;import java.lang.StringBuffer;public class DecimalToHex{public static void main(String [] agrs){Scanner input =new Scanner(System.in);System.out.print("Input the Decimal :");int dec=input.nextInt();StringBuffer s=new StringBuffer();//int q=-1;int remain=0;while(dec!=0){remain=dec%16;dec/=16;char ch;if(remain>=0&& remain<10)   //ch= (char)(remain+'0');   s.append(remain);else {ch=(char)(remain-10+'A');s.append(ch);}}System.out.println(s.reverse());}}

这个解法是自己使用了后面我们会讲到的StringBuffer(囧,刚刚写的时候忘了new Stringbuffer 后来一直丢出nullexception),别忘了最后用reverse函数逆序原Stringbuffer

课本上的解法是为了practice 类方法的使用,所以按下面的步骤

1 输入一个十进制数

2 调用decToHex 方法 ,该方法中还调用toHexChar 也就是将余数转为对应的char型。

3 输出

import java.util.Scanner;import java.lang.StringBuffer;public class DecimalToHex{public static void main(String [] agrs){Scanner input =new Scanner(System.in);System.out.print("Input the Decimal :");int dec=input.nextInt();System.out.println("the decimal "+dec+",convert to hex is "+decToHex(dec));}public static String decToHex(int n){String hex="";while(n!=0){int value=n%16;hex=toHexChar(value)+hex;n/=16;}return hex;}public static char toHexChar(int n){if(n<=9&&n>=0)return (char)(n+'0');elsereturn (char)(n-10+'A');}}
这种方法就将代码模块化了一些,只是decTohex中使用了String hex 来存储结果不是很好,在后面会看到,这样每循环一次,就会不断的创建新的String.





0 0
原创粉丝点击