728x90

 


Area 0 : R3, R4
Area 1 : R1, R3
Area 2 : R2, R4
Area 3 : R3, R4, R5, R6
 

root@R1# run show configuration
## Last commit: 2011-05-30 10:27:15 UTC by root
version 10.0R3.10;
system {
    host-name R1;
    root-authentication {
        encrypted-password "$1$Le7q3/yU$l2sBlzemteXJhXQBnqnFX."; ## SECRET-DATA
    }
    services {
        ssh {
            root-login allow;
        }
    }
    syslog {
        user * {
            any emergency;
        }
        file messages {
            any notice;
            authorization info;
        }
        file interactive-commands {
            interactive-commands any;
        }
    }              
}
interfaces {
    em1 {
        unit 0 {
            family inet {
                address 192.168.10.1/24;
            }
        }
    }
    em2 {
        unit 0 {
            family inet {
                address 192.168.1.1/24;
            }
        }
    }
    em3 {
        unit 0 {
            family inet {
                address 192.168.3.1/24;
            }
        }
    }              
    lo0 {
        unit 0 {
            family inet {
                address 1.1.1.1/32;
            }
        }
    }
}
protocols {
    ospf {
        area 0.0.0.1 {
            stub default-metric 20; // default metric 값 20 으로 변경 0.0.0.0
            interface em2.0;
        }
    }
}

root@R2# run show configuration
## Last commit: 2011-05-27 11:41:31 UTC by root
version 10.0R3.10;
system {
    host-name R2;
    root-authentication {
        encrypted-password "$1$Le7q3/yU$l2sBlzemteXJhXQBnqnFX."; ## SECRET-DATA
    }
    services {
        ssh {
            root-login allow;
        }
    }
    syslog {
        user * {
            any emergency;
        }
        file messages {
            any notice;
            authorization info;
        }
        file interactive-commands {
            interactive-commands any;
        }
    }              
}
interfaces {
    em1 {
        unit 0 {
            family inet {
                address 192.168.20.4/32;
            }
        }
    }
    em2 {
        unit 0 {
            family inet {
                address 192.168.8.2/24;
            }
        }
    }
    em3 {
        unit 0 {
            family inet {
                address 192.168.3.2/24;
            }
        }
    }              
    lo0 {
        unit 0 {
            family inet {
                address 1.1.1.2/32;
            }
        }
    }
}
protocols {
    ospf {
        area 0.0.0.2 {
            stub default-metric 10 no-summaries;
            interface em2.0;
        }
    }
}

root@R3# run show configuration
## Last commit: 2011-05-30 10:28:45 UTC by root
version 10.0R3.10;
system {
    host-name R3;
    root-authentication {
        encrypted-password "$1$Le7q3/yU$l2sBlzemteXJhXQBnqnFX."; ## SECRET-DATA
    }
    services {
        ssh {
            root-login allow;
        }
    }
    syslog {
        user * {
            any emergency;
        }
        file messages {
            any notice;
            authorization info;
        }
        file interactive-commands {
            interactive-commands any;
        }
    }              
}
interfaces {
    em0 {
        unit 0 {
            family inet {
                address 192.168.5.3/24;
            }
        }
    }
    em1 {
        unit 0 {
            family inet {
                address 192.168.2.3/24;
            }
        }
    }
    em2 {
        unit 0 {
            family inet {
                address 192.168.1.3/24;
            }
        }
    }              
    em3 {
        unit 0 {
            family inet {
                address 192.168.4.3/24;
            }
        }
    }
    lo0 {
        unit 0 {
            family inet {
                address 1.1.1.3/32;
            }
        }
    }
}
protocols {
    ospf {
        area 0.0.0.1 {
            stub default-metric 20;
            interface em2.0;
        }
        area 0.0.0.0 {
            interface em3.0;
            interface lo0.0;
        }
        area 0.0.0.3 {
            interface em1.0;
            interface em0.0;
        }
    }
}

