java

java 활용 예시(내부 클래스)

goshek 2024. 9. 13. 10:39

package chapter16;

 

/*

내부 클래스

: 다른 클래스 내부에 선언된 클래스를 의미

 

장점

: 코드의 가독성 향상 - 코드의 구조 파악에 용의

: 클래스 간의 관계를 명확하게 함

- 내부 클래스는 외부 클래스의 멤버에 쉽게 접근 가능

 

=종류=

1. 비정적 내부 클래스(가장 기본적인 클래스 형태)

: 외부 클래스의 인스턴스에 속함

>> 외부 클래스의 인스턴스 변수 및 메소드에 직접 접근이 가능하다.

 

2. 정적 내부 클래스

: "외부 클래스의 정적 멤버", 외부 클래스의 인스턴스 변수에 접근할 수 없다.

>> 외부 클래스의 정적 멤버에만 접근 가능

>> new 생성자 초기화가 가능

 

3. 메소드 내부 클래스(지역 클래스)

: 메소드 내에 정의된 내부 클래스, 해당 메소드 내에서만 사용 가능

>> 메소드의 지역 변수에만 접근 가능

cf) 지역 변수가 final인 경우에만 접근가능

 

4. 익명 (내부) 클래스

: 이름이 없는 내부 클래스

: 주로 인터페이스나 추상 클래스 구현에 사용

>> 즉시 객체 생성 가능

주로 클래스를 일회용으로 사용할 때 이용

*/

 

// 외부클래스

class OuterClass{

private String outerField= "외부 클래스의 필드";

static String staticField= "외부 클래스의 스태틱 필드";

 

// 비정적 내부 클래스- 인스턴스 필드, 인스턴스 메소드처럼 사용

class InnerClass{

void display() {

System.out.println("외부 클래스 필드에 접근: " + outerField);

}

}

 

// 정적 내부 클래스

static class StaticClass{

void display() {

// System.out.println("외부 클래스 필드에 접근(인스턴스): "+ outerField); -Error

System.out.println("외부 클래스 필드에 접근(정적): "+ staticField);

}

}

 

void outerMethod() {

final String localVar= "로컬 변수";

 

// 메소드 내부에서만 사용 가능

class MethodClass {

void display() {

System.out.println("로컬 변수에 접근"+ localVar);

}

}

 

MethodClass inner= new MethodClass();

inner.display();

}

}

 

// 추상 클래스: 하나 이상의 추상 메소드를 포함

// cf) 추상 메소드: 구현부가 없는 메소드

abstract class AbstractClass{

abstract void display();

}

 

public class A_Inner01 {

 

public static void main(String[] args) {

// OuterClass.InnerClass.display -Error

// === 비정적 내부 클래스 ===

// 1. 외부 클래스 인스턴스화

OuterClass outer1= new OuterClass();

 

// 2. 외부 클래스.내부 클래스 형식으로 내부 클래스를 생성

// 외부 클래스타입.내부클래스타입 변수= 외부인스턴스.new 내부클래스();

OuterClass.InnerClass inner= outer1.new InnerClass();

 

// === 정적 내부 클래스(스태틱 클래스) ===

// 1. 외부 클래스 인스턴스화가 필요없이 클래스를 통한 내부 클래스 생성이 가능

// 외부클래스 타입. 내부클래스타입 변수= new 외부클래스.내부클래스();

OuterClass.StaticClass staticInner= new OuterClass.StaticClass();

 

// === 메소드 내부 클래스(지역 클래스) ===

OuterClass outer2= new OuterClass();

outer2.outerMethod();

// === 익명 내부 클래스*** ===

AbstractClass obj= new AbstractClass() {

 

@Override

void display() {

System.out.println("익명 내부 클래스");

}

};

obj.display();

}

 

}

 

예시

package chapter16;

 

// 학교 클래스

class School{

private String name;

public School(String name) {

this.name= name;

}

public String getName() {

return name;

}

 

// 내부 클래스- 학생, 교사 클래스

class Student{

private String studentName;

private int studentId;

 

public Student(String studentName, int studentId) {

this.studentId= studentId;

this.studentName= studentName;

}

 

public void displayInfo() {

System.out.println("학교: "+ name);

System.out.println("이름: "+ studentName);

System.out.println("ID: "+ studentId);

}

}

 

class Professor{

private String professorName;

private String professorId;

 

public Professor(String professorName, String string) {

this.professorId= string;

this.professorName= professorName;

}

 

public void displayInfo() {

System.out.println("학교: "+ name);

System.out.println("이름: "+ professorName);

System.out.println("ID: "+ professorId);

}

}

}

 

public class A_Inner02 {

 

public static void main(String[] args) {

School school= new School("한국초등학교");

School.Student student1= school.new Student("gwon", 1001);

School.Student student2= school.new Student("hyuck", 1002);

 

School.Professor professor1= school.new Professor("Kim", "코딩");

 

student1.displayInfo();

student2.displayInfo();

 

professor1.displayInfo();

}

}