[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

- ์ •์˜ : ํ”„๋กœ๊ทธ๋žจ์˜ ์†Œ์Šค์ฝ”๋”ฉ ์•ˆ์— ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์„ ์œ„ํ•œ ์ •๋ณด๋ฅผ ๋ฏธ๋ฆฌ ์•ฝ์†๋œ ํ˜•์‹์œผ๋กœ ํฌํ•จ์‹œํ‚จ ๊ฒƒ 

- ์žฅ์  : ์–ด๋…ธํ…Œ์ด์…˜์€ ๋งˆ์น˜ ์ฃผ์„์ฒ˜๋Ÿผ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์œผ๋ฉด์„œ๋„ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์—๊ฒŒ ์œ ์šฉํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณต

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