root@R4# run show configuration
## Last commit: 2011-05-27 14:00:08 UTC by root
version 10.0R3.10;
system {
    host-name R4;
    root-authentication {
        encrypted-password "$1$Le7q3/yU$l2sBlzemteXJhXQBnqnFX."; ## SECRET-DATA
    }
    services {
        ssh {
            root-login allow;
        }
    }
    syslog {
        user * {
            any emergency;
        }
        file messages {
            any notice;
            authorization info;
        }
        file interactive-commands {
            interactive-commands any;
        }
    }              
}
interfaces {
    em0 {
        unit 0 {
            family inet {
                address 192.168.6.4/24;
            }
        }
    }
    em1 {
        unit 0 {
            family inet {
                address 192.168.9.4/24;
            }
        }
    }
    em2 {
        unit 0 {
            family inet {
                address 192.168.8.4/24;
            }
        }
    }              
    em3 {
        unit 0 {
            family inet {
                address 192.168.4.4/24;
            }
        }
    }
    lo0 {
        unit 0 {
            family inet {
                address 1.1.1.4/32;
            }
        }
    }
}
protocols {
    ospf {
        area 0.0.0.0 {
            interface em3.0;
            interface lo0.0;
        }
        area 0.0.0.2 {
            stub default-metric 10 no-summaries;
            interface em2.0;
        }
        area 0.0.0.3 {
            interface em0.0;
            interface em1.0;
        }
    }
}

root@R5# run show configuration
## Last commit: 2011-05-27 13:56:54 UTC by root
version 10.0R3.10;
system {
    host-name R5;
    root-authentication {
        encrypted-password "$1$Le7q3/yU$l2sBlzemteXJhXQBnqnFX."; ## SECRET-DATA
    }
    services {
        ssh {
            root-login allow;
        }
    }
    syslog {
        user * {
            any emergency;
        }
        file messages {
            any notice;
            authorization info;
        }
        file interactive-commands {
            interactive-commands any;
        }
    }              
}
interfaces {
    em0 {
        unit 0 {
            family inet {
                address 192.168.6.5/24;
            }
        }
    }
    em1 {
        unit 0 {
            family inet {
                address 192.168.2.5/24;
            }
        }
    }
    em2 {
        unit 0 {
            family inet {
                address 192.168.7.5/24;
            }
        }
    }              
    lo0 {
        unit 0 {
            family inet {
                address 1.1.1.5/32;
            }
        }
    }
}
routing-options {
    static {
        route 100.100.100.0/24 next-hop 192.168.7.6; // static 경로를 테이블에 올리기 위해 임으로 설정함.
    }
}
protocols {
    ospf {
        export redis_static;
        area 0.0.0.3 {
            interface em0.0;
            interface em1.0;
            interface em2.0;
            interface lo0.0;
        }
    }              
}
policy-options {
    policy-statement redis_static {    //static경로를 ospf로 재분배 하는 정책
        from protocol static;
        then accept;
    }
}

root@R6# run show configuration
## Last commit: 2011-05-27 13:59:36 UTC by root
version 10.0R3.10;
system {
    host-name R6;
    root-authentication {
        encrypted-password "$1$Le7q3/yU$l2sBlzemteXJhXQBnqnFX."; ## SECRET-DATA
    }
    services {
        ssh {
            root-login allow;
        }
    }
    syslog {
        user * {
            any emergency;
        }
        file messages {
            any notice;
            authorization info;
        }
        file interactive-commands {
            interactive-commands any;
        }
    }              
}
interfaces {
    em0 {
        unit 0 {
            family inet {
                address 192.168.5.6/24;
            }
        }
    }
    em1 {
        unit 0 {
            family inet {
                address 192.168.9.6/24;
            }
        }
    }
    em2 {
        unit 0 {
            family inet {
                address 192.168.7.6/24;
            }
        }
    }              
    lo0 {
        unit 0 {
            family inet {
                address 1.1.1.6/32;
            }
        }
    }
}
routing-options {
    static {
        route 200.200.200.0/24 next-hop 192.168.7.5;
    }
}
protocols {
    ospf {
        export redis_static;
        area 0.0.0.3 {
            interface em0.0;
            interface em1.0;
            interface em2.0;
            interface lo0.0;
        }
    }              
}
policy-options {
    policy-statement redis_static {
        from protocol static;
        then accept;
    }
}

