JAVA/예제

[JAVA] ch05-13. Static method (스태틱 메서드) = 클래스메서드

밍글링글링 2017. 8. 22.
728x90
 
public class Ex13 {
    public static void main(String[] args){ // 클래스메서드=스태틱 메서드(객체 생성없이 사용할 수 있는 메서드)
        System.out.println(Calculator.add(300,200));
        System.out.println(Calculator.subtract(300,200));
        System.out.println(Calculator.multiple(300,200));
        System.out.println(Calculator.divide(300,200));
        
        Calculator caculator = new Calculator();
        caculator.a = 300;
        caculator.b = 200;
        
        System.out.println(caculator.add());
        System.out.println(caculator.subtract());
        System.out.println(caculator.multiple());
        System.out.println(caculator.divide());
    }
}

class Calculator{
    int a,b;
    
    int add(){ return a+b;}
    int subtract(){ return a-b;}
    int multiple(){ return a*b;}
    double divide(){ return a/b;}
    
    static int add(int a, int b){ return a+b;}
    static int subtract(int a, int b){ return a-b;}
    static int multiple(int a, int b){ return a*b;}
    static double divide(int a, int b){ return a/b;}
}

728x90

댓글