1. 중첩 멤버 클래스에 대한 설명으로 틀린 것은 무엇입니까?
4번. 정적 멤버 클래스는 바깥 클래스의 객체가 없어도 사용할 수 있기 때문에, 객체가 반드시 있어야 사용할 수 있는 바깥 클래스의 필드를 정적 멤버 클래스 내부에서 사용할 수 없다.
2. 로컬 클래스에 대한 설명으로 틀린 것은 무엇입니까?
3번. 로컬 클래스는 static 키워드를 붙일 수 없다.
3. 익명 객체에 대한 설명으로 틀린 것은 무엇입니까?
3번. 익명 객체는 생성자를 선언할 수 없다.
4. 다음과 같이 Car 클래스 내부에 Tire와 Engine 클래스가 멤버로 선언되어 있습니다. CarExample 클래스에서 Tire와 Engine 객체를 생성하는 코드를 작성해보세요.
public class Car {
class Tire{}
static class Engine{}
}
public class CarExample {
public static void main(String[] args){
Car myCar = new Car();
Car.Tire tire = myCar.new Tire();
Car.Engine engine = new Car.Engine();
}
}
5. Action 인터페이스는 다음과 같이 work() 추상 메소드를 가지고 있습니다. ActionExample 클래스의 main() 메소드에서 Action의 익명 구현 객체를 만들어 실행 결과와 동일하게 나오도록 박스 안에 들어갈 코드를 작성해보세요.
public interface Action {
public void work();
}
public class ActionExample {
public static void main(String[] args){
Action action = new Action(){
@Override
public void work() {
System.out.println("복사를 합니다.");
}
};
action.work();
}
}
//출력
//복사를 합니다.
6. AnonymouseExample 클래스의 실행 결과를 보고, Vehicle 인터페이스의 익명 구현 객체를 필드와 로컨 변수의 초기값 그리고 메소드의 매개값으로 대입해보세요.
public interface Vehicle {
public void run();
}
public class Anonymouse {
Vehicle field = new Vehicle() {
@Override
public void run() {
System.out.println("자전거가 달립니다.");
}
};
void method1(){
Vehicle localVar = new Vehicle() {
@Override
public void run() {
System.out.println("승용차가 달립니다.");
}
};
localVar.run();
}
void method2(Vehicle v){
v.run();
}
}
public class AnonymousExample {
public static void main(String[] args){
Anonymouse anony = new Anonymouse();
anony.field.run();
anony.method1();
anony.method2(new Vehicle() {
@Override
public void run() {
System.out.println("트럭이 달립니다.");
}
});
}
}
7. 다음 Chatting 클래스는 컴파일 에러가 발생합니다. 원인을 설명해보세요.
final 변수는 값이 변경이 되지 않는다. 따라서 nickName = chatId;에서 컴파일 에러가 난다.
→ String nickName = chatId;로 변경해주면 에러 해결.
'언어 > JAVA' 카테고리의 다른 글
[chapter 13] 확인 문제 (0) | 2023.03.02 |
---|---|
[멀티 스레드2] (0) | 2023.02.27 |
[중첩 인터페이스] 익명 객체(익명 자식 객체, 익명 구현 객체) (0) | 2023.02.05 |
[중첩 클래스] (0) | 2023.02.05 |
[chapter5] 확인 문제 (2) | 2023.01.30 |