美文网首页
12月30日学习内容

12月30日学习内容

作者: 萧灬瑟 | 来源:发表于2015-12-31 08:36 被阅读0次

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

相关文章

  • 学习内容

    语文学习d.t ,n,l ,g,k,h,j,q,x 数学学习了回答电视上的问题!还学的科学

  • 学习内容

    今天数学学了0-10书写,还学习了长方形、正方形还有球,语文学了6个单韵母和23个声母、还有整体认读

  • 学习内容

    今天数学学的一到六的合成,我学会了。语文学了几个复韵母,我学会了。回家去做作业啦!我做完啦。发一篇阅读我就睡觉啦。

  • 学习内容

    数学学习10以内的分成和合成!语文学的读一读!还有过马路儿歌

  • 学习内容

    今天上了书法课!练习了笔顺,数学还考试了!语文学习了秋天还有拼音

  • 学习内容

    语文学习了《古朗月行》,下午语文考试了!数学学的7加几,道德与法治课上老师让我们看的动画片,还发了冬季校服

  • 学习内容

    语文学了《谁会飞》《明天要远足》,数学学习了20以内的加法和连加,还有十以内的分成!上了体育课,还上了魔方课老师告...

  • 学习内容

    今天语文学习整体认读音节!声母,韵母,数学还做了一张试卷!我考了98分!下次争取考一百分!有三个男生考了一百分

  • 学习内容

    今天学习了道德与法制《开开心心上学校》《爱新书爱书包》数学学习了1-5正确书写!语文金木水火土上口耳手足

  • 学习内容

    今天我们上折纸课!老师教我们叠小鱼!因为我上接送站没有带彩纸!是我的同学魏路德送给我的的!

网友评论

      本文标题:12月30日学习内容

      本文链接:https://www.haomeiwen.com/subject/rcuihttx.html