728x90

객체 = 인스턴스 = 클래스변수(잘 사용 안함)  //  객체를 가장 많이 쓴다.

- 오버로딩 (중목 메서드)
  같은 클래스내에 메서드 이름이 같은 메서드가 여러개 존재할때
  이때 구분은 매개변수(인수, 인자)로 한다.   매개변수 순서가 바뀌어도 자료형이 틀려서 구분 가능하면 OK
------------------------------------------------------
Overloading 예제
------------------------------------------------------
class Test_7
{
 public int sum(int a, int b)
 {
  System.out.println("sum(int a, int b)...");
  int s= a+b;
  return s;
 }
 public float sum(float a, float b)
 {
  System.out.println("sum(float a, float b)...");
  float s = a+b;
  return s;
 }
 public double sum( double a, double b)
 {
  System.out.println("sum(double a, double b)...");
  double s = a+b;
  return s;
 }
}// end Test_7

class Method_7
{
 public static void main(String args[])
 {
  Test_7 A = new Test_7();
  int a1 = A.sum(1,2);
  float a2 = A.sum(1.3f, 1.3f);
  double a3A.sum(1.34d,1.345d);  
 }//end main
}//endl Test_7
-----------------------------------------------------------------
예제 (매개변수로 배열을 넘기는 경우)
-----------------------------------------------------------------
class Test_9
{
 public void aa(int ar[], int y) // a = ar
 {
  for(int i = 0; i<ar.length; i++)
  {
   ar[i]+=y;
  }
 } 
}
class Method_9
{
 public static void main(String args[])
 {
  int a[] = {10,20,30};
  
  Test_9 A = new Test_9();
  A.aa(a,10);  //인자값으로  배열(a)와 10 을  넘긴다.
  
  disp(a);  // static메소드안에서는 static메소드만 호출이 가능하다. 
 }
 
 public static void disp(int a[])  //   static 빠지면 컴파일 에러!
 {
  for(int i = 0; i<a.length; i++)
  {
   System.out.print(a[i]+"  ");
  }
 }
}
----------------------------------------------------------------------
- this  ( 자신의 객체를 가리킨다.)
----------------------------------------------------------------------
class Sinsang{
 //맴버변수 = 필드선언
 String name;
 int age;
 double ki;
 
 public void setData(String name , int age, double ki)
 {
  this.name            =              name;
 //자신의 객체 name        매개변수에서 넘어온 name
  this.age= age;
  this.ki =  ki;
 }
 public String getName(){
  return name;
 }
 public void disp(){
  System.out.println("이름 :" + name);
  System.out.println("나이 :" + age);
  System.out.println("키 :" + ki);
 }
}

class Method_10
{
 public static void main(String args[])
 {
  Sinsang A = new Sinsang();
  A.setData("유승호", 19, 180.3);
  A.disp();
  
  System.out.println();
  
  System.out.println("이름 : "+ A.getName());
  
  System.out.println();
  
  A.setData("이민정", 26, 160.3);
  A.disp();
  
  System.out.println();
  
  A.setData("신세경",19,163.3);
  A.disp();
  
 }//main

}//class
-----------------------------------------------------------------

- static 변수, 매서드

 static메서드는 static으로 선언된 변수,메서드만 호출 할 수 있다.
 static메서드는 객체 생성 하지 않고도 호출 할 수 있다.
 클래스명.메서드(); 
 클래스이름.맴버변수;
 static 변수는 모든 객체가 공유한다.

 main함수가 static으로 선언된 이유.
  -  main함수는 JVM이 객체 생성 없이 바로 호출해야 하기 때문이다.
----------------------------------------------------------------
 예제  (static메서드는 객체 생성 하지 않고도 호출 할 수 있다. )
----------------------------------------------------------------
class Test_1{
 public static int su = 100;
 
 public static void kk(){
  System.out.println("kk()  call.....");
  su+=100;
 }
}

