- 论坛徽章:
- 0
|
无参无返回值的方法,用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)); //调用方法
- }
- }
复制代码 |
|