Java中使用方法的注意事项

 

                                                      Java方法使用的注意事项
java

本文列举了几个小白在java中使用方法应该注意的几个地方spa

1. 方法应该定义在类中
2.方法中不能够再嵌套方法
3.方法定义的先后顺序无所谓
4.想要执行方法必需要调用
5.若是方法有返回值必需要"return+返回值" 不能没有
6.void方法也能够使用return,可是后面不能够有返回值,这里return的做用至关于结束该方法的调用
package cn.itcast;

public class Test {
    public static void main(String[] args) {
        //在本类中的方法直接调用便可
        one();
        double two = two();
        System.out.println(two);
    }
    public static void one(){
        //return 10;该代码会报错,是错误写法
        return;//正确写法
    }
    public static double two(){
     //return  "这是字符串";返回值类型错误,会报错
        return  100;
    }
}

传入参数的一些注意事项code

package cn.itcast;

public class Test {
    public static void main(String[] args) {
       //three(14);调用该方法报错,由于参数列表中的个数与传入参数的个数要一致
      // three("faf",14);该方法也报错,由于参数列表中的参数与传入参数的类型也要一致
        three(10,10);//正确
    }
    public static void three(int a,int b) {

    }
}

 

 

 

 

 

以上是我对方法常见注意事项的一些总结blog

转载请标明博客连接:https://www.cnblogs.com/pjhaymy/three

相关文章
相关标签/搜索