14 [인강] 자바의정석 ch07
Machine translation — switch to KO for the original.
| 참조변수 super, 생성자 super( )
객체 자신을 가리키는 참조변수, 인스턴스 메서드 내에서만 존재 //this와 유사
조상의 멤버를 자신의 멤버와 구별할 때 사용
class Ex7-2 {
public static void main(String args[ ]) {
Child c = new Child ( );
c.method( );
}
}
class Parent { int x = 10; } //super.x, 조상메서드
class Child extends Parent { //this.x
int x = 20;
void method( ) {
System.out.println("x="+x); //가까운 쪽, this.x
System.out.println("this.x="+this.x);
System.out.println("super.x="+super.x);
}
}
//자식메서드에 별도 변수 없는 경우, 조상메서드의 변수가 super.x이자 this.x
//동일하게 쓰일 수 있음
super( ) 조상의 생성자 호출시 //this( )
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
class Point3D extends Point {
int z;
Point3D (int x, int y, int z) {
super(x, y); //조상클래스의 생성자 호출시
this.z = z; //자신의 멤버 초기화
생성자의 첫 줄에 반드시 생성자를 호출해야 한다.
컴파일러가 자동으로 super( );입력
class Point extends Object {
int x;
int y;
Point ( ) {
this (0, 0);
}
Point(int x, int y) {
super( );
this.x = x;
this.y = y;
}
class Point {
int x;
int y;
Point (int x, int y) {
super( ); //컴파일러가 자동추가, 모든 생성자는 첫줄에 다른 생성자를 호출해야, 기본생성자 필수
this.x = x;
this.y = y;
}
String getLocation( ) {
return "x :" + x + "y :" + y;
}
}
class Point3D extends Point {
int z;
Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
String getLocation ( ) {
return "x :" + x + ", y :" + y + ", z :" + z;
}
| 패키지, 클래스 패스
서로 관련된 클래스의 묶음
클래스는 파일(*class), 패키지는 폴더, rt.jar는 클래스들을 압축한 파일 → 모듈개념으로 변경
패키지 선언 //없는 경우 이름없는 패키지(default package)에 속함
package com.codechobo.book;
클래스 패스 등록 → 환경변수
| import문, static import문
클래스 사용할 때 패키지 이름을 생략할 수 있다
import java.util.*; //기본패키지로 import없이 사용가능, * 대신 실제사용 클래스 Date 적어도 됨
class ImprotTest {
Date today = new Date ( );
}
//java.*는 안됨, Date클래스가 java.sql.Date || java.util.Date인지 알 수 없음
import static java.lang.Integer.*;
import static java.lang.Math.random;
import static java.lang.System.out;
out.println(random( )); //클래스 이름의 생략이 가능하게 해줌
System.out.println(Math.random( ));
| 제어자, static, final, abstract
제어자: 형용사, 클래스나 클래스 멤버 앞에 붙는 것
접근 제어자: public, protected, (default), private //1개만 가능, 왼쪽 끝 배치
그 외: static, final, abstract, native, transient, synchronized, volatile, strictfp
public static final int WIDTH = 200;
static 멤버변수, 메서드, 클래스 초기화 블럭
final 클래스, 메서드, 멤버변수, 지역변수
final class FinalTest { //final class → String(보안), Math(static)
final int Max_SIZE = 10; //값을 변경할 수 없는 멤버변수(상수)
final void getMaxSize( ) { //오버라이딩 할 수 없는 메서드(변경불가)
final int LV = Max_SIZE; //값을 변경할 수 없는 지역변수(상수)
return Max_SIZE;
}
}
abstract 클래스, 메서드
abstract class AbstractTest { //추상매서드를 가지고 있는 추상클래스
abstract void move ( ); //추상메서드(구현부가 없는 메서드, 미완성)
}
//인스턴트 생성불가, 상속받아 완전한 클래스 만든 경우에만 객체 생성가능
AbstractTest a = new AbstractTest( );
Comments
No comments yet. Be the first!
319 posts in 테크
- 368Supabase 프로젝트 복사하기 (Restore to a New Project)NEW
- 341Migrating from Permanent Access Tokens to Token Exchange — Why Order Matters
- 326Startup & Product Glossary: Terms Every Solo Founder Should Know
- 325Context Management — How I Do It Now
- 324Claude Code Routines vs Cowork Schedule — What's the Difference?