๋ฐ์ํ
โถ ์์ฑ์
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() ์ด๋ผ๋ ๊ธฐ์กด ์์ฑ์์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
๋ฐ์ํ
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ์ฐธ์กฐ ์๋ฃํ (0) | 2021.02.18 |
---|---|
[Java] ์์ฑ์ ์ค๋ฒ๋ก๋ (0) | 2021.02.18 |
[Java] ํด๋์ค+์ธ์คํด์ค ๋ง๋ค๊ธฐ ์ค์ต (0) | 2021.02.18 |
[Java] ์ฐธ์กฐ ๋ณ์์ ์ฐธ์กฐ ๊ฐ (0) | 2021.02.18 |
[Java] ์ธ์คํด์ค ์ฌ๋ฌ ๊ฐ ์์ฑํ๊ธฐ (0) | 2021.02.18 |
๋๊ธ