클래스 함수

Computer/JAVA 2008. 11. 1. 15:31
package hello;

public class Arith {
        private int result=0;
        public Arith add(int n1){ //클래스 명을 함수로 넘기면 재귀함수처럼 계속 클래스 내에서 호출한다.
            result+=n1;
            return this;
        }
        public Arith min(int n1){
            result-=n1;
            return this;
        }
        public Arith mult(int n1){
            result*=n1;
            return this;   
        }
        public Arith div(int n1){
            result/=n1;
            return this;
        }
}

public class Calculator {
    public static void main(String args[]){
            Arith calc = new Arith();

           
            System.out.println(calc.min(3).add(83)); //이렇게 계속 함수를 호출가능.
           
    }
}

Posted by Triton
,