public class OverLoading {
	public static void main(String[] args){
		
		int a=5,b=10;

		//클래스 안쪽

		/*필드가 2개 있고,plus라는 메서드가 있어서*/
		Cal cal = new Cal();
		int c = cal.plus(a,b);
		
		System.out.println("더한 값은 = "+c);
	}
}

//클래스 바깥쪽
class Cal{
	int x;
	int y;
	
	public int plus(int a,int b){//int를 리턴하는 메서드
		return a+b;
	}
}






public class OverLoading {
	public static void main(String[] args) {
		
		int a=5, b=10;
		Cal cal=new Cal();
		
		int c=cal.plus(a, b);
		
		System.out.println("더한값은 "+c);
		
		/* 정수 3개를 받아 더하고 리턴하는 메서드를
		 *  만들어 사용 하세요 */
		c=cal.plus(99,33,22);
		System.out.println("3개를 더한값은 "+c);
		
		/* 정수 4개를 받아 더하고 리턴하는 메서드를
		 *  만들어 사용 하세요 */
		c=cal.plus(99,33,22,-88);
		System.out.println("4개를 더한값은 "+c);
		
		String z="안녕";
		String x=" // ";
		
		System.out.println(cal.plus(z, x));
		
		float f=-2.5f;
		
		Math.abs(f);//f값의 절대값을 리턴한다.
		System.out.println(Math.abs(f));
		
		double g=-3.4f;
		System.out.println(Math.abs(g));
		
		int i=-100;
		System.out.println(Math.abs(i));
		
		long j=-200L;
		System.out.println(Math.abs(j));
	}	
	// 클래스 안쪽	
}

// 클래스 바깥쪽
class Cal {
	int x;
	int y;
	
	//다형성의 한 축을 담당하는 오버로딩
	public String plus(String a, String b){
		return a+b;
	}
	public int plus(){
		return 777;
	}
	public int plus(int a, int b){  //int를 리턴하는 메서드 
		return a+b;
	}
	//메서드 오버로딩
	public int plus(int a, int b, int c){
		return a+b+c;
	}
	public int plus(int a,int b,int c,int d){
		return a+b+c+d;
	}
}





'Java > 2012.04 강좌' 카테고리의 다른 글

7일차 인스턴스 this, static  (0) 2012.04.12
7일차 혈핵형에 따른 다른 출력값, 생성자  (0) 2012.04.12
7일차 Data Hiding  (0) 2012.04.12
6일차 class연습2  (0) 2012.04.10
6일차 Class  (0) 2012.04.10

+ Recent posts