Search
Duplicate

Object - Oriented Java

νƒœκ·Έ
객체지ν–₯
ꡬ뢄
코데카데미

Class

What is class?
클래슀λ₯Ό κ°„λ‹¨ν•˜κ²Œ λ§ν•˜μžλ©΄ β€˜μ²­μ‚¬μ§„β€™ 이라 ν•  수 μžˆλ‹€. λ§κ·ΈλŒ€λ‘œ μš°λ¦¬κ°€ 집어넣을 new data type(instance) κ³Ό κ·Έ 데이터가 μ–΄λ–»κ²Œ 행동할지λ₯Ό λ§ν•΄μ£ΌλŠ” 것이닀.
ꡬ쑰λ₯Ό κ°„λ‹¨ν•˜κ²Œ 그리면 λ‹€μŒκ³Ό κ°™λ‹€.
[μ˜ˆμ‹œ μ½”λ“œ]
public class Store { // instance fields String productType; int inventoryCount; double inventoryPrice; // constructor method public Store(String product, int count, double price) { productType = product; inventoryCount = count; inventoryPrice = price; } // main method public static void main(String[] args) { Store lemonadeStand = new Store("lemonade", 42, .99); Store cookieShop = new Store("cookies", 12, 3.75); System.out.println("Our first shop sells " + lemonadeStand.productType + " at " + lemonadeStand.inventoryPrice + " per unit."); System.out.println("Our second shop has " + cookieShop.inventoryCount + " units remaining."); } }
Java
볡사
Store λΌλŠ” 클래슀 μ•ˆμ—λŠ” λ‹€μ–‘ν•œ 데이터 νƒ€μž…λ“€μ΄ μ§€μ •λ˜μ–΄ μžˆλ‹€. λ˜ν•œ 메인 method 외에도 λ‹€λ₯Έ method 도 μžˆλŠ” 것을 확인 ν•  수 μžˆλ‹€. 이 클래슀λ₯Ό μ‹€ν–‰ν•œλ‹€λ©΄ κ²°κ³ΌλŠ” μ–΄λ–»κ²Œ λ‚˜μ˜¬κΉŒ?
Output
Our first shop sells lemonade at 0.99 per unit. Our second shop has 12 units remaining.
Object
μœ„ μ˜ˆμ‹œμ˜ 좜λ ₯문을 보자. 클래슀 μ•ˆμ—λŠ” μ‹€μ œ 값이 μ•„λ‹Œ λ‹€μ–‘ν•œ 데이터 νƒ€μž…λ“€μ΄ μ§€μ •λ˜μ–΄ μžˆλ‹€κ³  ν–ˆλ‹€. κ·Έλ ‡λ‹€λ©΄ 레λͺ¨λ„€μ΄λ“œμ˜ 가격 λ“±κ³Ό 같은 μ‹€μ œ 값은 뭐라고 λΆˆλŸ¬μ•Ό ν• κΉŒ? λ°”λ‘œ object 이닀.
ν΄λž˜μŠ€μ™€ 였브젝트의 ꡬ쑰도λ₯Ό 보면 λŒ€λž΅ λ‹€μŒκ³Ό κ°™λ‹€.
즉, Class λŠ” μ²­μ‚¬μ§„μœΌλ‘œ, Object λŠ” κ΅¬ν˜„ν•  λŒ€μƒμ΄λΌκ³  보면 λœλ‹€. 클래슀의 μΈμŠ€ν„΄μŠ€ 라고도 λΆˆλ¦°λ‹€.

Instance λž€?

Class λ₯Ό λ°”νƒ•μœΌλ‘œ μ˜€λΈŒμ νŠΈμ— κ΅¬ν˜„λœ ꡬ체적인 싀체이며, μ›λ³ΈμœΌλ‘œλΆ€ν„° μƒμ„±λœ λ³΅μ œλ³Έμ„ λ§ν•œλ‹€. 이 싀체화 된 μΈμŠ€ν„΄μŠ€λŠ” λ©”λͺ¨λ¦¬μ— ν• λ‹Ή λœλ‹€.
Syntax of Class
public class example { // 클래슀 이름은 μžλ°” 파일과 같이 ν•΄μ€˜μ•Ό ν•œλ‹€. public static void main(String[] args){ // main λ¬Έ μ—†μ΄λŠ” λŒμ•„κ°€μ§€ μ•ŠλŠ”λ‹€! } }
Java
볡사
Constructors
객체(클래슀의 instance) λ₯Ό λ§Œλ“€κΈ° μœ„ν•΄μ„œλŠ” Constructor method κ°€ ν•„μš”ν•˜λ©°, ConstructorλŠ” 클래슀 μ•ˆμ— μ •μ˜λ˜μ–΄ μžˆλ‹€.
[μ˜ˆμ‹œ μ½”λ“œ]
public class Car { // Constructor method public Car() { // instructions for creating a Car instance } public static void main(String[] args) { // body of main method } }
Java
볡사
instance λ₯Ό λ§Œλ“€κΈ° μœ„ν•΄μ„œλŠ” 메인 λ©”μ„œλ“œ μ•ˆμ—μ„œ constructor λ₯Ό ν˜ΈμΆœν•΄μ•Ό ν•œλ‹€.
[μ˜ˆμ‹œ μ½”λ“œ]
public class Car { public Car() { // instructions for creating a Car instance } public static void main(String[] args) { // Invoke the constructor Car ferrari = new Car(); } }
Java
볡사
μ˜ˆμ‹œμ—μ„œ λ³€μˆ˜ ferrari λŠ” reference data type 이닀. 즉 μΈμŠ€ν„΄μŠ€ λ©”λͺ¨λ¦¬ μ£Όμ†Œμ˜ reference 이닀. μ •μ˜λ₯Ό 내릴 λ•Œμ˜ ꡬ문은 λ‹€μŒκ³Ό κ°™λ‹€.
[클래슀λͺ…] [λ³€μˆ˜μ΄λ¦„] = new [constructor name]();
μ—¬κΈ°μ„œ new λΌλŠ” ν‚€μ›Œλ“œλ₯Ό μƒλž΅ν•˜λ©΄ μ—λŸ¬κ°€ λ‚˜νƒ€λ‚˜κ²Œ λœλ‹€.
μœ„μ™€ 같은 ꡬ문을 톡해 Car instance λ₯Ό ferrari λΌλŠ” λ³€μˆ˜λ‘œ 지정할 수 μžˆλ‹€.
이 λ³€μˆ˜λ₯Ό 좜λ ₯ν•˜λ©΄ μš°λ¦¬λŠ” ferrari 의 λ©”λͺ¨λ¦¬ μ£Όμ†Œλ₯Ό μ•Œ 수 μžˆλ‹€.

λ§Œμ•½ referenceλ₯Ό null둜 μ§€μ •ν•œλ‹€λ©΄ μ–΄λ–»κ²Œ 될까?

null 은 값을 가지고 μžˆμ§€ μ•Šλ‹€. λ§Œμ•½ null 을 object 둜 μ§€μ •ν•œλ‹€λ©΄ κ·Έ object λŠ” void reference κ°€ 될것이닀. μ•„λž˜μ™€ 같이 Car μ΄λΌλŠ” instance λ₯Ό λ§Œλ“€κ³ , reference λ₯Ό μ§€μ •ν•œ 뒀에 κ·Έ 값을 null 둜 λ°”κΏ”λ³΄μž.
[μ˜ˆμ‹œ μ½”λ“œ]
Car thunderBird = new Car(); System.out.println(thunderBird); // Prints: Car@76ed5528 thunderBird = null; // change value to null System.out.println(thunderBird); // Prints: null
Java
볡사
차이가 λ³΄μ΄λŠ”κ°€?

[ν•œκ±ΈμŒ 더]

λ§Œμ•½ λ©”μ„œλ“œλ₯Ό λΆ€λ₯΄κ±°λ‚˜ instance λ³€μˆ˜μ— μ ‘κ·Όν•˜κΈ° μœ„ν•΄ null reference λ₯Ό μ΄μš©ν•œλ‹€λ©΄ μ–΄λ–»κ²Œ 될까? μš°λ¦¬λŠ” NullPointerException μ—λŸ¬λ₯Ό 보게 될 것이닀.

μ½”λ“œλ₯Ό λ³΄λ©΄μ„œ μ‹€ν–‰ μˆœμ„œ μ΄ν•΄ν•˜κΈ°

[μ˜ˆμ‹œ μ½”λ“œ]
public class Store { // new method: constructor! public Store() { System.out.println("I am inside the constructor method."); } // main method is where we create instances! public static void main(String[] args) { System.out.println("Start of the main method."); // create the instance below Store lemonadeStand; lemonadeStand = new Store(); // print the instance below System.out.println(lemonadeStand); } }
Java
볡사
1.
메인 λ©”μ„œλ“œλ₯Ό μ‹€ν–‰ν•œλ‹€.
2.
instanceλ₯Ό λ§Œλ“€μ—ˆκΈ° λ•Œλ¬Έμ— 메인 λ©”μ„œλ“œμ—μ„œ Store constructor둜 μ΄λ™ν•œλ‹€.
3.
Store λ©”μ„œλ“œκ°€ 싀행이 λœλ‹€.
4.
Store λ©”μ„œλ“œμ˜ 싀행이 λλ‚˜λ©΄ λ‹€μ‹œ 메인 λ©”μ„œλ“œλ‘œ λŒμ•„μ™€ 좜λ ₯을 ν•˜κ²Œ λœλ‹€.
Instance Fields
instance λ³€μˆ˜λ‚˜ instance fields λ₯Ό μ„€λͺ…ν•˜κΈ° μœ„ν•΄ μš°λ¦¬λŠ” object 에 데이터λ₯Ό μΆ”κ°€ν•œλ‹€. κ·Έλ ‡λ‹€λ©΄ μ—¬κΈ°μ„œ instance fields λž€ λ¬΄μ—‡μΌκΉŒ? μ•„λž˜μ˜ μ½”λ“œλ₯Ό 보면 μ‰½κ²Œ 이해 κ°€λŠ₯ν•˜λ‹€.
[μ˜ˆμ‹œ μ½”λ“œ]
public class Car { /* declare fields inside the class by specifying the type and name */ String color; public Car() { /* instance fields available in scope of constructor method */ } public static void main(String[] args) { // body of main method } }
Java
볡사
단어 뜻 κ·ΈλŒ€λ‘œ μ˜μ—­μ΄λ‹€. 이 μ˜μ—­μ΄ 어디에 μœ„μΉ˜ν•΄ μžˆλŠλƒμ— 따라 영ν–₯을 λ―ΈμΉ˜λŠ” 곳이 λ‹€λ₯΄λ‹€.
Constructor Parameters
dynamic, individual μƒνƒœμ˜ objectλ₯Ό λ§Œλ“€κΈ° μœ„ν•΄ constructor method 와 μΈμŠ€ν„΄μŠ€ ν•„λ“œλ₯Ό μ΄μš©ν•œλ‹€.
잘 생각해보면 μš°λ¦¬λŠ” 이미 메인 λ©”μ„œλ“œμ—μ„œ νŒŒλΌλ―Έν„°λ₯Ό λ³Έ 적이 μžˆλ‹€. String[] args κ°€ λ°”λ‘œ 그것이닀. 메인 λ©”μ„œλ“œ 말고 λ‹€λ₯Έ constructor λ©”μ„œλ“œμ— μžˆλŠ” νŒŒλΌλ―Έν„°λ₯Ό μ•„λž˜μ˜ μ˜ˆμ‹œμ—μ„œ ν™•μΈν•΄λ³΄μž.
[μ˜ˆμ‹œ μ½”λ“œ]
public class Car { String color; // constructor method with a parameter public Car(String carColor) { // parameter value assigned to the field color = carColor; } public static void main(String[] args) { // program tasks } }
Java
볡사
μ—¬κΈ°μ„œ String carColorλŠ” parameter 이며, color λŠ” constructor λ©”μ„œλ“œ μ™ΈλΆ€μ—μ„œ μ§€μ •λœ λ³€μˆ˜μ΄λ‹€. 이 λ³€μˆ˜μ˜ 값을 contstructor λ©”μ„œλ“œ μ•ˆμ—μ„œ μ§€μ •ν•˜κΈ° μœ„ν•΄μ„œ parameter λ₯Ό μ‚¬μš©ν–ˆλ‹€. κ·Έλ ‡κ²Œ ν•΄μ•Ό μΈμŠ€ν„΄μŠ€μ— μ§€μ •ν•˜κΈ° μ›ν•˜λŠ” 데이터에 μ ‘κ·Όν•  수 있기 λ•Œλ¬Έμ΄λ‹€.

Formal parameter

formal parameter λŠ” λ°μ΄ν„°μ˜ νƒ€μž…μ΄λ‚˜ 이름과 같은 값을 λͺ…μ‹œν•˜λ©°, μ΄λŠ” λ©”μ„œλ“œλ₯Ό 톡과 ν•  수 μžˆλ‹€. μœ„μ˜ μ˜ˆμ‹œμ—μ„œ μ°Ύμ•„λ³Έλ‹€λ©΄, String carColor κ°€ λ˜κ² λ‹€. carColor λŠ” constructor λ©”μ„œλ“œλ₯Ό ν†΅κ³Όν•˜λŠ” String ν˜•νƒœμ˜ μ–΄λ– ν•œ 값이든 λŒ€ν‘œν•  것이닀.
μžλ°”μ—μ„œλŠ” ν•œ ν΄λž˜μŠ€κ°€ μ—¬λŸ¬κ°œμ˜ constructorλ₯Ό κ°€μ§€λŠ” 것도 κ°€λŠ₯ν•˜λ‹€. λŒ€μ‹  λ‹€λ₯Έ parameter λ₯Ό κ°€μ Έμ•Ό ν•œλ‹€. μ•„λž˜μ˜ μ˜ˆμ‹œλ₯Ό 보자.
[μ˜ˆμ‹œ μ½”λ“œ]
public class Car { String color; int mpg; boolean isElectric; // constructor 1 public Car(String carColor, int milesPerGallon) { color = carColor; mpg = milesPerGallon; } // constructor 2 public Car(boolean electricCar, int milesPerGallon) { isElectric = electricCar; mpg = milesPerGallon; } }
Java
볡사
μœ„μ˜ μ˜ˆμ‹œλ₯Ό 보면 λ‘κ°œμ˜ constructor κ°€ μžˆλ‹€λŠ” 것을 μ•Œ 수 μžˆλ‹€. λ§Œμ•½μ— object λ₯Ό μ‹€ν–‰ν•œλ‹€λ©΄ complier λŠ” argument 의 νƒ€μž…κ³Ό constructor 의 μ‹œκ·Έλ‹ˆμ²˜λ₯Ό 비ꡐ 해보고 μ–΄λ–€ constructor 을 μ‚¬μš©ν•  것인지 νŒλ‹¨ ν•œλ‹€.
λ§Œμ•½ constructor λ₯Ό μ •μ˜ν•˜μ§€ μ•Šμ•˜λ‹€λ©΄ μžλ°” μ»΄νŒŒμΌλŸ¬λŠ” μ•„λ¬΄λŸ° argument 도 ν¬ν•¨ν•˜μ§€ μ•ŠλŠ” λ””ν΄νŠΈ μƒμ„±μžλ₯Ό μƒμ„±ν•˜κ³  λ””ν΄νŠΈ 값을 지정할 것이닀. λ””ν΄νŠΈ 값은 μ„ μ–Έν•˜λŠ” λ™μ•ˆ μΈμŠ€ν„΄μŠ€ ν•„λ“œμ— μ§€μ •λœ 값에 μ˜ν•΄ λ§Œλ“€μ–΄ 진닀.
[μ˜ˆμ‹œ μ½”λ“œ]
public class Car { String color = "red"; boolean isElectric = false; int cupHolders = 4; public static void main(String[] args) { Car myCar = new Car(); System.out.println(myCar.color); // Prints: red } }
Java
볡사
μ˜ˆμ‹œλ₯Ό 보면 constructor 도 μ—†κ³ , 이 λ•Œλ¬Έμ— constructor λ₯Ό ν˜ΈμΆœν•  argument 도 μ—†λ‹€λŠ” 것을 μ•Œ 수 μžˆλ‹€.
signature
λ©”μ„œλ“œμ˜ νŒŒλΌλ―Έν„°μ™€ 이름을 μ •μ˜ν•˜λŠ”κ²ƒμ„ λ§ν•œλ‹€.
μœ„ μ˜ˆμ‹œ μ—μ„œλŠ” Car(String carColor, int milesPerCallon) 이 ν•΄λ‹Ήλœλ‹€.
parameter 와 argument
단어
λ²ˆμ—­
의미
Parameter
λ§€κ°œλ³€μˆ˜
λ©”μ„œλ“œ μž…λ ₯ λ³€μˆ˜(Variable) λͺ…
Argument
μ „λ‹¬μΈμž, 인자
λ©”μ„œλ“œλ₯Ό 호좜 ν•  λ•Œμ˜ μž…λ ₯ κ°’(Value)
Assigning Values to Instance Fields
μ˜ˆμ‹œλ₯Ό λ¨Όμ € 보고 μˆœμ„œλ₯Ό 잘 이해해 보자!
[μ˜ˆμ‹œ μ½”λ“œ]
public class Car { String color; public Car(String carColor) { // assign parameter value to instance field color = carColor; } public static void main(String[] args) { // parameter value supplied when calling constructor Car ferrari = new Car("red"); }
Java
볡사
μƒμ„±μž Car 의 호좜둜 인해 β€œred” λΌλŠ” 슀트링 값을 전달할 것이닀. μ΄λ•Œ β€œred” λΌλŠ” κ°’μ˜ νƒ€μž…μ€ λ°˜λ“œμ‹œ νŒŒλΌλ―Έν„°μ˜ νƒ€μž…κ³Ό μΌμΉ˜ν•΄μ•Ό ν•œλ‹€. 일치 ν•œλ‹€λ©΄, μƒμ„±μž Car μ•ˆμ—μ„œ νŒŒλΌλ―Έν„°λŠ” μΈμŠ€ν„΄μŠ€ μ˜μ—­μ— μžˆλŠ” color 에 지정될 것이닀. color λŠ” 첫뢀뢄에 μ–΄λ–€ νƒ€μž…μ„ κ°€μ§€λŠ”μ§€ 선언이 λ˜μ—ˆκΈ° λ•Œλ¬Έμ— λ”°λ‘œ νƒ€μž…μ„ 지정 ν•΄ 쀄 ν•„μš”λŠ” μ—†λ‹€. 결둠적으둜 객체 ferrari λŠ” β€œred” λΌλŠ” 값을 color 의 μƒνƒœλ‘œ 가지고 μžˆλ‹€. λ§Œμ•½μ— 이 값에 μ ‘κ·Όν•˜κ³  μ‹Άλ‹€λ©΄ μ•„λž˜μ™€ 같이 μ„ μ–Έν•˜λ©΄ λœλ‹€.
[μ˜ˆμ‹œ μ½”λ“œ]
/* accessing a field: objectName.fieldName */ ferrari.color; // "red"
Java
볡사
Actual parameter λ‚˜ argument λŠ” λ©”μ„œλ“œκ°€ ν˜ΈμΆœλ˜λŠ” λ™μ•ˆμ— μ „λ‹¬λ˜λŠ” 값을 λ§ν•œλ‹€. argument 값을 μ΄μš©ν•˜μ—¬ ν˜ΈμΆœμ„ ν•  λ•Œ, argument κ°€ μ „λ‹¬λ˜λ©΄ formal parameter λŠ” argument 값을 λ³΅μ‚¬ν•˜μ—¬ 이λ₯Ό 처음 κ°’μœΌλ‘œ μ‚ΌλŠ”λ‹€.
Multiple Fields
κ°μ²΄λŠ” ν•„μš”μ— 따라 μ—¬λŸ¬κ°€μ§€ μΈμŠ€ν„΄μŠ€ μ˜μ—­μ„ κ°€μ§ˆ 수 μžˆλ‹€. μ•„λž˜μ˜ μ˜ˆμ‹œλ₯Ό 보자.
[μ˜ˆμ‹œ μ½”λ“œ]
public class Car { String color; // new fields! boolean isRunning; int velocity; // new parameters that correspond to the new fields public Car(String carColor, boolean carRunning, int milesPerHour) { color = carColor; // assign new parameters to the new fields isRunning = carRunning; velocity = milesPerHour; } public static void main(String[] args) { // new values passed into the method call Car ferrari = new Car("red", true, 27); Car renault = new Car("blue", false, 70); System.out.println(renault.isRunning); // false System.out.println(ferrari.velocity); // 27 } }
Java
볡사
μ˜ˆμ‹œμ—μ„œμ˜ μƒμ„±μžλŠ” μƒˆλ‘œμš΄ μ˜μ—­μ˜ 값을 λ°›κΈ° μœ„ν•œ λ‹€μˆ˜μ˜ νŒŒλΌλ―Έν„°λ₯Ό 가지고 있으며, 각각의 νƒ€μž…λ„ λͺ…μ‹œλ˜μ–΄ μžˆλ‹€. 메인 λ©”μ„œλ“œμ—μ„œ μƒμ„±μžλ₯Ό ν˜ΈμΆœν•˜κΈ° μœ„ν•΄ μΈμŠ€ν„΄μŠ€λ₯Ό 생성할 λ•ŒλŠ” λ°˜λ“œμ‹œ νŒŒλΌλ―Έν„°μ˜ μˆœμ„œμ™€ λ˜‘κ°™μ΄ 값을 λ„£μ–΄μ€˜μ•Ό ν•œλ‹€.
좔상화 기법
Classification: 객체λ₯Ό 좔상적인 κ°œλ…μœΌλ‘œ λ¬ΆλŠ” 것을 λ§ν•œλ‹€.
Instantiation: 클래슀 λ‚΄μ˜ 객체에 λŒ€ν•΄ μ–΄λ–»κ²Œ μ‚¬μš©ν•  것인지 μ •μ˜ ν•˜κ³ , 이름을 λΆ™μ΄λŠ” λ“±μ˜ μž‘μ—…μ„ 톡해 μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“œλŠ” 것이닀.

Methods

What is Methods?
λ©”μ„œλ“œλŠ” νŠΉμ •ν•œ 일을 λ‹¬μ„±ν•˜κΈ° μœ„ν•΄ 반볡적으둜 μ‚¬μš©λ˜λŠ” μ½”λ“œ 블둝이닀. λ§Žμ€ 일을 ν•œλ²ˆμ˜ λ©”μ„œλ“œλ‘œ μ‹€ν–‰ν•˜κΈ°μ—λŠ” 무리가 있기 λ•Œλ¬Έμ— λ©”μ„œλ“œ λΆ„ν•΄λ₯Ό ν†΅ν•΄μ„œ μž‘κ³ , νŠΉμ •ν•œ 일듀을 λ™μ‹œμ— μˆ˜ν–‰ν•œλ‹€.
개인적으둜 코딩을 ν•˜κΈ° λ³΄λ‹€λŠ” μΌν„°μ—μ„œ ν˜‘μ—…μ„ ν•˜λŠ” κ²½μš°κ°€ λ§Žμ•„μ§ˆ 것이닀. μ΄λ•Œ λͺ¨λ“  μ‚¬λžŒλ“€μ΄ νŠΉμ • λ©”μ„œλ“œκ°€ μ–΄λ–»κ²Œ μž‘λ™ν•˜λŠ”μ§€λŠ” μ•Œ ν•„μš”κ°€ μ—†λ‹€. λŒ€μ‹  μ–΄λ–€ κ²½μš°μ— μ–΄λ–€ λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λŠ”μ§€λŠ” μ•Œμ•„μ•Ό ν•œλ‹€. 이λ₯Ό procedural abstrction 이라고 ν•œλ‹€.
Defining Methods
μš©μ–΄ 정리
public : λ‹€λ₯Έ ν΄λž˜μŠ€λ“€λ„ 이 λ©”μ„œλ“œμ— 접근이 κ°€λŠ₯ν•˜λ‹€λŠ” λœ»μ„ λ‹΄κ³  μžˆλ‹€.
void : λ©”μ„œλ“œλ₯Ό 톡해 νŠΉμ •ν•œ 값을 내놓지 μ•ŠλŠ”λ‹€λŠ” λœ»μ΄λ‹€.
method signature: λ©”μ„œλ“œμ˜ 이름과 λ³€μˆ˜ νƒ€μž…μ„ 말해쀀닀.
Calling Methods
객체에 λ§€μ„œλ“œλ₯Ό ν˜ΈμΆœν•΄μ•Ό λ§€μ„œλ“œκ°€ μˆ˜ν–‰λœλ‹€. ν˜ΈμΆœν•  λ•Œμ—λŠ” λ‹€μŒκ³Ό 같은 ꡬ문을 μ΄μš©ν•œλ‹€.
[객체λͺ…].[호좜 ν•  λ©”μ„œλ“œλͺ…]();
μ½”λ“œλŠ” 일반적으둜 νƒ‘λ‹€μš΄ λ°©μ‹μœΌλ‘œ μ‹€ν–‰λœλ‹€. κ·ΈλŸ¬λ‚˜ λ©”μ„œλ“œμ˜ 경우, ν˜ΈμΆœλ˜μ§€ μ•ŠλŠ” 이상 μ»΄νŒŒμΌλŸ¬κ°€ λ¬΄μ‹œν•œλ‹€. λ§Œμ•½ 호좜 λ˜μ—ˆλ‹€λ©΄ μ»΄νŒŒμΌλŸ¬λŠ” λ©”μ„œλ“œ μ•ˆμ— μžˆλŠ” λͺ¨λ“  λ‚΄μš©λ“€μ΄ νƒ‘λ‹€μš΄ λ°©μ‹μœΌλ‘œ μ§„ν–‰λœλ‹€.
Scope
λ§€μ„œλ“œλŠ” 클래슀의 객체가 μ‹€ν–‰ν•΄μ•Όν•  일이라고 ν•  수 μžˆλ‹€. 이 일의 λ²”μœ„λŠ” { } 둜 μ§€μ •λœλ‹€. 이것을 λ§€μ„œλ“œμ˜ λ²”μœ„ 라고 λΆ€λ₯Έλ‹€. 이 λ²”μœ„ λ°–μ˜ λ³€μˆ˜μ—λŠ” μ ‘κ·Ό ν•  수 μ—†λ‹€λŠ” 것을 λͺ…μ‹¬ν•˜μž!
Adding Parameters
λ©”μ„œλ“œμ˜ νŒŒλΌλ―Έν„° λ¦¬μŠ€νŠΈκ°€ λ‹€λ₯Έ λ©”μ„œλ“œλ“€κ³Ό λ‹€λ₯΄λ‹€λ©΄ λ©”μ„œλ“œ λͺ…은 같아도 λœλ‹€.
[μ˜ˆμ‹œ μ½”λ“œ]
// Method 1 public void startRadio(double stationNum, String stationName) { System.out.println("Turning on the radio to " + stationNum + ", " + stationName + "!"); System.out.println("Enjoy!"); } // Method 2 public void startRadio(double stationNum) { System.out.println("Turning on the radio to " + stationNum + "!"); } public static void main(String[] args){ Car myCar = new Car("red"); // Calls the first startRadio() method myCar.startRadio(103.7, "Meditation Station"); // Calls the second startRadio() method myCar.startRadio(98.2); }
Java
볡사
Returns
λ³€μˆ˜λ“€μ€ μ„ μ–Έλœ λ²”μœ„ λ‚΄μ—μ„œλ§Œ μ‘΄μž¬ν•  수 μžˆλ‹€. ν•˜μ§€λ§Œ λ©”μ„œλ“œμ— 리턴 ν‚€μ›Œλ“œκ°€ μžˆλŠ” 경우, λ©”μ„œλ“œλ‘œ λΆ€ν„° 리턴 받은 값을 λ²”μœ„ λ°–μ—μ„œλ„ μ“Έ 수 μžˆλ‹€.
[μ˜ˆμ‹œ μ½”λ“œ]
public int numberOfTires() { int tires = 4; // return statement return tires; }
Java
볡사
이 λ©”μ„œλ“œμ—μ„œλŠ” 4 λΌλŠ” 값을 λ°˜ν™˜ ν•  것이닀. 리턴 ꡬ문이 μ‹€ν–‰λ˜λ©΄ complier λŠ” ν•¨μˆ˜λ₯Ό μ‹€ν–‰ μ‹œν‚€λŠ”λ°, μ΄λ•Œ 리턴 λ¬Έμž₯ 이후에 μžˆλŠ” μ½”λ“œλ“€μ€ λ¬΄μ‹œν•œλ‹€.

λ§Œμ•½ μƒˆλ‘œ λ§Œλ“  λ©”μ„œλ“œκ°€ void 인 경우 μ–΄λ–»κ²Œ ν•΄μ•Όν• κΉŒ?

void λŠ” λΉ„μ–΄μžˆμŒμ„ μ˜λ―Έν•œλ‹€. 즉, λ©”μ„œλ“œκ°€ 호좜 된 이후에 λ°˜ν™˜ 될 값이 μ—†λ‹€λŠ” 것이닀. 이럴 λ•Œμ—λŠ” 리턴 νƒ€μž…μ„ λͺ…μ‹œν•΄ μ€˜μ•Ό ν•˜κΈ°λ•Œλ¬Έμ— void λ₯Ό 리턴 νƒ€μž…μœΌλ‘œ λ°”κΎΈμ–΄ μ£Όμ–΄μ•Ό ν•œλ‹€.
non-void λ©”μ„œλ“œμ˜ 경우 μ•„λž˜μ™€ 같이 쓰일 수 μžˆλ‹€.
[μ˜ˆμ‹œ μ½”λ“œ]
public static void main(String[] args){ Car myCar = new Car("red"); int numTires = myCar.numberOfTires(); }
Java
볡사
메인 λ©”μ„œλ“œ μ•ˆμ— μžˆλŠ” myCar 에 numberOfTires() λ©”μ„œλ“œλ₯Ό 호좜 ν•  수 μžˆλ‹€. λ©”μ„œλ“œκ°€ 4λΌλŠ” 값을 λ°˜ν™˜ ν•œ μ΄ν›„μ—λŠ” numTires 라고 λΆˆλ¦¬λŠ” λ³€μˆ˜ μ•ˆμ— λ³΅μ‚¬λœ 값인 4κ°€ μ €μž₯ 될 것이닀.

λ©”μ„œλ“œμ˜ primitive 값이 μ•„λ‹Œ 객체의 값을 λ°˜ν™˜ν•˜λ©΄ μ–΄λ–»κ²Œ 될까?

[μ˜ˆμ‹œ μ½”λ“œ]
class CarLot { Car carInLot; public CarLot(Car givenCar) { carInLot = givenCar; } public Car returnACar() { // return Car object return carInLot; } public static void main(String[] args) { Car myCar = new Car("red", 70); System.out.println(myCar); CarLot myCarLot = new CarLot(myCar); System.out.println(myCarLot.returnACar()); } }
Java
볡사
primitive 값은 κ°’μ˜ 볡사본이 λ°˜ν™˜λœλ‹€. ν•˜μ§€λ§Œ 객체λ₯Ό λ°˜ν™˜ ν•  경우, 객체의 볡사본 λŒ€μ‹ μ— 객체λ₯Ό μ°Έμ‘° ν•œλ‹€. μœ„μ˜ μ˜ˆμ‹œλ₯Ό 보면 carInLot μ΄λΌλŠ” ν΄λž˜μŠ€λŠ” Car 을 νŒŒλΌλ―Έν„°(λ³€μˆ˜λͺ…) 둜 μ“΄λ‹€. 그리고 Car 객체λ₯Ό λ°˜ν™˜ν•˜λŠ” λ©”μ„œλ“œλ₯Ό 포함 ν•œλ‹€. 이 μ½”λ“œλ₯Ό 좜λ ₯해보면 같은 λ©”λͺ¨λ¦¬ μ£Όμ†Œλ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŒμ„ μ•Œ 수 μžˆλ‹€. μ™œλƒν•˜λ©΄ myCar κ³Ό carInLot 이 λ‘˜λ‹€ λ˜‘κ°™μ€ μ°Έμ‘°ν˜• 값을 가지고 있기 λ•Œλ¬Έμ΄λ‹€.
The toString() Method
객체λ₯Ό 좜λ ₯ν•˜λ©΄ 객체의 λ©”λͺ¨λ¦¬μƒ μœ„μΉ˜κ°€ 결과둜 λ‚˜μ˜€λŠ” 것을 μ•Œ 수 μžˆλ‹€. ν•˜μ§€λ§Œ toString() λ©”μ„œλ“œλ₯Ό μ΄μš©ν•˜λ©΄ 객체λ₯Ό 좜λ ₯ ν•  수 μžˆλ‹€.
[μ˜ˆμ‹œ μ½”λ“œ]
class Car { String color; public Car(String carColor) { color = carColor; } public static void main(String[] args){ Car myCar = new Car("red"); System.out.println(myCar); } public String toString(){ return "This is a " + color + " car!"; } }
Java
볡사
Constructor VS Methods
Constructor
(1) μ–΄λ– ν•œ 값도 λ°˜ν™˜ν•˜μ§€ μ•Šμ§€λ§Œ void λ₯Ό μ‚¬μš©ν•˜μ§€λŠ” μ•ŠλŠ”λ‹€.
(2) 주둜 객체λ₯Ό μ΄ˆκΈ°ν™” ν•˜λŠ” 데 μ‚¬μš©ν•˜λ©°, μΈμŠ€ν„΄μŠ€λ₯Ό 생성할 λ•Œ μ‹€ν–‰λ˜μ–΄μ•Ό ν•  μž‘μ—…μ„ μœ„ν•΄μ„œ μ‚¬μš©λ˜κΈ°λ„ ν•œλ‹€.
(3) 클래슀 내에 μ„ μ–Έλ˜λ©°, 클래슀의 이름과 κ°™μ•„μ•Ό ν•œλ‹€.
(4) λ‹€λ₯Έ λ©”μ„œλ“œλ“€κ³Ό 같이 μ˜€λ²„λ‘œλ”©μ΄ κ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμ— ν•˜λ‚˜μ˜ ν΄λž˜μŠ€λŠ” μ—¬λŸ¬κ°€μ§€ μƒμ„±μžλ₯Ό κ°€μ§ˆ 수 μžˆλ‹€.
(5) new λ₯Ό ν†΅ν•΄μ„œ μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜κ³ , μƒμ„±μžλŠ” μΈμŠ€ν„΄μŠ€ 생성 μ‹œ μ΄ˆκΈ°ν™”μ— μ‚¬μš©λœλ‹€.
(6) μ’…λ₯˜
1.
No Argument μƒμ„±μž
2.
νŒŒλΌλ―Έν„°κ°€ μžˆλŠ” μƒμ„±μž
3.
λ””ν΄νŠΈ μƒμ„±μž
아무 λ§€κ°œλ³€μˆ˜λ„ μ—†κ³  λ‚΄μš©λ„ μ—†λŠ” μƒμ„±μžμ΄λ‹€. ν΄λž˜μŠ€μ— κ΅¬ν˜„λœ μƒμ„±μžκ°€ μ—†λ‹€λ©΄ κΈ°λ³Έ μƒμ„±μžκ°€ μΆ”κ°€λ˜μ–΄ 컴파일 λœλ‹€.(λ§Œμ•½ μ ‘κ·Ό μ œν•œμžκ°€ public 인 경우 public μ œν•œμžκ°€ 뢙은 κΈ°λ³Έμƒμ„±μžκ°€ μΆ”κ°€λœλ‹€.)
Methods
(1) 클래슀의 μƒμ„±μžμ™€ 같은 이름을 κ°€μ§ˆ 수 μ—†λ‹€.
(2) λ©”μ„œλ“œλŠ” void λ₯Ό 포함해, λ°˜ν™˜ 값을 가지고 μžˆλ‹€.
(3) μžλ°” μ»΄νŒŒμΌλŸ¬λŠ” λ””ν΄νŠΈ λ©”μ„œλ“œλ₯Ό μ œκ³΅ν•˜μ§€ μ•ŠλŠ”λ‹€.
(4) 상속과 μž¬μ •μ˜ κΈ°λŠ₯은 non-static λ©”μ„œλ“œμ˜ νŠΉμ§•μ΄λ‹€.
(5) 객체(non-static λ©”μ„œλ“œμ˜ 경우), 클래슀 μ°Έμ‘°(static λ©”μ„œλ“œμ˜ 경우), λ©”μ„œλ“œ 이름을 μ΄μš©ν•˜μ—¬ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•  수 μžˆλ‹€.
(6) 객체의 μž‘λ™μ„ ꡬ체화 ν•˜κΈ° μœ„ν•΄ μ‚¬μš©λœλ‹€.