详解Java面向对象编程中方法的使用
|
一个 Java 方法是为了执行某个操作的一些语句的组合。举个例子来说,当你调用 System.out.println 方法时,系统实际上会执行很多语句才能在控制台上输出信息。 现在你将学习怎么创建你自己的方法,他们可以有返回值也可以没有返回值,可以有参数,也可以没有参数,重载方法要使用相同的方法名称,并在程序设计中利用抽象的方法。 创建方法
public static int funcName(int a,int b) {
// body
}
在这里
方法也包含过程或函数。
方法的定义包含方法头和方法体。如下所示:
modifier returnType nameOfMethod (Parameter List) {
// method body
}
以上的语法包括
示例 这是上面定义的方法max(),该方法接受两个参数num1和num2返回两者之间的最大值。
/** the snippet returns the minimum between two numbers */
public static int minFunction(int n1,int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
方法调用 调用方法很简单,当程序需要调用一个方法时,控制程序转移到被调用的方法,方法将会返回两个条件给调用者:
将返回void的方法作为一个调用语句,让我看下面的例子:
System.out.println("wiki.jikexueyuan.com!");
该方法的返回值可以通过下面的例子被理解: int result = sum(6,9); 示例 下面的例子表明了怎么定义方法和怎么调用它:
public class ExampleMinNumber{
public static void main(String[] args) {
int a = 11;
int b = 6;
int c = minFunction(a,b);
System.out.println("Minimum Value = " + c);
}
/** returns the minimum of two numbers */
public static int minFunction(int n1,int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
将会产生如下的结果 Minimum value = 6 关键字 void
public class ExampleVoid {
public static void main(String[] args) {
methodRankPoints(255.7);
}
public static void methodRankPoints(double points) {
if (points >= 202.5) {
System.out.println("Rank:A1");
}
else if (points >= 122.4) {
System.out.println("Rank:A2");
}
else {
System.out.println("Rank:A3");
}
}
}
这将产生如下的结果: Rank:A1 通过值来传递参数 通过值传递参数意味着调用方法的参数,通过参数值来传递给参数。 示例 下面的程序给出了一个例子来显示通过值来传递参数。在调用方法后参数值是不会发生变化的。
public class swappingExample {
public static void main(String[] args) {
int a = 30;
int b = 45;
System.out.println("Before swapping,a = " +
a + " and b = " + b);
// Invoke the swap method
swapFunction(a,b);
System.out.println("n**Now,Before and After swapping values will be same here**:");
System.out.println("After swapping,a = " +
a + " and b is " + b);
}
public static void swapFunction(int a,int b) {
System.out.println("Before swapping(Inside),a = " + a
+ " b = " + b);
// Swap n1 with n2
int c = a;
a = b;
b = c;
System.out.println("After swapping(Inside),a = " + a
+ " b = " + b);
}
}
这将产生如下的结果: Before swapping,a = 30 and b = 45 Before swapping(Inside),a = 30 b = 45 After swapping(Inside),a = 45 b = 30 **Now,Before and After swapping values will be same here**: After swapping,a = 30 and b is 45 方法的重载 让我们来考虑之前的找最小整型数的例子。如果我们要求寻找浮点型中最小的数时,我们就需要利用方法的重载来去创建函数名相同,但参数不一样的两个或更多的方法。 下面的例子给予解释:
public class ExampleOverloading{
public static void main(String[] args) {
int a = 11;
int b = 6;
double c = 7.3;
double d = 9.4;
int result1 = minFunction(a,b);
// same function name with different parameters
double result2 = minFunction(c,d);
System.out.println("Minimum Value = " + result1);
System.out.println("Minimum Value = " + result2);
}
// for integer
public static int minFunction(int n1,int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
// for double
public static double minFunction(double n1,double n2) {
double min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
这将产生如下结果: Minimum Value = 6 Minimum Value = 7.3 重载方法使程序易读。在这里,两种方法名称相同但参数不同。产生整型和浮点类型的最小数作为程序运行结果。 使用命令行参数 在命令行中,当要执行程序文件时,一个命令行参数是紧接着文件名字后面的出现的。要接受命令行参数在 Java 程序中是十分容易的。它们传递到 main 函数字符数组内。 示例 下面的例子展示了将所有命令行参数输出的程序:
public class CommandLine {
public static void main(String args[]){
for(int i=0; i<args.length; i++){
System.out.println("args[" + i + "]: " +
args[i]);
}
}
}
通过以下方法来执行该程序: java CommandLine this is a command line 200 -100 这将产生如下的结果: args[0]: this args[1]: is args[2]: a args[3]: command args[4]: line args[5]: 200 args[6]: -100 构造函数
// A simple constructor.
class MyClass {
int x;
// Following is the constructor
MyClass() {
x = 10;
}
}
你可以通过以下方法来调用构造函数来实例化一个对象:
public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass();
MyClass t2 = new MyClass();
System.out.println(t1.x + " " + t2.x);
}
}
通常,你将需要用构造函数来接受一个或多个参数。参数的传递和以上介绍的普通方法的参数传递是一样的,就是在构造函数的名字后面列出参数列表。 示例 这是一个简单的使用构造函数的例子:
// A simple constructor.
class MyClass {
int x;
// Following is the constructor
MyClass(int i ) {
x = i;
}
}
你可以通过以下方法来调用构造函数来实例化一个对象:
public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass( 10 );
MyClass t2 = new MyClass( 20 );
System.out.println(t1.x + " " + t2.x);
}
}
这将产生如下的结果: 10 20 可变长参数 typeName... parameterName 方法声明时,你要在省略号前明确参数类型,并且只能有一个可变长参数,并且可变长参数必须是所有参数的最后一个。 (编辑:哈尔滨站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
