Lab 9: Questions and Additional Notes about the  four types of inner classes  

Due Date : May 16,  23 : 59 

Total  score: 30

Submit your assignment using the following file format:  LabNumber_Name_Student_ID.zip

>> Example: Lab7_Hongkildong_201620505.zip

This  zip file will contain two types of files, namely:

1) Report file with file format “Report_Lab number” (eg.report_8) to answer theory questions and to write the screen shot of your program.

2) Source code file that contains codes of classes to answer programming questions.


Java 1.0에서 GUI용 AWT API에는 버그가 있음
Java 1.1에서 버그는 inner class 개념을 이용하여 이벤트 핸들러 개념을 구현함으로써 고정된다.
한 유형의 개체가 존재하지 않는다면, 다른 유형의 개체가 존재할 가능성이 없다면, 우리는 inner class을 사용하였다.
university 개체가 없다면 학과 개체는 존재할 수 없다.


첫째, 우리는 Inner Class을 그들의 선언의 위치에 따라 나눈다.
둘째, 우리는 그들의 목적에 따라 그들을 더 나누었다.


Part 1: Non-local and non-static inner class 

Answer : 

a) two

b) University.class,  University$Department.class


Answer : 

a) static member는 static class 안에서만 만들 수 있기 때문이다.

b) non-static inner class can not have a static method including main()





from the ouput of the above code, a method(belongs to non-static inner class) can access fields of outer classes?

Answer : 

Yes. 

inner class로 인해 만들어진 instance의 method(belongs to non-static inner class)는 outer class에 access 할 수 있기 때문이다. 

 



외부 클래스에 M1, M2, M3 등 10가지 method이 있다고 가정해 보십시오.
M1이 M2를 100회 호출했다고 가정한다. 
이는 M3, M4, ...M10이 M2를 호출하지 않음을 의미한다.
따라서 우리는 M2 선언문을 외부에서 M1의 본체로 옮길 수 있다.

그러나, 자바는 다른 방법 내부의 한 방법의 선언을 허용하지 않는다.

 

해결책: 먼저, M1 내부의 내부 클래스 선언
둘째, 내부 계급 내부에서는 M2를 선언한다.
따라서 M3, M3, M3,..M10은 M2에 접근할 수 없다.
실질적으로 이름을 가진 local inner class는 매우 드물게 사용된다.
대신, anonymous local inner class은 자주 사용된다.

M2 method는 z 변수에 접근할 수 있는가?

 

Answer : 

그렇다. local inner class의 member는 그 method 안에서만 접근이 가능하므로 M2 method는 z 변수에 접근할 수 있다.

또한 local inner class는 outer class의 member에게도 접근이 가능하다. 


 

Answer : 

a) A a1 = new A() { public void M1() { ...("travel by ship");} } }; 이 코드로 인해 M1을 override 하는 Anonymous class가 사용되면서 Anonymous object가 만들어 지기 때문이다. 이 결과로 "travel by ship" 이 출력된다.

반면 A a2 = new A(); 로 만들어진 a2 객체는 class A의 method M1이 그대로 쓰이기 때문에 "travel by plane"이 출력된다.

b) Three classes are generated. : A.class , B.class , B$1.class




Answer : 

참고 : namocom.tistory.com/669

'📌 java > Object-oriented Programming' 카테고리의 다른 글

Homework_11  (0) 2020.05.30
Homework_10  (0) 2020.05.19
java - use case diagram, scenario  (0) 2020.05.09
ATM_Implementation  (0) 2020.05.09
ATM_Design  (0) 2020.05.09
복사했습니다!