overloading

NOAH·2021년 1월 31일
0
post-thumbnail
package chapter6;
/*
 Overloading ?

 in one class, several method.

 void println()
 void println(boolean x)
 void println(char x)
 void println(char[] x)
 void println(double x)
 void println(float x)

 requirement
 1. method name should be same
 1. the number of parameter 'or' type should be different
 3. nothing to do with return type
...
 */
 
 
 
public class Ex6_10 {
    public static void main(String[] args){
        MyMath3 mm = new MyMath3();
        System.out.println("mm.add(3, 3) :" + mm.add(3,3));
        System.out.println("mm.add(3L, 3) :" + mm.add(3L,3));
        System.out.println("mm.add(3, 3L) :" + mm.add(3,3L));
        System.out.println("mm.add(3L, 3L) :" + mm.add(3L,3L));

        int[] a = {100, 200, 300};
        System.out.println("mm.add(a)  :" + mm.add(a));
    }
}

class MyMath3{

    int add(int a, int b){
        System.out.print("long add(int a, long b - ");
        return a + b ;
    }
    long add(long a, int b){
        System.out.print("long add(int a, long b - ");
        return a + b ;
    }
    long add(int a, long b){
        System.out.print("long add(int a, long b - ");
        return a + b ;
    }
    long add(long a, long b){
        System.out.print("long add(int a, long b - ");
        return a + b ;
    }

    int add(int[] a){
        System.out.print("int add(int[] a - " );
        int result = 0 ;
        for(int i = 0 ; i <  a.length; i++)
            result += a[i];

        return result;
    }

}

0개의 댓글