관리 메뉴

nalaolla

Method 예제 2 (Method 정리) 본문

JAVA/8. Method1

Method 예제 2 (Method 정리)

날아올라↗↗ 2016. 6. 22. 17:22
728x90
  1. package test.com;
  2.  
  3. public class Test01Method {
  4.  
  5.     // 1. member field : 속성
  6.     int x;
  7.     // 2. constructor : 생성 및 초기화
  8.     public Test01Method() {
  9.         System.out.println("객체생성시 : " + x);
  10.     }
  11.    
  12.     // 3. method : 기능, 동작
  13.     // 1) 매개변수 없고 리턴없는 매소드
  14.     public void aaa() {
  15.         System.out.println("test");
  16.         return//void의 경우 생략가능
  17.        
  18.     }
  19.     // 2) 매개변수 있고 리턴없는 메소드
  20.     public void aaa2(int x) {
  21.         System.out.println("aaa2()" + x);
  22.         return//void의 경우 생략가능
  23.        
  24.     }
  25.     // 3) 매개변수 없고 리턴있는 메소드
  26.     public int aaa3() {
  27.         System.out.println("aaa3()");
  28.         return 100;
  29.        
  30.     }
  31.     // 4) 매개변수 있고 리턴있는 메소드
  32.     public String aaa4(String name) {
  33.         System.out.println("aaa4() : " + name);
  34.         return "daniel";
  35.        
  36.     }
  37.     public static void main(String[] args) {
  38.         System.out.println("method");
  39.        
  40.         Test01Method tm =  new Test01Method();
  41.         //함수호출 : 메소드호출 : 메소드 콜
  42.         tm.aaa();   //반환값(리턴값)의 타입이 void인 경우 어떠한 타입에도 대입(할당)할 수 없다.
  43.        
  44.         tm.aaa2(100);
  45.        
  46.         int a3 = tm.aaa3();
  47.         System.out.println("tm.aaa3() : " + tm.aaa3());
  48.         System.out.println("a3 : " + a3);
  49.         System.out.println(tm.aaa3() > 90);
  50.        
  51.         String name = tm.aaa4("KIM");
  52.         System.out.println(name);
  53.         System.out.println(tm.aaa4("KIM").equals("aaa"));
  54.        
  55.     }
  56.  
  57. }


728x90

'JAVA > 8. Method1' 카테고리의 다른 글

Method 예제 5  (0) 2016.06.22
Method 예제 4  (0) 2016.06.22
Method 예제 3 (활용예제)  (0) 2016.06.22
Method 예제 1 (예외처리)  (0) 2016.06.22