๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java] ์ƒ์„ฑ์ž

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 18.
๋ฐ˜์‘ํ˜•

โ–ถ ์ƒ์„ฑ์ž

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() ์ด๋ผ๋Š” ๊ธฐ์กด ์ƒ์„ฑ์ž์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€