java方法的调用

各种方法的调用实例

package cn.edu.fhj.day004;

public class FunctionDemo {
//    定义全局的变量
    public int a = 3;
    public int b = 8;
    
//    定义一个没有返回值的函数
    public void sayHello() {
        System.out.println("kkkhhh");
    }
    
//    无参数,有返回值
    public int  getSelfSum() {
        return a+b;
    }
    
//    有参,无返回值
    public void getName(String name ){
        System.out.println("kkk" + name);
    };
    
//    有参,有返回值,且是静态
    public static int getSumIsStatic(int number1 , int number2) {
        return number1+number2;
    }
    
    
//    参数,有返回值,非静态
    public int getSumNoStatic(int x) {
        return this.a + this.b + x;
    }
    
    
    
}

 

 

调用

package cn.edu.fhj.day004;

public class FunctionDemoTest {
//    public static void main(String[] args) {
//        FunctionDemo fun = new FunctionDemo();
//        fun.sayHello();
//    }
    
    
//    调用定义的有返回值的函数
//    public static void main(String[] args) {
//        FunctionDemo fun = new FunctionDemo();
//        
//        System.out.println(fun.getSelfSum());
//    }
    
    
//    调用有参,无返回值
//    public static void main(String[] args) {
//        FunctionDemo fun = new FunctionDemo();
//        fun.getName("fhj");
//    }
    
    
//    调用有参,有返回值
    public static void main(String[] args) {
        FunctionDemo fun = new FunctionDemo();
        System.out.println(fun.getSumNoStatic(3));
        
    }
    
    
    
    
    
}

 

posted @ 2019-01-07 14:50  cerofang  阅读(525)  评论(0编辑  收藏  举报