class StaticMethod_1
{
 public static void main(String args[]){
  System.out.println(Test_1.su);// static 맴버 번수 출력(객체 생성 전)
  
  Test_1.kk(); //static 매서드 호출
  
  System.out.println(Test_1.su);// static 맴버 변수 출력(객체 생성 후)
  
  Test_1 A = new Test_1();
  System.out.println(A.su);
  A.kk();
  System.out.println(A.su);

 }//end main
}//end StaticMethod_1
----------------------------------------------------------------------
예제1 (static메서드는 static으로 선언된 변수,메서드만 호출 할 수 있다.)
----------------------------------------------------------------------

class StaticMethod_2
{
 int a = 100; 
 static int b = 200;
 
 public static void main(String args[]){
  //System.out.println(a);// Error!!!  static메서드는 static변수만 참조가능
  System.out.println(b);
  
  // aa(); // Error!!!  static메서드는 static 메서드만 호출 가능
  bb();
 }
 public void aa(){
  System.out.println("aa() call");
 }
 public static void bb(){
  System.out.println("bb() call");
 }
}
----------------------------------------------------------------------
예제2 (static메서드는 static으로 선언된 변수,메서드만 호출 할 수 있다.)
----------------------------------------------------------------------
/*
자신의 클래스 내의 메서드 호출
다른 클래스 내의 메서드 호출
 */
class Btest
{
 void b(){
  System.out.println("bbb");
 }
}
class Atest
{
 public static void main(String args[]){
  
  //a();   //Error!!  static 메서드는 static 메서드만 호출 가능
  Atest A = new Atest();
  A.a();  // 이경우에는 객체를 생성했기 때문에 가능.
  
  Btest B = new Btest();
  B.b();
 }
 void a(){
  System.out.println("aaa");
 }
}

-------------------------------------------------------------
static (정상적인 활용)
-------------------------------------------------------------
class Singsang{
 String name;
 int age;
 static int sungin = 20; // 미성년자 판정 나이는 결정되어 있으므로 변할 필요가 없고 공유되도 상관없다.
 
 public void setData(String name, int age){
  this.name = name;
  this.age = age;
 }
 public void disp(){
  String temp;
  if(age>=sungin){
   temp= "성인";
  }else{
   temp = "미성년자";
  }
  System.out.println( name + " 님은 "+ age + "살이고 " +  temp);
 }
}

class StaticMethod_3
{
 public static void main(String args[]){
  Singsang kim = new Singsang();
  Singsang lee = new Singsang();
  Singsang park = new Singsang();
  
  kim.setData("kim",18);
  lee.setData("lee",21);
  park.setData("park",22);
  
  kim.disp();
  lee.disp();
  park.disp();
  
 }//end main

}
----------------------------------------------------
static ( 잘못된 예제)
 ---------------------------------------------------
class Sangpum{
 String pum;
 int su;
 static int dan; // dan에다가 계속 단가를 덥어씌운다. 그래서 마지막 단가(딸기) 로 몽땅 출력됨. 
 public void setData(String pum, int su, int dan){
  this.pum=pum;
  this.su=su;
  this.dan=dan;
 }
 public void disp(){
  System.out.println(pum + "~~~" +su+ "~~~" + dan);
 }
}

class StaticMethod_4{
 public static void main(String args[]){
  Sangpum A = new Sangpum();
  Sangpum B = new Sangpum();
  Sangpum C = new Sangpum();
  
  A.setData("사과",3,2000);
  B.setData("감",10,300);
  C.setData("딸기",2,2500);
  
  A.disp();
  B.disp();
  C.disp();
 }
}

 - 생성자
  1. 클래스명과 같은 이름을 기재
  2. 리턴형이 없다.
  3. 필드 초기화 (목적)
  4. 인수를 갖는다 (오버로딩 가능)
  5. 객체 싱성시 자동호출

 *사용자가 생성자를 새로 정의하면 default생성자는 생성이 안된다.  이럴 경우 습관적으로 default 생성자를 만들어줘야함.
 
  생성자를 안쓰면 defalut 생성자를 호출한다. ( default생성성자는 아무 기능 없음.)
  사용자가 생성자를 만들면 default 생성자는 없어진다.

  this()     자신의 클래스 생성자 호출
  super()  상위 클래스 생성자 호출
  this       자신의 객체 가리킴
  super    상위 객체 가리킴( 상속관계일때)

   * static 메서드 에서는 this 사용못함 (왜냐면 자신이 2번 호출 되니까)
     
