[Day17] Java 17 [8/31]
1. has-a ๊ด๊ณ (์์ ๊ด๊ณ)
์์กด์ฑ ์ฃผ์ (DI, Dependency Injection) : https://dev-coco.tistory.com/70
ex) ๋ชจ๋ ์๋์ฐจ engine ๊ต์ฒดํด์ผํ ๋ ํ๋์ฉ ๊ต์ฒดํ ์ ์๊ธฐ ๋๋ฌธ์ ์์กด์ฑ ์ฃผ์ ํ๋ ๊ฒฝ์ฐ
Car ํด๋์ค
// ์๋์ฐจ ํด๋์ค ์ ์ธ๋ถ
public class Car {
// ํ๋
String name;
String gearType;
int door;
// ํด๋์ค ๊ฐ์ฒด;
private Engine engine = null;
//private Engine engine = new Engine(); // ๋ช
์์ ์ด๊ธฐํ
// ์ธ์คํด์ค ์ด๊ธฐํ ๋ธ๋ญ
/*
{
this.engine = new Engine();
}
*/
// getter , setter
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
// ์์ฑ์
Car(){
// this.engine = new Engine();
}
public Car(String name, String gearType) {
// this.engine = new Engine();
this();
this.name = name;
this.gearType = gearType;
}
public Car(String name, String gearType, Engine engine) {
this(name, gearType);
this.engine = engine;
}
// ๋ฉ์๋
void speedUp(int fuel) {
// ์ฐธ์กฐํ - ์ธ์คํด์ค X, ๋ฐฐ์ด X
// engine ๋์ด๋ค.
this.engine.moreFuel(fuel); // ์ด ์ฝ๋ฉ์์ java.lang.NullPointerException ์์ธ ๋ฐ์ํ๋ค.
}
void speedDown(int fuel) {
this.engine.lessFuel(fuel);
}
void stop() {
this.engine.stop();
}
} // class
Engine ํด๋์ค
// ์์ง ํด๋์ค ์ ์ธ๋ถ๋ถ
public class Engine {
// ํ๋
double speed;
// ์์ฑ์
public Engine() {}
// ๋ฉ์๋
void moreFuel( int fuel ) {
this.speed += fuel * 0.05;
}
void lessFuel( int fuel ) {
this.speed -= fuel * 0.05;
}
void stop() {
this.speed = 0;
}
} // class
1) setter ์ฃผ์
Car car1 = new Car("K9", "Auto");
car1.setEngine(engine);
2) constructor ์ฃผ์ (๊ถ์ฅ)
Engine engine = new Engine();
Car car1 = new Car("K9", "Auto", engine);
* ํ์ฉ
//์คํผ๋ ์ค์
car1.speedUp(10);
// ์คํผ๋ ์ถ๋ ฅ
System.out.printf( "> ์ฐจ ์คํผ๋ = %f \n" , car1.getEngine().speed );
2. is-a ๊ด๊ณ (์์๊ด๊ณ)
์์
1) ์์์ด๋? : ๊ธฐ์กด ํด๋์ค๋ฅผ ์ฌ์ฌ์ฉํด์ ์๋ก์ด ํด๋์ค๋ฅผ ์์ฑํ๋ ๊ฒ
2) ์ฅ์ : ์ฝ๋ ์ฌ์ฌ์ฉ, ์ค๋ณต ์ ๊ฑฐ -> ์์ฐ์ฑ ํฅ์ / ์ ์ง๋ณด์ ํ์ฅ์ฑ ์ฉ์ด
3) ์ ์ธ ํ์
[์ ์ง] [๊ธฐ์ ] class ํด๋์ค๋ช
extends superํด๋์ค{
}
4) ๋ช ์นญ
๊ธฐ์กด ํด๋์ค == ๋ถ๋ชจ(parent) ํด๋์ค == ์์(super) ํด๋์ค == ๊ธฐ์ด(base) ํด๋์ค
์๋ก์ด ํด๋์ค == ์์(child) ํด๋์ค == ํ์(sub) ํด๋์ค == ํ์(derived) ํด๋์ค
5) ์ด๋ค ๊ฑธ ์์?
super ํด๋์ค์ ๊ตฌ์ฑ์์(ํ๋,๋ฉ์๋ == ๋ฉค๋ฒ)๋ฅผ ์์ํ๋ค.
์์ฑ์ ์์ X (์ด๊ธฐํ ๋ธ๋ก๋ ์์ X)
6) this VS super
this : ์ธ์คํด์ค(ํด๋์ค ์๊ธฐ ์์ )์ ์ฃผ์๊ฐ์ ๊ฐ๋ ์ฐธ์กฐ๋ณ์
super : ๋ถ๋ชจ ์ธ์คํด์ค์ ์ฃผ์๊ฐ์ ๊ฐ๋ ์ฐธ์กฐ๋ณ์
7) ๋ค์ค ์์
๋ค์ค์์ ๋ถ๊ฐ!!
but [์์ -> ์์] ์ ๊ฐ๋ฅ
public class Ex06 {
public static void main(String[] args) {
GrandChild gc = new GrandChild();
gc.p = 1; // Parent
gc.c = 2; // Child
gc.g = 3; // GrandChild
} // main
} // class
//class Parent extends Object{
class Parent{
int p;
}
class Child extends Parent{
int c;
}
class GrandChild extends Child{
int g;
}
8) Object ํด๋์ค
๋ชจ๋ ํด๋์ค์ ์ต์์ ๋ถ๋ชจํด๋์ค๋ java.lang.Object ํด๋์ค์ด๋ค.
9) ์์ ํธ์ถ ์์
employee ๋ถ๋ชจ ๊ฐ์ฒด ๋จผ์ ์ ์ธ (๋ถ๋ชจ ์ธ์คํด์ค ๋จผ์ )-> ์์ ์ธ์คํด์ค ํธ์ถ
์์
public class Ex07 {
public static void main(String[] args) {
// ๊ฐ์ฒด(ํด๋์ค) ๋ฐฐ์ด ์ด๊ธฐํ
Point [] p = {
new Point(1,1),
new Point(10, 100),
new Point(120, 15)
};
Triangle t = new Triangle(p);
// Shape ์์๋ ํ๋, ๋ฉ์๋
t.color = "red";
t.draw();
} // main
} // class
// The type Point is already defined Ex05.java ๋ชจ๋ ์ฃผ์ ์ฒ๋ฆฌ.
class Point{
int x, y;
Point(){}
Point(int x, int y){
this.x = x;
this.y = y;
}
String getPoint() {
return String.format("(%d,%d)", this.x, this.y);
}
}
// ๋ํ ํด๋์ค - ์ผ๊ฐํ, ์, ์ฌ๊ฐํ, ๋ง๋ฆ๋ชจ ๋ฑ๋ฑ
class Shape{
// ํ๋
String color = "black";
// ๋ฉ์๋
void draw() {
System.out.printf("[Color=%s]\n", this.color);
}
}
// class Circle extends Shape extends Point{
// class Circle extends Shape, Point{
// ์๋ฐ๋ ๋ค์ค ์์์ ์ง์ํ์ง ์์ต๋๋ค.
class Circle extends Shape{
// ์์
Point center; // has-a ๊ด๊ณ
int r; // radius ๋ฐ์ง๋ฆ
Circle(){
this( new Point(0,0) , 100) ;
}
Circle(Point center, int r) {
this.center = center;
this.r = r;
}
}
class Triangle extends Shape{
// ์ 3
Point [] p = new Point[3];
Triangle( Point [] p ) {
this.p = p;
}
}
class Rectangle extends Shape{
// ์ 4
Point [] p= new Point[3];
Rectangle( Point [] p) {
this.p = p;
}
}
3. ์ค๋ฒ๋ผ์ด๋ฉ ์กฐ๊ฑด
๋งํฌ : https://m.blog.naver.com/PostView.naverisHttpsRedirect=true&blogId=heartflow89&logNo=220961515893
1) ๋ฉ์๋๋ช ๋์ผ
2) ๋งค๊ฐ๋ณ์ ๋์ผ
3) ๋ฆฌํด์๋ฃํ ๋์ผ
4) ์ ๊ทผ์ง์ ์๋ ๋ฒ์๊ฐ ๊ฐ๊ฑฐ๋ ๋๊ฒ ์ค์ ํ ์ ์๋ค.
์) protected ~ -> public
private < [default] < protected < public
5) ์ธ์คํด์ค ๋ฉ์๋ <-> static ๋ฉ์๋ X
6) ์์ธ๋ ๋ ๋ง์ด ์ ์ธํ ์ ์๋ค.
4. ์ด๋ ธํ ์ด์
์ด๋ ธํ ์ด์ annotation
ex) @Override
- ์ ์ : ํ๋ก๊ทธ๋จ์ ์์ค์ฝ๋ฉ ์์ ๋ค๋ฅธ ํ๋ก๊ทธ๋จ์ ์ํ ์ ๋ณด๋ฅผ ๋ฏธ๋ฆฌ ์ฝ์๋ ํ์์ผ๋ก ํฌํจ์ํจ ๊ฒ
- ์ฅ์ : ์ด๋ ธํ ์ด์ ์ ๋ง์น ์ฃผ์์ฒ๋ผ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ์ํฅ์ ๋ฏธ์น์ง ์์ผ๋ฉด์๋ ๋ค๋ฅธ ํ๋ก๊ทธ๋จ์๊ฒ ์ ์ฉํ ์ ๋ณด๋ฅผ ์ ๊ณต
์ต๊ทผ๋๊ธ