JavaSE review diary: method invocation and method overloading

WBOY
Release: 2016-07-30 13:30:55
Original
1656 people have browsed it

<span>/*</span><span> *                          方法的调用和方法重载
 </span><span>*/</span><span>/*</span><span> *      什么是方法?
 *          方法就是一个有名字的代码段;
 *      方法的调用:
 *          在方法里调用另外一个方法里面的东西就是方法调用;
 *          或者可以认为"另外一个方法的名字()"就是方法的调用;
 *      方法的重载:
 *          就是在main方法外写了多个方法名相同,但是形参列表不同的方法,在main方法中调用这个方法时括号内写上实参,程序会默认调用实参和调用方法里形参相匹配的方法;
 </span><span>*/</span><span>//</span><span>                              方法形式和方法的类部调用</span><span>/*</span><span>public class JavaSE{
    public static void main(String[] args){
        JavaSE.Method_1();//方法的调用就是:类名.方法名(实参列表);
        Method_2(1,2);//main方法调用这个类里面的静态方法也可以这么写;
        Method_3(5,5);
    }
    public static void Method_1(){
        System.out.println( "我很帅" );
    }
    public static void Method_2(int a,int b){
        int c = a + b;
        System.out.println( c );
    }
    public static int Method_3(int e,int d){//注意这里static后面跟的是int,是返回值类型,这是方法最后必须写return语句;
        int f = e + d;
        System.out.println( f );
        return f;//return语句在有返回值类型的时候必须有返回值,不然会报错;
    }
}
</span><span>*/</span><span>//</span><span>-------------------------------------------------------------------------

</span><span>//</span><span>                          方法的重载</span><span>public</span><span>class</span><span> JavaSE{
    </span><span>public</span><span>static</span><span>void</span><span> main(String[] args){
       Method_4(</span><span>1</span>,<span>1.0</span>);<span>//</span><span>这里1是int型的,1.0是double型的,结果是2.0,结果自动转换为double型</span>       Java.sum(<span>2</span>,<span>1</span>);<span>//</span><span>调用外部类的方法必须是:外部类名.方法名(实参列表);</span><span>    }
    </span><span>public</span><span>static</span><span>void</span> Method_4(<span>int</span> a,<span>int</span><span> b){
        </span><span>int</span> c = a +<span> b;
    }
    </span><span>public</span><span>static</span><span>void</span> Method_4(<span>int</span> a,<span>double</span><span> b){
        System.</span><span>out</span>.println( a +<span> b );
    }
}

</span><span>class</span><span> Java{
    </span><span>public</span><span>static</span><span>void</span> sum(<span>int</span> a,<span>int</span><span> b){
        System.</span><span>out</span>.println( a +<span> b );
    }
    </span><span>public</span><span>static</span><span>void</span> sum(<span>int</span> a,<span>double</span><span> b){
        System.</span><span>out</span>.println( a -<span> b );
    }
}</span>
Copy after login

The above introduces the JavaSE review diary: method invocation and method overloading, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template