์ ๋ฒ์๊ฐ๊น์ง , Jshll์ ์ด์ฉํ์์ง๋ง
์ค๋๋ถํฐ ์ดํด๋ฆฝ์ค๋ฅผ ์ฌ์ฉํ์ฌ ๋ณธ๊ฒฉ์ ์ผ๋ก ์๋ฐ์ ๊ฐ์ฒด์งํฅ ๊ฐ๋ ๊ณต๋ถ๋ฅผ ์์ํ์๋ค.
Class
//Main(Runner)ํจ์
package com.firstjavaproject;
public class MulitplicationRunner {
public static void main(String[] args) {
// TODO Auto-generated method stub
Multiplication table=new Multiplication();
// table.print();
table.print(6);
table.print(6,11,20);
}
}
package com.firstjavaproject;
public class Multiplication {
void print() {
for(int i=1;i<=10;i++) {
System.out.printf("%d * %d=%d",5,i,5*i).println();
}
}
void print(int table) {
for(int i=1;i<=10;i++) {
System.out.printf("%d * %d=%d",table,i,table*i).println();
}
}
void print(int table, int from, int to) {
for(int i=from;i<=to;i++) {
System.out.printf("%d * %d=%d",table,i,table*i).println();
}
}
}
๊ตฌ๊ตฌ๋จ์ ์ถ๋ ฅํ๋ ์ฝ๋์ด๋ค.
ํด๋์ค๋ฅผ ๋ถ๋ฆฌ(Multiplication,MulitplicationRunner)ํ์ฌ ์ฝ๋๋ฅผ ์คํํ๋ฉฐ, ์ค๋ฒ๋ก๋ฉ์ผ๋ก ๊ฐ ๋งค๊ฐ๋ณ์์ ๋ฐ๋ผ ๋ค๋ฅธ ๋ฉ์๋๋ฅผ ๊ตฌํํ์๋ค.
โ๏ธ์๋ฐ๋ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ(OOP) ์ธ์ด๋ก, ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ, ์ ์ง๋ณด์์ฑ, ํ์ฅ์ฑ ๋ฑ์ ๋์ด๊ธฐ ์ํด ํด๋์ค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ฌ์ฉํ๋ค.
์ฐ์ต๋ฌธ์
์ฝ๋ฉ ์์ : ์ง๊ฐ์ผ๊ฐํ์ผ๊น์?
public class TriangleValidator {
public boolean isRightAngled(int side1, int side2, int side3) {
// write your code
if (side1 <= 0 || side2 <= 0 || side3 <= 0) {
return false;
}
if (side1 * side1 + side2 * side2 == side3 * side3) {
return true;
}
if (side2 * side2 + side3 * side3 == side1 * side1) {
return true;
}
if (side3 * side3 + side1 * side1 == side2 * side2) {
return true;
}
return false;
}
}
์ฝ๋ฉ ์์ : ์ฒซ ์ซ์ N๊ฐ์ ์ ๊ณฑ์ ํฉ
public class SumOfSquares {
public long calculateSumOfSquares(int n) {
if(n<0) return -1;
int answer=0;
for(int i=1;i<=n;i++){
answer+=i*i;
}
return answer;
}
}
์ฝ๋ฉ ์์ : ์ค๋ ์ธ์ง ๊ฒ์ฌํ๊ธฐ
public class LeapYearChecker {
public boolean isLeapYear(int year) {
if(year<=0) return false;
//์ค๋
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
return true;
}
return false;
}
}
์ฝ๋ฉ ์์ : ์์ ์์ธ์ง ๊ฒ์ฌํ๊ธฐ
public class PerfectNumberChecker {
public boolean isPerfectNumber(int number) {
if(number<=0) return false;
int sum=0;
for(int i=1;i<number;i++){
if(number%i==0) sum+=i;
}
return sum==number;
}
}
๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ(OOP)
Class, Object
ํด๋์ค๋ ๋ฐ์ดํฐ ๋ฉค๋ฒ (์์ฑ)์ ๋ฉ์๋ (๋์)๋ฅผ ํจ๊ป ์บก์ํํ์ฌ ๊ฐ์ฒด๋ฅผ ํ์ฑํฉ๋๋ค.
๐๐ป data(state) + action(behavior)
Online Shopping System๋ฅผ ํด๋์ค๋ก ๊ตฌํํ๋ค๋ฉด?
- customer
- name,address
- login(), logout(), selectProduct(product)
- Shopping Cart
- items
- add(item), remove(item)
- Product
- name,price,quantityAvailable
- order(),changePrice()
package oop;
public class MotorBike {
// state
short speed;
//behaviour
void start() {
System.out.println("Bike run!");
}
}
package oop;
public class MoterBikeRunner {
public static void main(String[] args) {
// TODO Auto-generated method stub
MotorBike ducati=new MotorBike();
MotorBike honda = new MotorBike();
ducati.start();
honda.start();
ducati.speed=100;
honda.speed=80;
}
}
๊ฐ ๊ฐ์ฒด=์ธ์คํด์ค(ducati, honda)๋ ๋ ๋ฆฝ์ ์ด๋ค!
โ๏ธRunnerํด๋์ค์์ ์ง์ ์ ์ผ๋ก MoterBike(์ธ์คํด์ค)๋ณ์์ ์ ๊ทผํ๊ณ ์๋ค( ๋ณ๊ฐ์ ํด๋์ค์์ ์ ๊ทผํ๊ณ ์๋ค) โ ์บก์ํ ํ๊ดด
์บก์ํ(Encapsulation)
๊ด๋ จ๋ ๋ฐ์ดํฐ์ ๋ฉ์๋๋ฅผ ํ๋์ ๋จ์๋ก ๋ฌถ๋ ๊ฒ์ ์๋ฏธํ๋ฉฐ, ์ด๋ ์ ๋ณด ์๋์ ํตํด ๊ฐ์ฒด์ ๋ด๋ถ ๊ตฌํ์ ์ธ๋ถ์์ ์ง์ ์ ๊ทผํ๋ ๊ฒ์ ์ ํํ๊ณ , ์ค์ง ๊ฐ์ฒด์ ๊ณต๊ฐ๋ ์ธํฐํ์ด์ค๋ฅผ ํตํด์๋ง ์ํธ์์ฉํ ์ ์๋๋ก ํ๋ ๊ฐ๋ ์ ๋๋ค.
โ ๋ค๋ฅธ ํด๋์ค๋ ํด๋น ๋ฐ์ดํฐ์ ์ ๊ทผ,์์ ํ๋ ค๋ฉด ๋ฉ์๋๋ฅผ ํตํด ์กฐ์ํด์ผํ๋ค. ์ธ๋ถ ๊ฐ์ฒด๊ฐ ์ง์ ๋์ ๋ฐ์ดํฐ๋ฅผ ์ ์ดํ์ง ๋ชปํ๊ฒ ๋ณดํธํ๋๊ฒ!
package oop;
public class MotorBike {
// state
private int speed; //member variable
//behavior
void setSpeed(int speed) {
this.speed=speed;
}
//getter
int getSpped(){
return this.speed;
}
void start() {
System.out.println("Bike run!");
}
}
๋ฉค๋ฒ๋ณ์: speed๋ private๋ก ๋ฐ์์ ์ ๊ทผํ ์ ์๋ค. ํด๋น ํด๋์ค๋ด์์๋ง ์ ๊ทผ ๊ฐ๋ฅ
(๐๐ป ๊ฐ์ฒด ๋ฉค๋ฒ ๋ณ์๋ฅผ ์ด๊ธฐํ ํ์ง ์์ ๊ฒฝ์ฐ ๋ํดํธ ๊ฐ์ ์ฐธ์กฐ=null, ์์=0์ด๋ค)
Getter ๋ฉ์๋: ๊ฐ์ฒด์ ํน์ ์์ฑ(๋ฉค๋ฒ ๋ณ์)์ ๊ฐ์ ์ฝ์ด์ค๋ ์ญํ ์ ํฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก get ์ ๋์ด๋ฅผ ์ฌ์ฉํ๋ฉฐ, ๋ฉค๋ฒ ๋ณ์์ ๊ฐ์ ๋ฐํํฉ๋๋ค. (๋น๊ณต๊ฐ ๋ฉค๋ฒ ๋ณ์์ ๊ฐ์ ์ก์ธ์คํ๋ ์ญํ )
Setter ๋ฉ์๋: ๊ฐ์ฒด์ ํน์ ์์ฑ(๋ฉค๋ฒ ๋ณ์)์ ๊ฐ์ ์ค์ ํ๋ ์ญํ ์ ํฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก set ์ ๋์ด๋ฅผ ์ฌ์ฉํ๋ฉฐ, ๋ฉ์๋ ํ๋ผ๋ฏธํฐ๋ฅผ ํตํด ์ ๋ฌ๋ ๊ฐ์ ๋ฉค๋ฒ ๋ณ์์ ์ ์ฅํฉ๋๋ค.
public void setSpeed(int speed) { //ducati.setSpeed(100);
System.out.println(speed); //100
System.out.println(this.speed); //0
this.speed=speed;
}
speed์ this.speed์ ๊ฐ์ด ๋ค๋ฅด๊ฒ ์ถ๋ ฅ๋๋๊ฒ์ ํ์ธํ ์ ์๋ค.
โ speed โ setSpeed ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ก ์ ์ธ๋ ์ง์ญ ๋ณ์
โ this.speed -> this.speed๋ ํ์ฌ ๊ฐ์ฒด์ ์ธ์คํด์ค ๋ณ์ (์ด๊ธฐ๊ฐ=0)
public void decreaseSpeed(int howMuch) {
if(this.speed>0) {
this.speed=this.speed - howMuch;
}
public void setSpeed(int speed) {
if (speed > 0) {
this.speed = speed;
}
}
//์ด์ฉ
public void decreaseSpeed(int howMuch) {
setSpeed(this.speed - howMuch);
}
//์๋์ผ๋ก 0๋ณด๋ค ํด๋๋ง setting
๊ฐ์ฒด ๋ด ๋ก์ง์ ์บก์ํํ์ฌ setSpeedํจ์๋ฅผ ์ฌํ์ฉํ์ฌ, 0๋ณด๋ค ํฐ ์กฐ๊ฑด์ ์์ด ๋ฐ๋ก ๊ฐ์ settingํ ์ ์๋ค.
์์ฑ์
์์ฑ์๋ ์๋ฐ ํด๋์ค์์ ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ํธ์ถ๋๋ ํน๋ณํ ๋ฉ์๋์ ๋๋ค. ๊ฐ์ฒด์ ์ด๊ธฐํ๋ฅผ ๋ด๋นํ๋ฉฐ, ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ๋ฑ ํ ๋ฒ ํธ์ถ๋๋ค.
new ํค์๋๋ก ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑ โ ์์ฑ์ ํธ์ถ
- ์์ฑ์์ ์ด๋ฆ์ ํด๋์ค์ ์ด๋ฆ๊ณผ ๋์ผํ๋ฉฐ, ๋ฐํ ๊ฐ์ด ์์ต๋๋ค.
- ๋ค๋ฅธ ๋ฉ์๋์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค. ์ฌ๋ฌ ๊ฐ์ ์์ฑ์๋ฅผ ์ ์ํ ์ ์๊ณ , ์ด๋ฅผ ํตํด ๋ค์ํ ๋ฐฉ์์ผ๋ก ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ ์ ์์ต๋๋ค.
//์ธ์คํด์ค ์์ฑ
MotorBike ducati=new MotorBike(100);
MotorBike honda = new MotorBike();
//์์ฑ์ ํจ์
//๋งค๊ฐ๋ณ์๊ฐ ์๋ ์์ฑ์
MotorBike(int speed){
this.speed=speed;
}
//๊ธฐ๋ณธ ์์ฑ์
MotorBike(){
this(5);
}
โ๏ธthis(5)
โ MotorBike(int speed) ์์ฑ์๋ฅผ ํธ์ถํ๋ฉด์ ๋งค๊ฐ๋ณ์๋ก 5๋ฅผ ์ ๋ฌํ๋ค
โ MotorBike(int speed) ์์ฑ์์ ๋ก์ง์ด ์คํ๋์ด speed ๋ฉค๋ฒ ๋ณ์์ 5๊ฐ ํ ๋น๋๋ค
โ ๊ธฐ๋ณธ ์์ฑ์ ์ญํ
โ๏ธthis๋ ์๋ฐ์์ ํ์ฌ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ์ฐธ์กฐ๋ก, ์ด ์ฝ๋์์ MotorBike๊ฐ์ฒด์ด๋ค
'โ๏ธ TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ ๋ฒ์๊ฐ๊น์ง , Jshll์ ์ด์ฉํ์์ง๋ง
์ค๋๋ถํฐ ์ดํด๋ฆฝ์ค๋ฅผ ์ฌ์ฉํ์ฌ ๋ณธ๊ฒฉ์ ์ผ๋ก ์๋ฐ์ ๊ฐ์ฒด์งํฅ ๊ฐ๋ ๊ณต๋ถ๋ฅผ ์์ํ์๋ค.
Class
//Main(Runner)ํจ์
package com.firstjavaproject;
public class MulitplicationRunner {
public static void main(String[] args) {
// TODO Auto-generated method stub
Multiplication table=new Multiplication();
// table.print();
table.print(6);
table.print(6,11,20);
}
}
package com.firstjavaproject;
public class Multiplication {
void print() {
for(int i=1;i<=10;i++) {
System.out.printf("%d * %d=%d",5,i,5*i).println();
}
}
void print(int table) {
for(int i=1;i<=10;i++) {
System.out.printf("%d * %d=%d",table,i,table*i).println();
}
}
void print(int table, int from, int to) {
for(int i=from;i<=to;i++) {
System.out.printf("%d * %d=%d",table,i,table*i).println();
}
}
}
๊ตฌ๊ตฌ๋จ์ ์ถ๋ ฅํ๋ ์ฝ๋์ด๋ค.
ํด๋์ค๋ฅผ ๋ถ๋ฆฌ(Multiplication,MulitplicationRunner)ํ์ฌ ์ฝ๋๋ฅผ ์คํํ๋ฉฐ, ์ค๋ฒ๋ก๋ฉ์ผ๋ก ๊ฐ ๋งค๊ฐ๋ณ์์ ๋ฐ๋ผ ๋ค๋ฅธ ๋ฉ์๋๋ฅผ ๊ตฌํํ์๋ค.
โ๏ธ์๋ฐ๋ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ(OOP) ์ธ์ด๋ก, ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ, ์ ์ง๋ณด์์ฑ, ํ์ฅ์ฑ ๋ฑ์ ๋์ด๊ธฐ ์ํด ํด๋์ค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ฌ์ฉํ๋ค.
์ฐ์ต๋ฌธ์
์ฝ๋ฉ ์์ : ์ง๊ฐ์ผ๊ฐํ์ผ๊น์?
public class TriangleValidator {
public boolean isRightAngled(int side1, int side2, int side3) {
// write your code
if (side1 <= 0 || side2 <= 0 || side3 <= 0) {
return false;
}
if (side1 * side1 + side2 * side2 == side3 * side3) {
return true;
}
if (side2 * side2 + side3 * side3 == side1 * side1) {
return true;
}
if (side3 * side3 + side1 * side1 == side2 * side2) {
return true;
}
return false;
}
}
์ฝ๋ฉ ์์ : ์ฒซ ์ซ์ N๊ฐ์ ์ ๊ณฑ์ ํฉ
public class SumOfSquares {
public long calculateSumOfSquares(int n) {
if(n<0) return -1;
int answer=0;
for(int i=1;i<=n;i++){
answer+=i*i;
}
return answer;
}
}
์ฝ๋ฉ ์์ : ์ค๋ ์ธ์ง ๊ฒ์ฌํ๊ธฐ
public class LeapYearChecker {
public boolean isLeapYear(int year) {
if(year<=0) return false;
//์ค๋
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
return true;
}
return false;
}
}
์ฝ๋ฉ ์์ : ์์ ์์ธ์ง ๊ฒ์ฌํ๊ธฐ
public class PerfectNumberChecker {
public boolean isPerfectNumber(int number) {
if(number<=0) return false;
int sum=0;
for(int i=1;i<number;i++){
if(number%i==0) sum+=i;
}
return sum==number;
}
}
๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ(OOP)
Class, Object
ํด๋์ค๋ ๋ฐ์ดํฐ ๋ฉค๋ฒ (์์ฑ)์ ๋ฉ์๋ (๋์)๋ฅผ ํจ๊ป ์บก์ํํ์ฌ ๊ฐ์ฒด๋ฅผ ํ์ฑํฉ๋๋ค.
๐๐ป data(state) + action(behavior)
Online Shopping System๋ฅผ ํด๋์ค๋ก ๊ตฌํํ๋ค๋ฉด?
- customer
- name,address
- login(), logout(), selectProduct(product)
- Shopping Cart
- items
- add(item), remove(item)
- Product
- name,price,quantityAvailable
- order(),changePrice()
package oop;
public class MotorBike {
// state
short speed;
//behaviour
void start() {
System.out.println("Bike run!");
}
}
package oop;
public class MoterBikeRunner {
public static void main(String[] args) {
// TODO Auto-generated method stub
MotorBike ducati=new MotorBike();
MotorBike honda = new MotorBike();
ducati.start();
honda.start();
ducati.speed=100;
honda.speed=80;
}
}
๊ฐ ๊ฐ์ฒด=์ธ์คํด์ค(ducati, honda)๋ ๋ ๋ฆฝ์ ์ด๋ค!
โ๏ธRunnerํด๋์ค์์ ์ง์ ์ ์ผ๋ก MoterBike(์ธ์คํด์ค)๋ณ์์ ์ ๊ทผํ๊ณ ์๋ค( ๋ณ๊ฐ์ ํด๋์ค์์ ์ ๊ทผํ๊ณ ์๋ค) โ ์บก์ํ ํ๊ดด
์บก์ํ(Encapsulation)
๊ด๋ จ๋ ๋ฐ์ดํฐ์ ๋ฉ์๋๋ฅผ ํ๋์ ๋จ์๋ก ๋ฌถ๋ ๊ฒ์ ์๋ฏธํ๋ฉฐ, ์ด๋ ์ ๋ณด ์๋์ ํตํด ๊ฐ์ฒด์ ๋ด๋ถ ๊ตฌํ์ ์ธ๋ถ์์ ์ง์ ์ ๊ทผํ๋ ๊ฒ์ ์ ํํ๊ณ , ์ค์ง ๊ฐ์ฒด์ ๊ณต๊ฐ๋ ์ธํฐํ์ด์ค๋ฅผ ํตํด์๋ง ์ํธ์์ฉํ ์ ์๋๋ก ํ๋ ๊ฐ๋ ์ ๋๋ค.
โ ๋ค๋ฅธ ํด๋์ค๋ ํด๋น ๋ฐ์ดํฐ์ ์ ๊ทผ,์์ ํ๋ ค๋ฉด ๋ฉ์๋๋ฅผ ํตํด ์กฐ์ํด์ผํ๋ค. ์ธ๋ถ ๊ฐ์ฒด๊ฐ ์ง์ ๋์ ๋ฐ์ดํฐ๋ฅผ ์ ์ดํ์ง ๋ชปํ๊ฒ ๋ณดํธํ๋๊ฒ!
package oop;
public class MotorBike {
// state
private int speed; //member variable
//behavior
void setSpeed(int speed) {
this.speed=speed;
}
//getter
int getSpped(){
return this.speed;
}
void start() {
System.out.println("Bike run!");
}
}
๋ฉค๋ฒ๋ณ์: speed๋ private๋ก ๋ฐ์์ ์ ๊ทผํ ์ ์๋ค. ํด๋น ํด๋์ค๋ด์์๋ง ์ ๊ทผ ๊ฐ๋ฅ
(๐๐ป ๊ฐ์ฒด ๋ฉค๋ฒ ๋ณ์๋ฅผ ์ด๊ธฐํ ํ์ง ์์ ๊ฒฝ์ฐ ๋ํดํธ ๊ฐ์ ์ฐธ์กฐ=null, ์์=0์ด๋ค)
Getter ๋ฉ์๋: ๊ฐ์ฒด์ ํน์ ์์ฑ(๋ฉค๋ฒ ๋ณ์)์ ๊ฐ์ ์ฝ์ด์ค๋ ์ญํ ์ ํฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก get ์ ๋์ด๋ฅผ ์ฌ์ฉํ๋ฉฐ, ๋ฉค๋ฒ ๋ณ์์ ๊ฐ์ ๋ฐํํฉ๋๋ค. (๋น๊ณต๊ฐ ๋ฉค๋ฒ ๋ณ์์ ๊ฐ์ ์ก์ธ์คํ๋ ์ญํ )
Setter ๋ฉ์๋: ๊ฐ์ฒด์ ํน์ ์์ฑ(๋ฉค๋ฒ ๋ณ์)์ ๊ฐ์ ์ค์ ํ๋ ์ญํ ์ ํฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก set ์ ๋์ด๋ฅผ ์ฌ์ฉํ๋ฉฐ, ๋ฉ์๋ ํ๋ผ๋ฏธํฐ๋ฅผ ํตํด ์ ๋ฌ๋ ๊ฐ์ ๋ฉค๋ฒ ๋ณ์์ ์ ์ฅํฉ๋๋ค.
public void setSpeed(int speed) { //ducati.setSpeed(100);
System.out.println(speed); //100
System.out.println(this.speed); //0
this.speed=speed;
}
speed์ this.speed์ ๊ฐ์ด ๋ค๋ฅด๊ฒ ์ถ๋ ฅ๋๋๊ฒ์ ํ์ธํ ์ ์๋ค.
โ speed โ setSpeed ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ก ์ ์ธ๋ ์ง์ญ ๋ณ์
โ this.speed -> this.speed๋ ํ์ฌ ๊ฐ์ฒด์ ์ธ์คํด์ค ๋ณ์ (์ด๊ธฐ๊ฐ=0)
public void decreaseSpeed(int howMuch) {
if(this.speed>0) {
this.speed=this.speed - howMuch;
}
public void setSpeed(int speed) {
if (speed > 0) {
this.speed = speed;
}
}
//์ด์ฉ
public void decreaseSpeed(int howMuch) {
setSpeed(this.speed - howMuch);
}
//์๋์ผ๋ก 0๋ณด๋ค ํด๋๋ง setting
๊ฐ์ฒด ๋ด ๋ก์ง์ ์บก์ํํ์ฌ setSpeedํจ์๋ฅผ ์ฌํ์ฉํ์ฌ, 0๋ณด๋ค ํฐ ์กฐ๊ฑด์ ์์ด ๋ฐ๋ก ๊ฐ์ settingํ ์ ์๋ค.
์์ฑ์
์์ฑ์๋ ์๋ฐ ํด๋์ค์์ ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ํธ์ถ๋๋ ํน๋ณํ ๋ฉ์๋์ ๋๋ค. ๊ฐ์ฒด์ ์ด๊ธฐํ๋ฅผ ๋ด๋นํ๋ฉฐ, ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ๋ฑ ํ ๋ฒ ํธ์ถ๋๋ค.
new ํค์๋๋ก ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑ โ ์์ฑ์ ํธ์ถ
- ์์ฑ์์ ์ด๋ฆ์ ํด๋์ค์ ์ด๋ฆ๊ณผ ๋์ผํ๋ฉฐ, ๋ฐํ ๊ฐ์ด ์์ต๋๋ค.
- ๋ค๋ฅธ ๋ฉ์๋์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค. ์ฌ๋ฌ ๊ฐ์ ์์ฑ์๋ฅผ ์ ์ํ ์ ์๊ณ , ์ด๋ฅผ ํตํด ๋ค์ํ ๋ฐฉ์์ผ๋ก ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ ์ ์์ต๋๋ค.
//์ธ์คํด์ค ์์ฑ
MotorBike ducati=new MotorBike(100);
MotorBike honda = new MotorBike();
//์์ฑ์ ํจ์
//๋งค๊ฐ๋ณ์๊ฐ ์๋ ์์ฑ์
MotorBike(int speed){
this.speed=speed;
}
//๊ธฐ๋ณธ ์์ฑ์
MotorBike(){
this(5);
}
โ๏ธthis(5)
โ MotorBike(int speed) ์์ฑ์๋ฅผ ํธ์ถํ๋ฉด์ ๋งค๊ฐ๋ณ์๋ก 5๋ฅผ ์ ๋ฌํ๋ค
โ MotorBike(int speed) ์์ฑ์์ ๋ก์ง์ด ์คํ๋์ด speed ๋ฉค๋ฒ ๋ณ์์ 5๊ฐ ํ ๋น๋๋ค
โ ๊ธฐ๋ณธ ์์ฑ์ ์ญํ
โ๏ธthis๋ ์๋ฐ์์ ํ์ฌ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ์ฐธ์กฐ๋ก, ์ด ์ฝ๋์์ MotorBike๊ฐ์ฒด์ด๋ค