zoukankan      html  css  js  c++  java
  • 方法

    一、方法申明及调用

    1.无参数调用

    public static void main(String[] args) {
    
    	nm();
    	}
    	public static void nm() {
    		System.out.println("sasa");
    	}
    

    2.有参数调用

    public static void main(String[] args) {
    		int e=method(2,3);
    		System.out.println(e);
    	}
    	public static int method(int a,int b) {
    		int c=a+b;
    		return c;
    		
    	}
    

    二、重载方法

    方法的重载是指一个类中可以定义多个方法名相同,但参数不同的方法。 调用时,会根据不同的参数自动匹配对应的方法

    构成方法重载的条件:

    1.不同的含义:形参类型、形参个数、形参顺序不同

    2.只有返回值不同不构成方法的重载

    public class test{
    public static void main(String[] args) {
    	int a=na(2,3);
    	double d=na(2,3,2.5);
    	boolean f=na(2,3.0);
    	System.out.println(a);
    	System.out.println(d);
    	System.out.println(f);
    }
    public static int na(int a,int b){
    int sum=a+b;
    return sum;
    }
    public static double na(int a,int b,double c){
    double multile=a*b*c;
    return multile;
    } 
    public static boolean na(int a,double b){
    boolean s=a>b?true:false;
    return s;
    }
    }
    
  • 相关阅读:
    【OpenCv/EmguCv】指针式仪表读数(二)
    合天网安实验室CTF练习赛之RE300
    Codeforces Round #527 -A. Uniform String(思维)
    CodeForces
    CodeForces
    CodeForces
    CodeForces
    CodeForces
    CodeForces
    CodeForces
  • 原文地址:https://www.cnblogs.com/zh93/p/12534069.html
Copyright © 2011-2022 走看看