phpyangbo

无参无返回值的方法,用public void 方法名,来声明;

有参无返回值的方法,用public void 方法名,来声明;

有参有返回值的方法,用public int 方法名(int i,int n),来声明(int 是参数的数据类型指定,也可以是其它数据类型,例如:String、char、double、int)。

实例:

//定义类
public class Test{    
    //无参无返回值的方法
    public void eat(){
        System.out.println("我在吃饭。");
    }
    //有参无返回值的方法
    public void cheng(int i,int n){
        System.out.println(i*n);
    }
    //有参有返回值的方法
    public int getAge(int a){
        return a;
    }
    public static void main(String[] args){
        Test    Myclass    =    new Test();    //创建对象
        Myclass.eat();    //调用方法
        Myclass.cheng(9,9);    //调用方法
        System.out.println(Myclass.getAge(18));    //调用方法
    }
}

 

分类:

技术点:

相关文章:

  • 2021-05-13
  • 2021-07-27
  • 2022-12-23
  • 2021-08-05
  • 2022-12-23
  • 2022-01-10
  • 2021-07-10
  • 2021-07-21
猜你喜欢
  • 2021-11-09
  • 2021-06-16
  • 2021-09-16
  • 2022-12-23
  • 2021-07-05
  • 2021-11-09
  • 2021-09-13
相关资源
相似解决方案