08_java基础知识——方法重载

一、自变量顺序不同

package com.huawei.test.java04;

/**
 * This is Description
 *
 * @author 王明飞
 * @date 2018/08/29
 */
public class OverLoad {

    public static void print(String s,int i)
    {
        System.out.println("String s ="+s+"\nint i="+i);
    }

    public static void print(int i,String s)
    {
        System.out.println("int i="+i+"\nString s="+s);
    }
    public static void main(String[] args) {
       print(5,"kkkk");
       System.out.println("------------------");
       print("cccc",4);
    }
}

输出结果:

/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=53899:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/lib/tools.jar:/Users/yanliya/Downloads/chinaShop-master/java基础/java_base/target/classes com.huawei.test.java04.OverLoad
int i=5
String s=kkkk
------------------
String s =cccc
int i=4

Process finished with exit code 0

二、主类型重载

package com.huawei.test.java04;

/**
 * This is Description
 *
 * @author 王明飞
 * @date 2018/08/29
 */
public class OverLoad1 {
    static void prt(String s) {
        System.out.println(s); }
    void f1(char x) { prt("f1(char)"); } void f1(byte x) { prt("f1(byte)"); } void f1(short x) { prt("f1(short)"); } void f1(int x) { prt("f1(int)"); }
    void f1(long x) { prt("f1(long)"); } void f1(float x) { prt("f1(float)"); } void f1(double x) { prt("f1(double)"); }
    void f2(char x) { prt("f2(char)"); } void f2(byte x) { prt("f2(byte)"); } void f2(short x) { prt("f2(short)"); } void f2(int x) { prt("f2(int)"); } void f2(long x) { prt("f2(long)"); } void f2(float x) { prt("f2(float)"); }
    void f3(char x) { prt("f3(char)"); } void f3(byte x) { prt("f3(byte)"); } void f3(short x) { prt("f3(short)"); } void f3(int x) { prt("f3(int)"); } void f3(long x) { prt("f3(long)"); }
    void f4(char x) { prt("f4(char)"); } void f4(byte x) { prt("f4(byte)"); } void f4(short x) { prt("f4(short)"); } void f4(int x) { prt("f4(int)"); }
    void f5(char x) { prt("f5(char)"); } void f5(byte x) { prt("f5(byte)"); } void f5(short x) { prt("f5(short)"); }
    void f6(char x) { prt("f6(char)"); } void f6(byte x) { prt("f6(byte)"); }
    void f7(char x) { prt("f7(char)"); }
    void testDouble() {
        double x = 0;
        prt("double argument:"); f1(x);f2((float)x);f3((long)x);f4((int)x); f5((short)x);f6((byte)x);f7((char)x);
    }
    public static void main(String[] args) {
        OverLoad1 p = new OverLoad1();
        p.testDouble(); }
}

输出结果:

/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=53986:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/lib/tools.jar:/Users/yanliya/Downloads/chinaShop-master/java基础/java_base/target/classes com.huawei.test.java04.OverLoad1
double argument:
f1(double)
f2(float)
f3(long)
f4(int)
f5(short)
f6(byte)
f7(char)

Process finished with exit code 0

三:不能根据返回值类型来过载

原文地址:https://www.cnblogs.com/smallTiger123/p/9556683.html

时间: 2024-10-08 16:35:02

08_java基础知识——方法重载的相关文章

1.28 Java基础总结 ①方法重载②构造方法

1.28 Java基础总结 ①方法重载②构造方法 一.方法的重载在同一个类中,方法名相同,形参列表不同访问修饰符.返回类型和是否重载无关:按顺序对应的形参列表类型不同即可 二.构造器在类创建一个实例时被调用,构造方法只能用new关键字调用目的:初始化实例,初始化类属性(0,0.0,null,false)无返回值,和类名一致,用来构造对象作用:为对象分配内存, 创建并初始化成员变量(通过类创建,所以可以在static方法中调用构造方法) 返回引用,引用.属性:引用.方法(所以无返回值) 三.构造方

