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) κ°μ²΄μ μλμ ꡬ체ν νκΈ° μν΄ μ¬μ©λλ€.