h-i-s-t-o-r-y

[Java] 오버라이딩(Overriding) / 패키지(package)와 import 본문

Java

[Java] 오버라이딩(Overriding) / 패키지(package)와 import

H' 2021. 7. 17. 23:10

>p.327 오버라이딩 (override)

p.328 오버라이딩의 조건
1. 함수명 X
2. 매개변수 X
3. 리턴자료형 X
4. 접근지정자는 바꿀 수 있다.
(범위가 같거나 넓은 쪽으로만 바꿀 수 있다.)
protected -> protected/public
패키지 내외부+상속  < 어디서든
5. throws A,B,C => 많은 수의 예외를 선언할 수 없다.
throws A,B,C[,D,E]

> p.330 super 키워드
ㄱ. 객체의 부모의 주소값을 갖는 참조변수
super.필드
super.메서드
ㄴ 부모의 멤버 (필드,메서드) 접근...
ㄴ. super 3가지 용도

 

> this 키워드
ㄱ. 클래스 자기자신의 주소값을  갖는 참조변수
ㄴ. this의 3가지 용도


> p.336 패키지(package)와 import

패키지(package)
1. 클래스 묶음
java.util 자주 사용되는 클래스 묶음
java.io Input/Output 자바에서 입출력 클래스

 

[import문]은 컴파일러에게
[소스파일에 사용된 클래스의 패키지]에 대한 정보를 제공하는 문
import java.lang.System


package days18;

public class Ex03 {

	public static void main(String[] args) {
		// p.318 상속
		/*
		Circle c1 = new Circle();
		System.out.println(c1.color);	//부모클래스 shape의 color필드
		System.out.println(c1.r);
		c1.draw();			//[color=black]
		//[center=(0,0), r=100, color=black]
		*/
		
		Point [] p = {
				new Point(100,100),
				new Point(140,50),
				new Point(200,100)
		};
		
		Triangle t = new Triangle(p);
		t.draw();
		//[p1=(100,100), p2=(140,50), p3=(200,100), color=black]
		
	}//main

}//class

//좌표점
class Point{
	//필드 - 인스턴스 변수
	int x, y;
	//디폴트 생성자
	Point(){
		this(0,0);	//this 2번째 용도 (다른 생성자 호출)
	}
	//2생성자
	Point(int x, int y){
		this.x = x;	//this 1번째 용도 (클래스 자신의 멤버 지칭)
		this.y = y;
	}
	//메서드 - 인스턴스 메서드
	String getXY() {
		// String 클래스 String.format() 메서드
		return String.format("(%d,%d)", this.x, this.y);
	}
}

//도형
class Shape{
	String color = "black";	//명시적 초기화
	//메서드
	void draw() {
		System.out.printf("[color=%s]\n",color);
	}
}

//원 extends 도형+원점
class Circle extends Shape{	//is-a 관계(상속)
	//필드
	Point center;	//원점	//has-a 관계
	int r;	//반지름
	//String color;
	
	//메서드 물려받음
	//void draw(){}		+ color + center, r 출력
						//1. 새로 draw()함수 생성
						//2. *** 부모 draw()수정해서 사용 - 오버라이딩 ***
						//		  부모메서드를 재정의		     재정의함수
						// [오버로딩과 오버라이딩의 차이점 설명하세요]
	
	//디폴트 생성자
	Circle(){
		this(new Point(0,0),100);
	}
	//매개변수 2 생성자
	Circle(Point center, int r){
		this.center = center;
		this.r = r;
	}
	
	
	// @ 어노테이션 ?
	@Override
	void draw() {
		System.out.printf("[center=(%d,%d), r=%d, color=%s]\n"
				, center.x, center.y, r, color);
	}
}

//삼각형 extends 도형+점(3)
class Triangle extends Shape{
	//color
	//draw()
	//필드
	Point [] p = new Point[3];	//클래스 (객체) 배열
	
	//생성자
	public Triangle(Point [] p) {
		this.p = p;
	}

	@Override
	void draw() {
		System.out.printf("[p1=%s, p2=%s, p3=%s, color=%s]\n"
				, p[0].getXY(), p[1].getXY(), p[2].getXY(), color);
	}
	
	
}
package days18;

// import문
// 묵시적으로 모든 자바파일 추가
// java.lang 패키지 안의 모든 클래스에 import하겠다는 의미
import java.lang.*;
//import java.*; 는 못써용..

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class Ex07 {

	public static void main(String[] args) {
		// p.340 import문
		// 다른 패키지의 클래스를 사용할 때는
		// 패키지명.클래스명
		
		java.lang.System.out.println("코딩");
		System.out.println("코딩");
		
		Calendar c;
		// Date 클래스 : 날짜+시간 클래스
		Date d = new Date();
		ArrayList a;
		
		//Wed Mar 31 14:12:49 KST 2021
		System.out.println(d);
		//2021. 3. 31 오후 2:13:32
		System.out.println(d.toLocaleString());
		
		//내가 원하는 형식의 날짜객체를 출력
		String pattern = "yyyy/MM/dd a hh:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		System.out.println(sdf.format(d));	//2021/03/31 오후 02:19:33
		
		
		
	}//main

}//class
Comments