附录A培训实习生-面向对象基础(3):方法重载

就上一篇代码而言,你如果写Cat cat = new Cat();会直接报错错误 : 1       “Cat”方法没有采用“0”个参数的重载 E:\大话设计模式学习\BigDesignPatterns\附录A培训实习生之面向对象基础\动物运动会\AnimalGames\Form1.cs       20     23     AnimalGames 原因就是必要给小猫起名字.如果当真需要不起名字也要生出小猫来,可以采用方法重载. 方法重载提供了创建同名的多个方法的能力,但这些方法需使用不同的参

【编程基础】——方法重载、重写、重构

方法重载: 方法重载是指在一个类中定义多个同名的方法,但要求每个方法具有不同的参数的类型或参数的个数. 用方法重载,就能更多的满足更多的情况.比如: <span style="font-size:18px;"><span style="font-size:18px;"> class Cat { private string name = ""; public Cat(string name) { this.name = n

Java基础知识(重载和覆盖)

重载(overload): 在一个类中,如果出现了两个或者两个以上的同名函数,只要它们的参数的个数,或者参数的类型不同,即可称之为该函数重载了. 即当函数同名时,只看参数列表.和返回值类型没关系. 重载使用的时候需要注意: 1.在使用重载时只能通过不同的参数样式.例如,不同的参数类型,不同的参数个数,不同的参数顺序. 2.方法的异常类型和数目不会对重载造成影响. 3.对于父类来说,子类可以重载与继承父类中的方法同名的方法,如果某一方法在父类中是访问权限是priavte,那么就不能在子类对其进行重

scala入门-02基础知识-&gt;方法

首先在src下的main下的scala下创建一个包为org.scala.base 创建一个scala类名称为HelloWorld.scla 由于main方法要位于object对象中,所以我们创建一个object 名称为HelloWorld 并增加main方法和打印一行内容 接下来 我们在object对象中增加一个方法:printlnHello; PS: 1.函数体的最后一行的值就是整个函数的返回值 2.类型的申明是位于变量或者函数或者类的后面 另一种方式定义方法: 当方法or 函数是无参数时候,

Unity 3D 基础知识方法

A. 组件中默认的方法有如下:            Awake,Start,Update,OnGUI,OnDisable,OnEnable,OnDestory,LateUpdate,FixedUpdate Awake 每当脚本被加载时调用,游戏对象拥有该脚本即执行,适合作为初始化操作 OnEnable 每次激活脚本时,调用 Start 在第一次调用Update 之前 调用Start方法 Update 每帧调用一次Update,常用来处理画面逻辑 OnDisable  禁用脚本时,调用 Late

(5)java基础知识-方法、关键字、命名规则、注释

一.方法 方法相当于功能的实现. public   static   int          max    (int num1, int num2)   {....................} 修饰符     返回类型   方法名   方法(参数可以有N个) 方法体 修饰符: 修饰符是可选的,告诉编译器如何调用该方法.这定义了该方法的访问类型. static: 类方法,可通过类名直接调用 返回类型:方法可以返回一个值.returnValueType的值是方法返回的数据类型.有些方法没有返

java基础知识——方法

public static void main(String[] args) { //方法调用 double area=getArea(1.2); System.out.println(area); } /*修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2,......){ 执行语句 --- return 返回值; }*/ //修饰符:public  static //返回值类型:方法结束后想要给调用者的数据类型 //方法名:自己起 //参数列表(需要外界传入参与方法中运算的值

C++之基础知识20170830

/******************************************************************************************************************/ 一.C++类的引入 与C相比, 1.编译使用g++代替 gcc,执行在linux中还是一样的 2.c++里面也有struct并对其进行了扩展,struct中的函数可以直接使用其成员,并可在struct中直接实现, 例: struct person { char *n