static 변수 , static메서드는 클래스랑 같이 메모리(정적메모리) 에 할당되어있다.
     같이 할당되어있기 때문에 this를 사용하면 자신을 2번 지칭하기 때문에..

int a = 100; // 전역변수나 static변수나 마찬가지로 메모리에 프로그램이 끝날때까지 할당되어있다.

class ConstTest_1
{
   public static void main(String args[])
      {
         this.a = 100; error!!!
       }
}

 예제 (생성자 Overloading)
----------------------------------------------------------------------------
class Cons
{
 public Cons()//default 생성자
 {
  System.out.println("Cons()");
 }
 public Cons(int a)
 {
  System.out.println("Cons(int a)");
 }
 public Cons(String name, int age)
 {
  System.out.println("Cons(String name, int age)");
 }
}

class ConTest_1
{
 public static void main(String args[])
 {
   new Cons();   //객체 생성, 생성자는 자동 호출
  new Cons(100);
  new Cons("김씨",200);

 }
}
--------------------------------------------------------

728x90

- Array(배열)
  동질자료형의 기억장소 집합체
  배열길이(배열요소갯수) 구하는 법.... 배열이름.length  // args.length
  배열은 지역에 선언해도 자동 초기화 된다.
  장점  1. 자료를 일괄처리 할 수 있다.
            2. new연산자를 사용하여 생성한다.      // int a[] = new int [3];  
                  new 연산자는 heap 영역에  클래스 객체를 생성하는 연산자이다.
                  자바에서는 배열도 클래스의 일종이다.  // new연산자를 이용해서 생성하니까.

 - 배열 선언 형식

    1. 자료형[] 배열명 = new 자료형[요소갯수];
    2. 자료형[] 배열명;
        배열명 = new 자료형[요소갯수];
    3. 자료형[] 배열명 = {초기값1,초기값2,초기값3};  // 변수 a 이름은 stack에 저장되고 초기값3개의 주소값을 가지고 있다.
    4. 자료형 배열명[]     ==    자료형 []배열명
      int a[] = {1,2,3}   ==  int a=1; int b=2; int c=3;
      String str = "hello"; //stack에 할당.
      String str = new String("hello") ;  // heap에 할당.

 - 2차원 배열
     a[][] ={10,20}, {30,40,50};
  ---------------------------
               행수            열수
  stack  /  heap ~~~~~~~
  a ->     a[0]    ->   a[0][0]
                              a[0][1]
             a[1 ]   ->   a[1][0]
                              a[1][1]
                              a[1][2]
 ----------------------------

 배열 자동 초기화 예제
  ------------------------------------------------------------------
  int n[] = new int [3];
  double d[] = new double [3];
  boolean b[] = new boolean [3];
  char ch[]= new char[3];
  String str[] = new String [3];

  n[0]=3;
  d[0]=12.5;
  b[0]=true;
  ch[0]='A';
  str[0]="kim";
  
  for(int i=0; i<3; i++)
       System.out.println(n[i]+ "   "+d[i]+ "  " +b[i]+"   "+ch[i]+"  "+str[i]);
 ------------------------------------------------------------------

 int a[]={1,3,5,7};
 int b[]= new int[a.length];
 b = a; //변수 a가 가리키는 heap영역의 주소를 b에 대입한다.
 //C언어에서는 오류, 자바에서는 가능.

- 문자열을 각 문자 배열의 요소로 분해
   문자열.getChars(    srcBegin,     srcEnd,           destArray,               destBegin)
                             시작위치            끝       복사받을 배열이름    복사받을 배열에서의 시작위치
   String str = "GoodMorning-Kil-Dong";
   char ch[] = new char[str.legth()];   // String은 배열과 달리 클래스이니까 .length() 로 길이를 구한다.
   str.getChars(4, 11, ch, 2)  //  문자열은 널 문자 포함이니까 11을 넣는다. 
