JAVA Practice notes - Zero 7

method

If we say that the value of life game character, attack power, magic value is an attribute, we can be created by declaring variables.

So, in the game control characters forward, open the bag, make attacking moves, release skills on all methods.

 

Defined methods

Package com.cnblogs.www.demo7; 

/ *     In general, the format is defined as a method 
      modifier static method return type name (parameter type parameter name) { 
          method body 
        return return value; 
    } 
 * / 
public  class MethodDefube1 {
     public  static  void main (String [] args) { 
        
        int X = sum (3,7);                          // call the method 
        System.out.println (X);                  

    } 
    
    // I sum method, and I can return two values 
    public  static  int SUM ( int a, int B) {          // definition of a method 
        int Result = a + b ;
        return result;
    }
}

 

 

Package com.cnblogs.www.demo7; 

// define a comparison of the method of 
public  class MethodDefube2 {
     public  static  void main (String [] args) { 
        
          int X =. 3 ;
           int Y =. 5 ;
           int Z = min (X, Y );                     // call the method                 
          System.out.println (x + "and" + y + "comparison, a minimum value:" + Z); 
          
       } 
     
       // I min method I can return the minimum of two values 
       public  static  int min ( int num1, int num2) {
           int Result;
          if (num1 > num2)
             result = num2;
          else
             result = num1;
     
          return result; 
     }
}

 

After the method definition, if you want to perform, be sure to call.

package com.cnblogs.www.demo7;

public class Method {
    public static void main(String[] args) {
        
          int x = 3;
          int y = 5;
          int c = min(x,y);                                //赋值调用
          System.out.println(c);                         
          
          int a = 2;
          int b = 1;                
          System.out.println(min(a,b));                    //打印调用    
       }
     
       public static int min(int num1, int num2) {         //min方法
          int result;
          if (num1 > num2)
             result = num2;
          else
             result = num1;
     
          return result; 
     }
}

 

方法的参数,实参和形参的区别

package com.cnblogs.www.demo7;

public class Parameter {
    public static void main(String[] args) {
        
        int x = sum(5,5);                             //实参是调用方法时,传给方法的值。
        System.out.println(x);
        
        System.out.println(sum(4,6));                 //4和6也是实参
    }
    
    public static int sum(int a,int b) {             //形参作用就是虚席以待,接收传入方法的实参     
        int result = a + b ;                         //准备将传入进来的值相加
        return result;
    }

}

 

方法的重载

package com.cnblogs.www.demo7;

public class chongzai {
 public static void main(String[] args) {

        System.out.println(sum(10,20));                    //输出30   参数个数是2
        System.out.println(sum(10,20,30));                 //输出60   参数个数是3
        System.out.println(sum(10,20,30,40));              //输出100  参数个数是4

    }

     //只需要唯一一个方法名称,就可以实现类似的多个功能。
    public static int sum(int a,int b) {                  //sum方法,参数个数2个,
        System.out.println("有两个参数方法的执行!");        
        return  a + b ;    
        
        //方法体和返回值类型也可以写成
//        int result = a + b;                              //这是将参数和赋值给一个,局部变量
//        return result;                                   //return 将这个局部变量作为返回值
    }

    public static int  sum(int a,int b,int c) {            //sum方法,参数个数3个
        System.out.println("有三个参数方法执行!");        
        return  a + b + c ;                                
    }

    public static int sum(int a,int b,int c,int d) {       //sum方法,参数个数4个
        System.out.println("有4个参数的方法执行");        
        return a + b + c + d ;                            
    }
}

 

Guess you like

Origin www.cnblogs.com/H742/p/11605707.html