Java
[Java] μμ±μ
μ½λ©νλ λΆμ΄
2021. 2. 18. 04:33
λ°μν
βΆ μμ±μ
package constructor;
public class Person {
String name;
float height;
float weight;
}
package constructor;
public class PersonTest {
public static void main(String[] args) {
Person personLee = new Person(); // μμ±μ
}
}
-μμ±μμ μν μ μ£Όλ‘ μΈμ€ν΄μ€ λ³μμ μ΄κΈ°νλ€.
βΆ λν΄νΈ μμ±μ
package constructor;
public class Person {
String name;
float height;
float weight;
public Person() { // μλ° μ»΄νμΌλ¬κ° μλμΌλ‘ μ 곡νλ λν΄νΈ μμ±μ
}
}
-λ³λλ‘ μμ±μλ₯Ό μ μΈνμ§ μμ κ²½μ°, μλμΌλ‘ λν΄νΈ μμ±μλ₯Ό λ§λ€μ΄μ€λ€.
-λν΄νΈ μμ±μλ λ§€κ° λ³μλ μκ³ κ΅¬ν μ½λλ μλ€.
β
package constructor;
public class Person {
String name;
float height;
float weight;
public Person(String name) {
name = name;
}
}
package constructor;
public class PersonTest {
public static void main(String[] args) {
Person personLee = new Person(); // μμ±μ
}
}
-λν΄νΈ μμ±μ μμ λ§€κ° λ³μλ₯Ό μ λ ₯νλ©΄ μ λ κ² new Person() μ΄λΌλ κΈ°μ‘΄ μμ±μμμ μ€λ₯κ° λ°μνλ€.
λ°μν