Presentation is loading. Please wait.

Presentation is loading. Please wait.

컴퓨터공학실습(I) 3주 인공지능연구실.

Similar presentations


Presentation on theme: "컴퓨터공학실습(I) 3주 인공지능연구실."— Presentation transcript:

1 컴퓨터공학실습(I) 3주 인공지능연구실

2 Modifier public protected default private 어느 class에서나 참조 가능
같은 package 내의 class에서만 참조 가능, 자식 class가 다른 package에 있는 경우, 그 자식class도 참조가능. default 같은 package 내의 class에서만 참조 가능. private 같은 class 내에서만 참조 가능.

3 static method(1) 사용 예 public class Count {
    int var1 = 0; // instance 변수     static int var2 = 1; // static 변수선언     static int getCount() { // static method 선언          return var2;     }     public static void main(String [] args){           System.out.println(Count.getCount()); // 여기 주목

4 static method(2) - main method
왜 main()은 public인가? JDK의 Java compiler 혹은 JVM이 main()을 호출한다. 자신의 package밖에서 호출된다는 말이다. 따라서 main()은 누구나 호출할 수 있어야 한다. 요즘은 생략하거나 private으로 써도 된다. 왜 main()은 static 인가? main()이 호출되는 시점에 object(혹은 instance)가 없이도 호출할 수 있어야 한다. 왜 main()은 void 형인가? int라고 하면 compile은 되지만 실행은 안 된다. argument arg는 무엇인가? C 언어와 다른 점은 array의 arg[0]가 program의 이름이 아니라 첫 번째 command line argument의 값이라는 것이다.

5 final keyword class에 final 키워드 사용 method에 final 키워드 사용
class는 절대로 상속될 수 없다. 누군가가 상속받아 새로운 class를 만들지 못하게 하려는 보안 관련 의도이다. method에 final 키워드 사용 final이 적용된 method는 절대로 overriding이 불가능하다. 보안과 프로그램의 속도 향상(overriding method 검사를 안하므로) variable(변수)에 final 키워드 사용 상수를 만들 때 사용한다. C의 #define과 유사하다.

6 abstract keyword interface도 일종의 abstract class
abstract class TestAbstract { //abstract class는 instance가 될 수 없다        public abstract void func1() ;  } class Child extends TestAbstract { //상속받아서 instance로 만들어야 한다        public void func1() { // abstract method overriding을 해야 한다                System.out.println("여러분 fighting!! ^0^");             } } public class MainClass {         public static void main(String [] args) {            Child ta = new Child(); // TestAbstract ta = new Child();라고 해도 된다             ta.func1();        } interface도 일종의 abstract class

7 interface keyword interface Test1 { int YYY = 8;  public void func1();  } // YYY는 상수다 interface Test2 { public void func2(); } // func2()는 body가 없다 class Child implements Test1, Test2 {// 다중상속이 가능하다              public void func1() { System.out.println("Class Test1");  }              public void func2() {  System.out.println("Class Test2"); } // body를 가졌다 } public class TestInter1 {                     public static void main(String [] args) {                     Child c = new Child();                     c.func1();                     c.func2();

8 deprecation 없어지거나 변경된 API deprecation API의 예

9 ==와 equals() ==는 주로 primitive data type간의 비교 equals()는 object간의 내용 비교
잘못된 예 public class TestEquality { public static void main(String [] args) {         String a = new String("test");         String b = new String("test");         if (a == b)   System.out.println("same"); //변수가 같은 지를 비교하는 것         else              System.out.println("different"); }

10 Inner class 특징 inner class outter class의 모든 member를 자유로이 호출할 수 있다.
outter class의 이름과 inner class의 이름은 정확히 달라야 한다. inner class는 method 안에서도 정의 가능하다. 그러나 이 때 이 inner class가 access할 수 있는 변수는 method안의 final 변수만 가능하다. abstract(추상) inner class의 정의도 가능하다. Event handling에서 주로 쓰인다.(나중에 설명) outterclass명$innerclass명.class

11 Wrapper class (1) 모든 primitive data type에 대하여 해당 wrapper class가 존재한다.
int x = 7; Object [] arr = new Object[7]; Integer wInt = new Integer(x); // wrapper class arr[0] = wInt; int y = wInt.intValue(); // Object형 arr에 int가 들어갈 수 있다

12 Wrapper class (2) Wrapper class list Primitive data Type Wrapper Class
boolean Boolean byte Byte char Character short Short int Integer long Long float Float double Double

13 기타 Collection toString()
여러 object를 element로 가지고 있는 object를 일컫는 말이다. 예를 들어 array는 collection중의 하나이다. toString() toString() method도 Object class가 가지고 있다. (equals()처럼)따라서 모든 class가 소유하고 있다. 이는 자신의 instance를 String instance로 바꾸는 데 사용된다. 사실 화면에 출력하려면 문자(String)형태로 변환되어야 한다. int d = 7; System.out.println(d); // System.out.println(d.toString());

14 Exception(1) Exception이란? 예 error이긴 error인데 프로그램이 멈출 필요까지는 없는 error
Exception을 대문자로 시작한 이유는 Exception이 class이기 때문이다. 이 class는 모든 exception의 부모 class이다. try {        } catch (ArrayIndexOutOfBoundsException e) {// ArrayIndexOutOfBoundsException가 발생하면 작동                  System.out.println(“바운드를 넘겼습니다!!");        } finally // 항상 작동 {                         System.out.println(“나는 맨날 나온다!!");        }

15 Exception(2) 자주 발생하는 Exception Exception 종류 예 ArithmeticException
int = 12/0; NullPointerException Data d =null; System.out.println(d.toString()); NegativeArraySizeException array[-1] ArrayIndexOutOfBoundsException String [] tmp = {“A”, “B”, “C”}; While(i<4){ System.out.println(tmp[i]); i++; }

16 Exception(3) – 사용자정의 exception(1)
public class ServerTimedOutException extends Exception { private int port; public ServerTimedOutException(String reason, int port) {          super(reason);          this.port = port; } public int getPort() {          return port;

17 Exception(4) – 사용자정의 exception(2)
public class TestExceptionThrows { static String Tmp; static public void connectMe(String SeverName) throws ServerTimedOutException { int success=-1; if(success==-1){ throw new ServerTimedOutException("Could not connect",80); } public static void main(String args[]){ Tmp="S"; try{ connectMe(Tmp); }catch (ServerTimedOutException e){ System.out.println(e.getMessage()); System.out.println(e.getPort());


Download ppt "컴퓨터공학실습(I) 3주 인공지능연구실."

Similar presentations


Ads by Google