-----------------------------
   G o o d m o r n i n  g   -
   0 1  2 3 4  5 6 7 8 9 10  11
-----------------------------

* 문자열 srcBegin 문자부터 srcEnd번째 까지를  destArray의 destBegin 번째 부터 복사한다.
--------------------------------------
  String str = "Goodmorning-Kim";
  char ch[] = new char[str.length()];
  str.getChars(4,11,ch,2);
  for(int i = 0; i<ch.length; i++)
   System.out.println(i+" "+ch[i] +" ");
--------------------------------------------------------------------------------

10~100까지 난수 발생
int a = (int)(Math.random()*91+10);   // 10은 가장 작은수, 91는 나올수 있는 가지수
--------------------------------------------------------------------------------
class Lotto
{
 public static void main(String args[])
 {
  int a[] = new int[6];
  for(int i=0 ; i<6 ; i++)
  {
   a[i]=(int)(Math.random()*45)+1;
  
   for(int j=0; j<i; j++)
   {
    //같은 값이 있는 비교할려고
    if(a[i] == a[j])
    {
     i--;
     break;
    }
   }
  }
  for(int i=0; i<6 ; i++)
  {
   System.out.print(a[i]+" ");
  }
 }//main
 }

- 배열 정렬
-----------------------------------------------------------------------------

class Array_Sort
{
 public static void main( String [] args )
 {
  String str[]={"나","사","아","마","바","다","가"};
  disp(str);
  
  Arrays.sort(str); // 배열 오름차순 정렬
  disp(str);
  Arrays.sort(str,Collections.reverseOrder()); //배열 내림차순 정렬
  disp(str);
 }// end main
 
 static void disp(String arr[])//사용자 정의 함수
 {
  for(int i=0;i<arr.length; i++)
  {
   System.out.println(arr[i]+" ");
  }
  System.out.println();
 }
}

Wrapper class  // 인자값을 변수가 아닌 객체로만 받는 메서드를 사용할때
byte - Byte
char - Character
short - Short
int - Integer
long - Lon
float - Float
double - Double
boolean - Boolean

------------------
import java.util.*;
class Array_Sort_2
{
 public static void main(String args[])
 {
  Integer a[]={90,88,77,66,55,33};
  disp(a);
  
  Arrays.sort(a);//오름차순.......... 인자값을 객체로 받는다.
  disp(a);
  
  Arrays.sort(a,Collections.reverseOrder());
  disp(a);
 }
 
 static void disp(Integer []b)
 {
  for(int i=0; i<b.length; i++)
   System.out.print(b[i]+ " ");
  
  System.out.println();
 }
}
----------------------------

객체지향 4대 특징

 - 캡슐화, 은닉화
 - 상속
 - 다형성(Overload, Overriding)
 - 추상화(복잡한 것을 단순화)
     클래스 설계자 입장
     클래스 사용자 입장

 - OverLoading

   1. 하나의 클래스 내에 이름이 같은 메서드가 여러개 있는 경우.
   2. 구분은 매개면수로 한다,  갯수가 다르거나 자료형이 달라야 한다.

   3. 확장(상속)이 가능하다.
   4. 리턴형은 상관없다.
    class AA
            {
                   void aa(){}
                   int aa(int a){}
                   String aa(String name){}
            } 

- Overrding

  1. 상속 받은 메서드 내용 재정의
  2. 리턴형, 인수, 인수자료형은 반드시 같아야 한다.

  3. 접근제한자는 상위와 같거나, 더 넓은 개념으로 사용 할 수 있다.  
     ( 상위의 protected 는 public 사용가능 하다. )
  4. 상속관계로서 사용가능.
  5. 확장(상속) 가능하다.
----------------------------------------------------------------------------------------------

