261p. 에제 7-10
//추상클래스의 작성
class Ex_JS {
public static void main(String args[]) {
Unit[] group = {new Marine(), new Tank(), new Dropship() };
for (int i =0; i<group.length; i++)
group[i].move(100, 200);
}
}
abstract class Unit {
int x, y;
abstract void move(int x, int y);
void stop() {};
}
class Marine extends Unit {
void move(int x, int y) {
System.out.println("Marine[x=" + x + ",y"+ y + "]");
}
void stimPack() {}
}
class Tank extends Unit {
void move(int x, int y) {
System.out.println("Tank[x=" + x + ",y"+ y + "]");
}
void changeMode() {}
}
class Dropship extends Unit {
void move(int x, int y) {
System.out.println("Dropship[x=" + x + ",y"+ y + "]");
}
void load() {}
void unload() {}
}
출력값:
Marine[x=100,y200]
Tank[x=100,y200]
Dropship[x=100,y200]
269p. 에제 7-11
//디폴트 메서드와 static메서드, 이름이 중복되어 충돌하는 경우
class Ex_JS {
public static void main(String[] args) {
Child3 c = new Child3();
c.method1();//인터페이스를 구현한 클래스Child3에서 디폴트 메서드를 오버라이딩함
c.method2(); //조상 클래스의 메서드가 상속되고, 인터페이스의 디폴트 메서드는 무시된다.
MyInterface.staticMethod();
MyInterface2.staticMethod();
}
}
class Child3 extends Parent3 implements MyInterface, MyInterface2 {
public void method1() {
System.out.println("method1() in Child3"); //오버라이딩
}
}
class Parent3 {
public void method2() {
System.out.println("method2() in Parent3");
}
}
interface MyInterface {
default void method1() {
System.out.println("method1() in Myinterface");
}
default void method2() {
System.out.println("method2() in Myinterface");
}
static void staticMethod() {
System.out.println("staticMethod() in Myinterface");
}
}
interface MyInterface2{
default void method1() {
System.out.println("method1() in Myinterface2");
}
static void staticMethod() {
System.out.println("staticMethod() in MyInterface2");
}
}
결과
method1() in Child3
method2() in Parent3
staticMethod() in Myinterface
staticMethod() in MyInterface2
274p. 에제 7-12
//내부 클래스의 제어자와 접근성1
class Ex_JS {
class InstanceInner { //인스턴스 클래스
int iv = 100;
//static int cv = 100; //에러! static변수를 선언할 수 없다.
final static int CONST = 100; //상수이므로 허용
}
static class SaticInner {
int iv = 200;
static int cv = 200;
}
void myMethod() {
class LocalInner {
int iv = 300;
//static int cv = 300; //에러! static변수를 선언할 수 없다.
final static int CONST = 300;
}
}
public static void main(String args[]) {
System.out.println(InstanceInner.CONST);
System.out.println(SaticInner.cv);
//System.out.println(LocalInner.CONST); 지역내부클래스이므로 해당 메서드 내에서만 사용가능
}
}
출력값:
100
200
275p. 에제 7-13
//내부 클래스의 제어자와 접근성2
class Ex_JS {
class InstanceInner {} //인스턴스 내부 클래스
static class StaticInner {} //static 내부 클래스
// 인스턴스멤버 간에는 서로 직접 접근이 가능하다.
InstanceInner iv = new InstanceInner();
// static 멤버 간에는 서로 직접 접근이 가능하다.
static StaticInner cv = new StaticInner();
static void staticMethod() {
// static멤버는 인스턴스멤버에 직접 접근할 수 없다.
//InstanceInner obj1 = new InstanceInner();
StaticInner obj2 = new StaticInner();
// 굳이 접근하려면 아래와 같이 객체를 생성해야 한다.
// 인스턴스클래스는 외부 클래스를 먼저 생성해야만 생성할 수 있다.
Ex_JS outer =new Ex_JS();
InstanceInner obj1 = outer.new InstanceInner();
}
void instanceMethod() {
// 인스턴스메서드에서는 인스턴스멤버와 static멤버 모두 접근 가능하다.
InstanceInner obj1 = new InstanceInner();
StaticInner obj2 = new StaticInner();
//메서드 내에 지역적으로 선언된 내부 클래스는 외부에서 접근할 수 없다.
//LocalInner lv = new LocalInner();
}
void myMethod() {
class LocalInner {}
LocalInner lv = new LocalInner();
}
public static void main(String[] args) {
}
}
276p. 에제 7-14
//내부 클래스의 제어자와 접근성3
class Outer {
private int outerIv = 0;
static int outerCv = 0;
class InstanceInner {
int iiv = outerIv; //1. 내부 클래스에서는 외부 클래스의 private멤버도 접근가능하다.
int iiv2 = outerCv;
}
static class StaticInner {
//static클래스는 외부 클래스의 인스턴스멤버에 접근할 수 없다
//int siv = outerIv;
static int scv = outerCv;
}
void myMethod() {
int lv = 0;
final int LV = 0;
class LocalInner { //2. 지역 내부 클래스에서는 상수만 접근 가능하다.
int liv = outerIv;
int liv2 = outerCv;
//int liv3 = lv; 에러! (JDK1.8부터 에러 아님)
int liv4 = LV;
}
}
}
277p. 에제 7-15
//내부 클래스의 제어자와 접근성4
class Outer2 { //외부 클래스
class InstanceInner { //인스턴스 클래스
int iv = 100;
}
static class StaticInner { //스테틱 클래스
int iv = 200;
static int cv = 300;
}
void myMethod() {
class LocalInner { //로컬 클래스
int iv = 400;
}
}
}
class Ex_JS {
public static void main(String[] args) {
Outer2 oc = new Outer2(); //외부 클래스 인스턴스 생성
Outer2.InstanceInner ii = oc.new InstanceInner();
//이후 인스턴스클래스의 인스턴스 생성 가능
System.out.println("ii.iv : "+ ii.iv);
System.out.println("Outer2.StaticInner.cv : "+Outer2.StaticInner.cv);
//스태틱클래스의 인스턴스는 외부 클래스를 먼저 생성하지 않아도 된다.
//바로 스태틱클래스의 인스턴스 생성 후 호출 가능
Outer2.StaticInner si = new Outer2.StaticInner();
System.out.println("si.iv : "+ si.iv);
}
}
출력값:
ii.iv : 100
Outer2.StaticInner.cv : 300
si.iv : 200
278p. 에제 7-16
//내부 클래스의 제어자와 접근성5
class Outer3 {
int value = 10; //Outer3.this.value
class Inner {
int value = 20; //this.value
void method1() {
int value = 30;
System.out.println("value :"+ value); //가까운 value
System.out.println("this.value :"+ this.value); //내부 클래스의 value
System.out.println("Outer3.this.value :"+ Outer3.this.value); //외부클래스의 value
}
}
}
class Ex_JS {
public static void main(String args[]) {
//내부 인스턴스클래스의 메서드를 호출하기 위해
//외부 클래스의 인스턴스를 생성 후 내부 클래스의 인스턴스를 생성함
Outer3 outer = new Outer3();
Outer3.Inner inner = outer.new Inner();
inner.method1();
}
}
출력값:
value :30
this.value :20
Outer3.this.value :10
279p. 에제 7-17
//익명 클래스
class Ex_JS {
Object iv = new Object(){ void method(){} };
//일회용 클래스, 선언과 생성을 동시에 한다.
//조상클래스이름 인스턴스이름 = new 조상클래스이름() {}
static Object cv = new Object(){ void method(){} };
void myMethod() {
Object lv = new Object(){ void method(){} };
}
}
280p. 에제 7-18~19
//익명 클래스 예제
import java.awt.*;
import java.awt.event.*;
class Ex_JS {
public static void main(String[] args) {
Button b = new Button("Start");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("ActionEvent occurred!!!");
}
}
);
}
}