JAVA 자바 inner ,Outer, anonymous Inner,String ,Exception 개념정리와 적용예제

Devel/JAVA|2020. 8. 16. 17:51
반응형
-중첩클래스:
클래스 안에 또 다른 클래스가 정의될 수 있다. 이런 형태의 클래스를 ‘중첩 클래스’

 
-inner 클래스
Outer 클래스의 멤버처럼 클래스가 정의된 경우이다. 즉, 객체를 생성해야만 사용할 수 있는 멤버들과 같은 형태이기 때문에 반 드시 Outer 클래스를 객체생성 후에 Inner 클래스를 사용할 수 있다.

 
+

class Outer2{
      int a = 10;
      private int b = 20;
      static int c =30;
      public void info() {
            int size = 100;
            class Inner{
                  int d = 40;
                  public void print() {
                        System.out.println(a);
                        System.out.println(b);
                        System.out.println(c);
                        System.out.println(d);
                        System.out.println(size);
                        
                  }
            }//end Inner
            Inner inner = new Inner();
            inner.print();
            
      }
}
public class Ex07_5 {
      public static void main(String[] args) {
            Outer2 outer = new Outer2();
            outer.info();
      }
}
 
 
 
- anonymous Inner 클래스
이름이 없는 Inner 클래스를 의미한다. 일반적으로 인터페이스 또는 추상클래스를 구현하는 클래스를 이용할 때 주 로 사용된다. ( GUI 이벤트 처리시 많이 사용 됨)

 
+Comparator
java.util.Comparator 인터페이스를 이용하면 배열에 저장된 클래스의 특정 변수 값으로 정렬할 수 있다.

import java.util.Arrays;
import java.util.Comparator;
class Person{
       String name;
       int age;
       public Person(String name, int age) {
              this.name = name;
              this.age = age;
       }
       @Override
       public String toString() {
              return  name + "" + age ;
       }      
}
class PersonComp implements Comparator<Person>{
       @Override
       public int compare(Person o1, Person o2) {
//            int result =1;
//            if(o1.age>=o2.age) result = -1;   //내림차순    
//            return result;
              int result = -1;
              if(o1.age>=o2.age) result = 1;    //오름차순    
              return result;
       }
}
public class Ex07_8 {
       public static void main(String[] args) {
              Person p = new Person("홍길동",20);
              Person p2 = new Person("이순신",44);
              Person p3= new Person("유관순",18);
              Person p4 = new Person("강감찬",66);
              Person [] ps = {p,p2,p3,p4};
              //age로 정령
              Arrays.sort(ps, new PersonComp());
for(Person person : ps) {
       System.out.println(person);
}
       }
}
 
 
 
-문자열 생성 방법
-String 클래스
 
정확한 내용비교를 하고 싶으면 equls를 사용

public class StringTest {
      public static void main(String[] args) {
            String a ="hello";
            String b ="hello";
            System.out.println("a==b:\t"+(a==b));
            System.out.println("a.equals(b)=====:"+a.equals(b));
            
            String c = new String("hello");
            String d = new String("hello");
            
            System.out.println("c==d:\t"+(c==d));
            System.out.println("c.equals(d)=====:"+c.equals(d));
            
      }
}
-Wrapper클래스의 주요 메서드

 
 
 
+Eum

public class EnumTest3 {
      //public enum 타입 {상수값,상수값2,상수값3}
      public enum Color {BLUE,RED,YELLOW}
      
      public static void main(String[] args) {
             // 타입.상수값
            Color c = Color.BLUE;
            Color c2 = Color.RED;
            Color c3 = Color.YELLOW;
            //Color c4 = Color.GREEN;
            System.out.println(c); //BLUE
            
            Color[] xx = Color.values();
            for (Color p : xx) {
                  System.out.println(p);
                  System.out.println(p.name()+"\t"+p.ordinal());
            }
            
      }//end main0
}//end class
 
 
 
-Exception
{} 끝날때마다 어떤게 끝난것인지 표시해주기
들여쓰기 해주기
 

 
 
 
 
 
 

 

댓글()
loading