class 클래스 이름
  {
      //변수선언
      private int age;            //은폐필드  (자신의 클래스 내에서만 사용 가능)
      생략  int age;             //같은 패키지 안에서만..
      protected String name;  //보호필드  ( 자신의 클래스와 , 상속 받은 클래스에서 사용가능)
      public String pum;        // 공개필드  ( 어디서나 사용가능)
 
     private 리턴형 메서드명 (){}
     생략 리턴형 메서드명 (){}  
     protected 리턴형 메서드명(){}
     public 리턴형 메서드명 (){}
  }
class  클래스이름2 extends 클래스 이름 implements 인터페이스명
{

}
----------------------------------------------------------------------------------------------

- 메서드란
  기본적으로 프로그램 명령문의 집합이며, 자바에서는 실행의 기본 단위가 된다.
  메서드는 클래스에 속한 일부분이다.
      System.  out.  println();
      클래스   객체  메서드

- 클래스란
  하나 이상의 변수, 또는 메서드를 포함하고 있는 변수와 메서드 집합체이다.
  하나의 단위 프로그램이다(모듈)

- 자바 프로그램이란
    하나 이상의 클래스로 구성된 클래스 집합체이다. 
    저장은 main()이 소속해 있는 클래스 이름으로 저장 한다.
    프로그램을 실행하는 동안 동일 클래스나, 다른 클래스에 있는 메서드를 호출 할 수 있다. 
    프로그램 코드는 항상 메서드내에 존재해야 하며, 매서드는 항상 클래스내에 존재해야 한다. 

- 메서드 기본 형식

  접근제한자   리턴형  메서드이름 (인수)
   {
       처리내용;
   }

  [접근제한자]  [static]  리턴형  메서드이름 (인수) throws ExceptionList
   {
       처리내용;
   }

 메서드는 실행중 런타임 오류가 발생 할 수 있다.
 자바에서는 이런 문제를 다룰 수 있는 예외처리 기능이 제공된다. 

- 메서드 예제

  void aa() {}
  boolean bb(int a) {}
  int cc(String str ){}
  void ee(char) {}

- 메서드 호출

  자신의 클래스에서는   ==>  메서드명(인수값)
  다른 클래스 메서드 호출 ==> 객체 생성하고 , 객체.매서드(인수값)
  static 메서드 호출은  ==>  클래스이름.메서드(인수값),         객체 생성하고,  객체. 메서드()


-------------------------------------------------------------------------
class Test_2
{
 public char aa()
 {
  System.out.println("aa() call...");
  return 'A';
 }
 public int bb(){
  System.out.println("bb() call...");
  return 100;
 }
 public double cc(){
  System.out.println("cc() call...");
  return 30.9;
 }
 //  end Test_2
}
class Method_3
{
 public static void main(String args[])
 {
  Test_2 B = new Test_2();// 객체 생성
  
  char ch = B.aa();
  System.out.println("ch : "+ ch);
  

  int in = B.bb();
  System.out.println("in : " + in);
  
 
  double dou = B.cc();
  System.out.println("dou : " + dou);
 }
}
-------------------------------------------------------------------------

- static 메서드 

  public static double abs(double a) ==> 절대값
  public static double ceil(double a) ==> 올림값
  public static double floor(double a) ==> 내림값
  public static double round(double a) ==>반올림
  public static double max(double a) ==> 둘 중 큰값 

예제  // static 메서드는 객체 생성 하지 않고도 호출 가능하다... main메서드 이전에 실행된다..
class Method_1
{
  public static void main(String args[])
  {
   System.out.println(Math.max(12.5, 77.7));  //큰값
   System.out.println(Math.min(12.5, 77.7));  //작은값
   System.out.println(Math.abs(-10));         //절대값
   
   System.out.println(Math.ceil(5.7));       //반올림
   System.out.println(Math.ceil(5.2));
   
   System.out.println(Math.floor(5.7));      //내림값
   
   System.out.println(Math.round(5.7));      //반올림
   System.out.println(Math.round(5.3));   
   
   System.out.println(Math.pow(2,10));       //승수표현 2^10
  }
}
 

+ Recent posts