码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 1327F AND Segments

时间:2020-08-10 14:31:19      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:line   res   har   its   多少   while   这一   差分   limit   

/*
    有些方法例如sumInt,sumLong,sunDouble方法虽然功能不同,
    但是功能相似,可以使用overload

    使用前提:功能相似的时候
*/
public class OverloadTest01
{
    public static void main(String[] args){
        
        //参数的类型不同,对应调用的方法不同
        //此时区分方法不再依靠方法名,依靠的是参数的数据类型
        System.out.println(sum(1,2));
        System.out.println(sum(1.0,2.0));
        System.out.println(sum(1L,2L));
    }

    public static int sum(int a,int b){
        return a + b;
    }

    public static long sum(long a,long b){
        return a + b;
    }

    public static double sum(double a,double b){
        return a + b;
    }
}
/*
    方法重载:
        
        1、方法重载又称为:overload
        2、什么时候考虑使用方法重载:
            功能相似的时候,尽可能让方法名相同

        3、什么条件满足之后构成了方法重载
            在同一个类
            方法名相同
            参数列表不同
                数量不同
                顺序不同
                类型不同


        4、方法重载和什么有关系,和什么没有关系
            方法重载和方法名+参数列表有关系
            方法重载和返回值类型无关,和修饰符列表无关
*/
public class OverloadTest02
{
    public static void main(String[] args){
        m1();
        m1(10);

        m2(1,2.0);
        m2(2.0,1);

        m3(10);
        m3(3.0);

        /*
        m4(1,2);
        m4(2,1)
        */
    }
    //以下两个方法构成重载:数量不同
    public static void m1(){}
    public static void m1(int a){}

    //以下两个方法构成重载:顺序不同
    public static void m2(int a,double b){}
    public static void m2(double a,int b){}

    //以下两个方法构成重载:类型不同
    public static void m3(int x){}
    public static void m3(double x){}

    /*
    //编译错误,方法重复
    public static void m3(int a,int b){}
    public static void m3(int b,int a){}
    */

    /*
    public static void x(){}
    public static int x(){
        return 1;
    }
    */

    /*
    void y(){}
    public static void y(){}
    */

}
public class OverloadTest03
{
    public static void main(String[] args){
        /*
        System.out.println("HelloWorld");
        */
        U.p("HelloWorld");
        U.p(10);
        U.p(5.0);
    }
}

//自定义类
class U
{
    public static void p(String b){
        System.out.println(b);
    }
    public static void p(int b){
        System.out.println(b);
    }
    public static void p(double b){
        System.out.println(b);
    }
}

 

CodeForces 1327F AND Segments

标签:line   res   har   its   多少   while   这一   差分   limit   

原文地址:https://www.cnblogs.com/Karry5307/p/13469525.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!