12月30日学习内容

Java方法

Java方法他们可有返回值也可以没有返回值,可以有参数,也可以没有参数。

创建方法

public class Lx{
    public static void main(String[] args) {
    
}
    /*public static:修饰符
      int:返回值的类型
      funcName:函数名称(functiong)
      a,b:形式参数
      int a,int b:参数列*/
    public static int funcName(int a, int b){
        int i = 9;
        return i;
    }
}
modifier returnType nameOfMethod (方法头){方法体}
modifier:他定义了方法的访问类型,它是可选的。
returnType:方法是可能返回一个值的。
nameOfMethod:这是方法的名称。方法签名包括方法名称和参数列表。
Parameter List:参数列表,它是参数的次序,类型,以及参数个数的集合。
这些都是可选的,当然方法也可以没有参数。```
###方法体
######方法体定义了这个方法是用来做什么的。
####例子

public class Lx{
public static void main(String[] args) {

}
//下面的方法是接受两个参数n1和n2返回两者之间的最大值
public static int minFunction(int n1,int n2){
int min;
if (n1 > n2)
min = n2;
else
min = n1;

    return min;

}
}```

方法调用

使用一个方法,该方法必须要被调用。调用方法有两种方式,一种是有返回值的,另一种没有返回值。

定义方法和调用的例子

public class Lx{public static void main(String[] args) {
    int a = 11;
    int b = 6;
    int c = minFunction(a , b);
    System.out.println("Minimum Value = " + c);
}
    /***比较两个数的大小,取出最小数*/
    public static int minFunction(int n1, int n2){
        int min;
        if (n1 > n2)
            min = n2;
        else
            min = n1;

        return min;
    }
}```
运行的结果为:Minimum value = 6
###关键字 void
void 允许我们创建一个没有返回值的方法。
使用void方法是没有return
###示例:

public class Lx{public static void main(String[] args) {
//调用 void 方法必须声明meP(255.7)
meP(255.7);
}
public static void meP(double p){
if (p >= 202.5){
System.out.println("rank:a1");
}
else if (p >= 122.4){
System.out.println("rank:a2");
}
else{
System.out.println("rank:a3");
}
}
}```

方法的重载

当一个方法有两个或者更多的方法,他们的方法名字一样但是参数不同时,就叫做方法的重载。

例子:

public class Lx{
    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);
        double result2 = minFunction(c,d);
        System.out.println("Minimum Value = " + result1);
        System.out.println("Minimum Value = " + result2);
}
    public static int minFunction(int n1, int n2){
        int min;
        if (n1 > n2)
            min = n2;
        else
            min = n1;
        return min;
    }
    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
###构造函数

class MyClass {
int x;

// Following is the constructor
MyClass() {
x = 10;
}
}```

可以通过以下方法来调用构造函数来实例化一个对象:

public class Lx {

   public static void main(String args[]) {
      MyClass t1 = new MyClass();
      MyClass t2 = new MyClass();
      System.out.println(t1.x + " " + t2.x);
   }
}```
####简单的使用构造函数的例子:

public class Lx{

int age;

String color;

Lx() {
age = 10;
color = "black";
}
Lx(int a) {
age = a;
color = "white";
}
public static void main(String args[]) {

  Lx jhon = new Lx();
  Lx jerry = new Lx(20);

  System.out.println(" jerry: "+jerry.age + " " + jerry.color);
  System.out.println(" jhon: "+jhon.age + " " + jhon.color);

}

}```

可变长参数

方法声明时,你要在省略号前明确参数类型,并且只能有一个可变长参数,并且可变长参数必须是所有参数的最后一个

例子:

public class Lx {

   public static void main(String args[]) {
      // Call method with variable args  
      printMax(34, 3, 3, 2, 56.5);
      printMax(new double[]{1, 2, 3});
   }

   public static void printMax( double... numbers) {
   if (numbers.length == 0) {
      System.out.println("No argument passed");
      return;
   }

   double result = numbers[0];

   for (int i = 1; i <  numbers.length; i++)
      if (numbers[i] >  result)
      result = numbers[i];
      System.out.println("The max value is " + result);
   }
}```
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容