ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA ์ž๋ฐ” ๊ฐ์ฒด, ํด๋ž˜์Šค, ๋ฉ”์†Œ๋“œ, ์ƒ์„ฑ์ž
    JAVA 2020. 8. 2. 21:15

    ๐ŸŽฏ ์ž๋ฐ” ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๊ฐœ๋…๊ณผ ํด๋ž˜์Šค, ๋ฉ”์†Œ๋“œ, ์ƒ์„ฑ์ž๋ฅผ ์•Œ์•„๋ณธ๋‹ค.

     

     


    ๐Ÿ”– ๋ชฉ์ฐจ

    ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
    ํด๋ž˜์Šค
    ๋ฉ”์†Œ๋“œ
    ์ƒ์„ฑ์ž


     

     

    ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

    `๊ฐ์ฒด`๋Š” ๋ชจ๋“  ์‚ฌ๋ฌผ์„ ์˜๋ฏธํ•œ๋‹ค. ์ง€๊ธˆ ๋‚ด ์•ž์— ์žˆ๋Š” ํ•ธ๋“œํฐ, ์ปดํ“จํ„ฐ, ์ฑ…, ์‚ฌ๋žŒ์ด ๋ชจ๋‘ ๊ฐ์ฒด์ด๋‹ค.

    ํ˜„์‹ค์„ธ๊ณ„์—์„œ ์ถ”์ถœํ•œ ๊ฐ์ฒด๋ฅผ ์ž๋ฐ”์™€ ๊ฐ™์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฐœ๋ฐœ๋ฐฉ๋ฒ•์„ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ด๋ผ๊ณ  ํ•œ๋‹ค. 

    ๋ถ„์„๋‹จ๊ณ„์˜ ๊ฐ์ฒด๋ชจ๋ธ๋ง

     

    ๊ฐ์ฒด๋ชจ๋ธ๋ง์„ ํ†ตํ•ด ํ˜„์‹ค์„ธ๊ณ„์—์„œ ์ถ”์ถœํ•œ ๊ฐ์ฒด๋ฅผ ์ž๋ฐ” ์–ธ์–ด๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ํด๋ž˜์Šค ํ˜•ํƒœ๋กœ ์„ค๊ณ„ํ•œ๋‹ค. ๊ฐ์ฒด์˜ ์†์„ฑ์€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋กœ ํ‘œํ˜„ํ•˜๊ณ , ๊ฐ์ฒด์˜ ๋™์ž‘์€ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋กœ ํ‘œํ˜„ํ•œ๋‹ค.

     

     

    ์„ค๊ณ„๋œ ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„๋‹จ๊ณ„์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ์ฒด ์ƒ์„ฑ์ด ํ•„์š”ํ•˜๋‹ค. ํด๋ž˜์Šค๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆฌ๋Š” ๊ฐ์ฒด์ƒ์„ฑ ์ž‘์—…์„ ์ธ์Šคํ„ด์Šคํ™”๋ผ๊ณ  ํ•˜๋ฉฐ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ„ ๊ฐ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šค๋ผ๊ณ  ํ•œ๋‹ค. ๊ฐ์ฒด์™€ ์ธ์Šคํ„ด์Šค๋Š” ๊ฐ™์€ ์šฉ์–ด๋‹ค.

     

     

    ํด๋ž˜์Šค

    ์ง€์ •์ž class ํด๋ž˜์Šค๋ช…{
      ์ธ์Šคํ„ด์Šค๋ณ€์ˆ˜ // ๋ฐ˜๋“œ์‹œ new๋กœ ์“ด๋‹ค
      ๋ฉ”์†Œ๋“œ
      ์ƒ์„ฑ์ž
    }

     

    ์ง€์ •์ž

    ํŠน์ • ๋ชฉ์ ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํ‚ค์›Œ๋“œ์ด๋ฉฐ ์ผ๋ฐ˜ ์ง€์ •์ž(static, final, abstract)์™€ ์ ‘๊ทผ ์ง€์ •์ž(public, private, default, protected)๋กœ ๋‚˜๋‰œ๋‹ค. ์ƒ๋žตํ•  ๊ฒฝ์šฐ default๊ฐ€ ๋˜๋ฉฐ ์ผ๋ฐ˜์ ์œผ๋กœ ํด๋ž˜์Šค๋Š” public ์ ‘๊ทผ์ง€์ •์ž๋ฅผ ๋ถ™์ธ๋‹ค. ์ผ๋ฐ˜ ์ง€์ •์ž์™€ ์ ‘๊ทผ ์ง€์ •์ž๋Š” ๊ฐ™์ด ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

     

    ํด๋ž˜์Šค๋ช…

    ์˜๋ฏธ์žˆ๊ณ  ๊ฐ„๋‹จํ•œ ๋ช…์‚ฌํ˜• ์ด๋ฆ„์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ฒซ๊ธ€์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ ์ง€์ •ํ•œ๋‹ค. Student Orange

     

    ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜

    ํด๋ž˜์Šค์˜ ๊ตฌ์„ฑ์š”์†Œ์ธ ์†์„ฑ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ์šฉ๋„์ด๋‹ค. ๋ณ€์ˆ˜๋ช…์€ ์˜๋ฏธ์žˆ๋Š” ๋ช…์‚ฌํ˜•์œผ๋กœ ์†Œ๋ฌธ์ž๋กœ ์ž‘์„ฑํ•œ๋‹ค. ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋Š” ๋กœ์ปฌ ๋ณ€์ˆ˜์™€ ๋‹ค๋ฅด๊ฒŒ ์ƒ์„ฑ ํ›„ ์ดˆ๊ธฐํ™”ํ•  ํ•„์š”์—†์ด ์ž๋™์œผ๋กœ ๋ฐ์ดํ„ฐํ˜•์˜ ๊ธฐ๋ณธ๊ฐ’์ด ์„ค์ •๋œ๋‹ค.

     

    ๋ฉ”์†Œ๋“œ

    ์ž๋ฐ”๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฉ”์†Œ๋“œ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ๋ฉ”์†Œ๋“œ๋Š” ๊ฐ์ฒด์˜ ๋™์ž‘๊ณผ ๊ธฐ๋Šฅ์„ ๋‹ด๋‹นํ•œ๋‹ค. ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ • ๋ฐ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  ๋ฐ˜๋“œ์‹œ ํด๋ž˜์Šค ๊ฐ์ฒด ์ƒ์„ฑ ํ›„ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.

     

    ๋ฉ”์†Œ๋“œ ์ƒ์„ฑ๊ณผ ์‚ฌ์šฉ

    public class method_test {
    
      public void a_Method() {
      
        System.out.println("a_Method()ํ˜ธ์ถœ_1");
        System.out.println("a_Method()ํ˜ธ์ถœ_2");
        System.out.println("a_Method()ํ˜ธ์ถœ_3");
        
      }// class ์„ ์–ธ. ๋ฉค๋ฒ„๋ฉ”์†Œ๋“œ(ํ•จ์ˆ˜) new ์‚ฌ์šฉ ์ƒ์„ฑ
        
        
      public static void main(String[] args) {
    
        System.out.println("main์‹œ์ž‘==========");
        method_test test = new method_test(); //๊ฐ์ฒด ์ƒ์„ฑ
        test.a_Method(); //ํ•จ์ˆ˜ํ˜ธ์ถœ
        System.out.println("main์ข…๋ฃŒ=========");
    
      }
    
    }

    ํด๋ž˜์Šค ๋‹จ์— public / ํ˜ธ์ถœํ•œ ๊ณณ์œผ๋กœ ๋Œ์•„๊ฐˆ ๊ฐ’์˜ ๋ฆฌํ„ดํƒ€์ž… / ํ•จ์ˆ˜์ด๋ฆ„(๋ฐ›์„ ์ธ์ž๊ฐ’)์„ ์ž‘์„ฑํ•ด ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ ๋‹ค. ์œ„์˜ ๋ฉ”์†Œ๋“œ ์ •์˜๋ฅผ ํ•ด์„ํ•ด๋ณด๋ฉด ์•„๋ฌด๋Œ€์„œ๋‚˜ ์ ‘๊ทผ๊ฐ€๋Šฅํ•œ/ ์ˆ˜ํ–‰๋งŒ ํ•˜๊ณ  ๋Œ๋ ค์ฃผ๋Š” ๊ฐ’์ด ์—†๋Š” / ํ•จ์ˆ˜์ด๋ฆ„() ์ด๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค. void๋Š” ๋ฆฌํ„ดํƒ€์ž…์ด๋ฉฐ ํ•จ์ˆ˜์— ๋Œ๋ ค์ฃผ๋Š” ๊ฐ’์ด ์—†์„ ๋•Œ ๋ช…์‹œํ•œ๋‹ค. ํ•จ์ˆ˜์— ๋Œ๋ ค์ค„ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—” ๋ฐ์ดํ„ฐํƒ€์ž…์„ ๋ช…์‹œํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํด๋ž˜์Šค ๋‹จ์— ์„ ์–ธ๋œ ๋ฉค๋ฒ„๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„  main๋ฉ”์†Œ๋“œ ์•ˆ์—์„œ ๊ฐ์ฒด์ƒ์„ฑ์„ ํ•ด์•ผํ•œ๋‹ค.

     

     

    ๋ชจ๋“ˆํ™”

    ํ•จ์ˆ˜๋Š” ํ•˜๋‚˜์˜ ๊ธฐ๋Šฅ๋งŒ ํ•˜๋‚˜์˜ ์ด๋ฆ„์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฒŒ ์ข‹๋‹ค. ๊ทธ๋ž˜์„œ ์•ž์œผ๋กœ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋‹ค ๋ถ„๋ฆฌํ•ด์„œ ํ•„์š”ํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ main์—์„œ ๋ถˆ๋Ÿฌ๋‹ค ์“ธ ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์„ ๋ชจ๋“ˆํ™”๋ผ๊ณ  ํ•œ๋‹ค. ์ฝ”๋“œ๊ฐ€ 1๋งŒ์ค„ ์žˆ์„ ๋•Œ ์ด๊ฑธ ์ „๋ถ€ ํ•˜๋‚˜์˜ ํŒŒ์ผ์— ๋ชฐ์•„๋„ฃ์œผ๋ฉด ์‚ฌ์šฉํ•˜๊ธฐ ๋ถˆํŽธํ•ด์ง„๋‹ค. ๋”ฐ๋ผ์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์€ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ์ชผ๊ฐœ๋Š” ๋ชจ๋“ˆํ™”๋ฅผ ํ†ตํ•ด ์ฝ”๋“œ๋ฅผ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ณ , ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋Š” ๊ณ„์† ์žฌํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜๋‚˜ํ•˜๋‚˜ ์ชผ๊ฐœ์ง„ ํŒŒ์ผ์„ ๋ชจ๋“ˆ์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

     

     

    ๋ชจ๋“ˆํ™”1. ํ•™์ƒ ๊ฐ์ฒด ์ƒ์„ฑ๊ณผ ์‚ฌ์šฉ

    public class Student {
    	String name;
    	int age;
    	String address;
    }

    ํ•™์ƒ๋“ค์„ ๋ชจ๋ธ๋งํ•ด์„œ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด๋ณธ๋‹ค. ํ•™์ƒ ๊ฐ์ฒด์˜ ์†์„ฑ์œผ๋กœ ์ด๋ฆ„, ๋‚˜์ด, ์ฃผ์†Œ๋ฅผ ์ถ”์ถœํ–ˆ๋‹ค. ์ด ์†์„ฑ ์ •๋ณด๋“ค์ด ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์ด๋‹ค. ์ด์ œ ์ด๋ ‡๊ฒŒ ์ •์˜๋œ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ์ฒด์ƒ์„ฑ์ด ํ•„์š”ํ•˜๋‹ค.

    public class TestStudent {
    
    	public static void main(String[] args) {
    
    		Student stu1 = new Student();
    		Student stu2 = new Student();
    		Student stu3 = new Student();
    
    		stu1.name = "ํ™๊ธธ๋™";
    		stu1.age = 20;
    		stu1.address = "์„œ์šธ";
    		
    		stu2.name = "์ด์ˆœ์‹ ";
    		stu2.age = 44;
    		stu2.address = "๋ถ€์‚ฐ";
    		
    		stu3.name = "์œ ๊ด€์ˆœ";
    		stu3.age = 19;
    		stu3.address = "๊ฒฝ๊ธฐ";
    	
    		System.out.println(stu1.name);
    		System.out.println(stu1.age);
    		System.out.println(stu1.address);
    		System.out.println(stu2.name);
    		System.out.println(stu2.age);
    		System.out.println(stu2.address);
    		System.out.println(stu3.name);
    		System.out.println(stu3.age);
    		System.out.println(stu3.address);
    	}
    
    }

    main๋ฉ”์†Œ๋“œ๋งŒ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ ๋‹ค. ๋จผ์ € ๋ฉ”์ธ ํ•จ์ˆ˜ ์•ˆ์— Student ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , Student ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์œ„์น˜๊ฐ’์„ ์ €์žฅํ•˜๋Š” ์ฐธ์กฐ๋ณ€์ˆ˜๋ช…์„ stu1๋กœ ์ง€์–ด์ค€๋‹ค. ์ด ์ฐธ์กฐ๋ณ€์ˆ˜ ๋’ค์— .(dot) ์ ์„ ์ฐ์œผ๋ฉด ์ €์ชฝ ํด๋ž˜์Šค์—์„œ ๋‚ด๊ฐ€ ์ฐธ์กฐํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ธ์Šคํ„ด์Šค๋ณ€์ˆ˜์™€ ๋ฉ”์†Œ๋“œ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. ๊ทธ ๋‹ค์Œ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•ด ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ณ  ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด๋ณธ๋‹ค.

     

     

    private ์ ‘๊ทผ์ œ์–ด์ž

    public์œผ๋กœ ์ •์˜๋œ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋Š” ์™ธ๋ถ€์—์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•œ ๋ฐ˜๋ฉด, private์€ ์™ธ๋ถ€์—์„œ ํด๋ž˜์Šค ๋‚ด๋ถ€์˜ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋‚˜ ๋ฉ”์†Œ๋“œ์— ์ ‘๊ทผํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ๊ฐ์ถฐ์ ธ ์žˆ๋Š” ์ƒํƒœ๋กœ ํด๋ž˜์Šค ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์™ธ๋ถ€์—์„œ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ด ๋ฉค๋ฒ„๋ณ€์ˆ˜๋“ค์— ๋Œ€ํ•œ public ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค.

    public class Student {
    	
    	private String name;
    	private int age;
    	private String address;
    
    	public void setName(String name) {
    		this.name = name;
    	}//๋ฉค๋ฒ„๋ณ€์ˆ˜ name ๊ฐ’ ์„ค์ •ํ•จ์ˆ˜
    	
    	public String getName() {
    		return name;
    	}//๋ฉค๋ฒ„๋ณ€์ˆ˜ name๊ฐ’์„ ํ•„์š”ํ•œ ๊ณณ์œผ๋กœ ๋ณด๋‚ด์ฃผ๋Š” ํ•จ์ˆ˜
    
    }

    ๋ฉ”์†Œ๋“œ๋Š” ๋ฐ˜๋“œ์‹œ main์—์„œ ํ˜ธ์ถœ(caller method)๋˜์–ด์•ผ ์ˆ˜ํ–‰(worker method)๋˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ์ˆ˜ํ–‰์—๋Š” ๋‘๊ฐ€์ง€ ์—ญํ• ์ด ์žˆ๋‹ค.

    set๋ณ€์ˆ˜๋ช…() set๋ฉ”์†Œ๋“œ๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•  ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.  caller๋ฉ”์†Œ๋“œ๊ฐ€ ์ธ์ž์— ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„์„œ ์ „๋‹ฌํ•˜๋ฉด set๋ฉ”์†Œ๋“œ๊ฐ€ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๊ณ  ํ•จ์ˆ˜์‹คํ–‰์ด ๋๋‚œ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์‹œ caller๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์—†์œผ๋ฏ€๋กœ return ํ‚ค์›Œ๋“œ๋Š” ์ƒ๋žต๋˜์—ˆ์œผ๋ฉฐ ์ด ๊ฒฝ์šฐ์—” ๋ฉ”์†Œ๋“œ์˜ ๋ฆฌํ„ดํƒ€์ž…์„ void๋กœ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค.

    get๋ณ€์ˆ˜๋ช…() get๋ฉ”์†Œ๋“œ๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. caller๋ฉ”์†Œ๋“œ์—์„œ ์ „๋‹ฌ๋ฐ›๋Š” ๊ฐ’์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์“ธ ํ•„์š” ์—†๋‹ค. ๋ฐ์ดํ„ฐ ์กฐํšŒ ํ›„ ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด return ๋ณ€์ˆ˜๊ฐ’์„ ๋ช…์‹œํ•˜๊ณ , ๋ฉ”์†Œ๋“œ ๋ฆฌํ„ดํƒ€์ž…์€ ๋ฐ˜ํ™˜๊ฐ’์˜ ๋ฐ์ดํ„ฐํƒ€์ž…์œผ๋กœ ์ง€์ •ํ•œ๋‹ค. ๋ณ€์ˆ˜๋ช…์˜ ์ฒซ๊ธ€์ž๋Š” camel ํ‘œ๊ธฐ ๊ทœ์น™์— ๋”ฐ๋ผ ๋Œ€๋ฌธ์ž๋กœ ์ž‘์„ฑํ•œ๋‹ค. setName getAge

    ์ž์‹ ์˜ ํด๋ž˜์Šค์— ์žˆ๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋• this ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. this.name์€ Student์˜ ์ธ์Šคํ„ด์Šค๋ณ€์ˆ˜์ธ name์„ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ this.name=name;์€ ๊ทธ๊ฒƒ์„ set๋ฉ”์†Œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์€ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•œ๋‹ค๋Š” ์˜๋ฏธ๋‹ค.

     

    public class Student {
    	private String name;
    	private int age;
    	private String address;
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAddress(String address) {
    		this.address = address;
    	}
    
    	public String getAddress() {
    		return address;
    	}
    }
    public class TestStudent {
    
    	public static void main(String[] args) {
    		
    		Student stu1 = new Student();
    		stu1.setName("ํ™๊ธธ๋™");
    		stu1.setAge(20);
    		stu1.setAddress("์„œ์šธ");
    		System.out.println(stu1.getName());
    		System.out.println(stu1.getAge());
    		System.out.println(stu1.getAddress());
            
    		}
    
    }

    Student ํด๋ž˜์Šค์— age, address ๋ฉค๋ฒ„๋ณ€์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” get, set ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€๋กœ ๋งŒ๋“ค๊ณ  TestStudent ํด๋ž˜์Šค์˜ main๋ฉ”์†Œ๋“œ์— ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. Student ํด๋ž˜์Šค๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ณ€์ˆ˜ stu1์˜ setter ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋ฐ์ดํ„ฐ๊ฐ’์„ ์ ์–ด์„œ ์ €์ชฝ์œผ๋กœ ์ „๋‹ฌํ•œ๋‹ค. ์ „๋‹ฌํ•ด์„œ ๋ณด๋‚ผ ๋ฟ ๋ฆฌํ„ด๋ฐ›๋Š” ๊ฐ’์€ ์—†๋‹ค. ๊ทธ๋ฆฌ๊ณ  set์œผ๋กœ ์ €์žฅํ•œ ๊ฐ’์„ ํ”„๋ฆฐํŠธํ•˜๊ธฐ ์œ„ํ•ด getter ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์ €์žฅ๋œ ๊ฐ’์„ ๋ฆฌํ„ด ๋ฐ›๊ณ  ๋ชจ๋“  ์‹คํ–‰์ด ์ข…๋ฃŒ๋œ๋‹ค.

     

     

    ๋ชจ๋“ˆํ™”2. Book ๊ฐ์ฒด ์ƒ์„ฑ๊ณผ ์‚ฌ์šฉ

    public class Book {
    	private String name;
    	private int price;
    	
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	
    	public void setPrice(int price) {
    		this.price = price;
    	}
    	
    	public int getPrice() {
    		return price;
    	}
    }

    private ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ name๊ณผ price๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  private์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก setter์™€ getter ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ ๋‹ค.

    public class TestBook {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Book book1 = new Book();
    		Book book2 = new Book();
    		book1.setName("1984");
    		book1.setPrice(8000);
    		book2.setName("ํ™๊ธธ๋™์ „");
    		book2.setPrice(4000);
    		System.out.println(book1.getName());
    		System.out.println(book1.getPrice());
    		System.out.println(book2.getName());
    		System.out.println(book2.getPrice());
    	}
    
    }

    ๋ฉ”์ธํ•จ์ˆ˜ ์•ˆ์— Book ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  set ํ˜ธ์ถœ ๋ฉ”์†Œ๋“œ๋กœ ๋ฐ์ดํ„ฐ๊ฐ’์„ ์ „์†ก, get ํ˜ธ์ถœ ๋ฉ”์†Œ๋“œ๋กœ ์ €์žฅํ•œ ๋ฐ์ดํ„ฐ๊ฐ’์„ ๋ฐ›๋Š”๋‹ค.

     

     

    getter and setter ์ž๋™์ž…๋ ฅ ํˆด

     

    ์ฝ”๋“œ์—์„œ ์˜ค๋ฅธ์ชฝ ๋งˆ์šฐ์Šค ํด๋ฆญ - Source - Generate Getters and Setters

     

    ์ ‘๊ทผํ•  ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์„ ํƒํ•œ๋‹ค.

     

    get set ๋ฉ”์†Œ๋“œ๋ฅผ ์ผ์ผ์ด ๋งŒ๋“ค๊ธฐ ๊ท€์ฐฎ์„ ๋•Œ ์ดํด๋ฆฝ์Šค ๊ฐœ๋ฐœํˆด์„ ์‚ฌ์šฉํ•˜๋ฉด ์ž๋™์œผ๋กœ ๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค.

     

     

    ์ƒ์„ฑ์ž

    ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์— ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜์Œ์œผ๋กœ ์ €์žฅํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ๋งˆ๋‹ค ์ดˆ๊ธฐ๊ฐ’์„ ๋‚ด๊ฐ€ ์›ํ•˜๋Š”๋Œ€๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฉ”์„œ๋“œ์ฒ˜๋Ÿผ ํ˜ธ์ถœ ํ›„์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋ฆฌํ„ดํƒ€์ž…์ด ์—†๊ณ  ํด๋ž˜์Šค๋ช…์œผ๋กœ ์ด๋ฆ„์„ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค. ์ƒ์„ฑ์ž๋ฅผ ์•„๋ฌด๊ฒƒ๋„ ๋งŒ๋“ค์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋ณด์ด์ง„ ์•Š์ง€๋งŒ ์ž๋™์ ์œผ๋กœ default ๊ธฐ๋ณธ์ƒ์„ฑ์ž๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. ํ•˜์ง€๋งŒ ์ƒ์„ฑ์ž๋ฅผ ํ•˜๋‚˜๋ผ๋„ ๋งŒ๋“ค์—ˆ์„ ๋• ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋Š” ์ž๋™์œผ๋กœ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.

    public class Student {
    	private String name;
    	private int age;
    	private String address;
    
    	public Student() {  //์ ‘๊ทผ์ œํ•œ์ž, ๋ฆฌํ„ดํƒ€์ž… ์—†์Œ, ํด๋ž˜์Šค์ด๋ฆ„()
    		System.out.println("Student ์ƒ์„ฑ์ž ํ˜ธ์ถœ");
    		name="ํ™๊ธธ๋™";
    		age=10;
    		address="๊ฐ•๋‚จ๊ตฌ";
    	}
    }

     

     

    ๊ฐ์ฒด ์ƒ์„ฑ

    ํด๋ž˜์Šคํ˜• ๋ณ€์ˆ˜๋ช… = new ํด๋ž˜์Šคํ˜•(ํŒŒ๋ผ๋ฏธํ„ฐ);

    ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“  ํ›„ ๋ฐ˜๋“œ์‹œ ๊ฐ์ฒด์ƒ์„ฑ์„ ํ•ด์•ผ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ์ฒด์ƒ์„ฑ์€ new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ž‘์—…์ด๋‹ค. ์—ฌ๊ธฐ์„œ new ๋’ท๋ถ€๋ถ„์ด ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค. ํŒŒ๋ผ๋ฏธํ„ฐ์— ๊ฐ’์„ ๋„ฃ์„ ๊ฒฝ์šฐ ์ƒ์„ฑ์ž๋กœ ๋„˜์–ด๊ฐ€์„œ ์ดˆ๊ธฐ๊ฐ’์„ ๋ฐ”๊ฟ”์ค€๋‹ค. ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„ฃ์ง€ ์•Š์„ ๊ฒฝ์šฐ์—” ๊ธฐ๋ณธ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋Š” ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ์ƒ์„ฑ์ž ํ˜ธ์ถœ ํ›„ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์น˜๊ฐ’์„ ์ฐธ์กฐ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค. ์ด ์ฐธ์กฐ๋ณ€์ˆ˜์— .(dot)์„ ๋ถ™์—ฌ์„œ ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

    	public Student() {} // ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๊ผญ ๊ธฐ์žฌํ• ๊ฒƒ
    
    	public Student(String name, int age, String address) {
    		System.out.println("Student ์ƒ์„ฑ์ž ํ˜ธ์ถœ");
    		this.name = name;
    		this.age = age;
    		this.address = address;	
    	}

    ํด๋ž˜์Šค์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค๊ณ  main ํ•จ์ˆ˜ ์•ˆ์—์„œ ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ ์ธ์ž๋ฅผ ์ ์–ด์ฃผ์ง€ ์•Š์€ ๋””ํดํŠธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ƒ์„ฑ์ž์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ ์œผ๋ฉด ์ž๋™ ๋””ํดํŠธ ์ƒ์„ฑ์ž๋Š” ์‚ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋”ฐ๋ผ์„œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๊ธฐ๋ณธ์ƒ์„ฑ์ž ๋˜ํ•œ ๋”ฐ๋กœ ๋ช…์‹œํ•ด์•ผ ํ•œ๋‹ค.

     

    public class Constructor {
    
    	private String name;
    	private int age;
    	private String address;
    
    	public Constructor() {
    	}
    
    	public Constructor(String name, int age, String address) {
    		System.out.println("Constructor ์ƒ์„ฑ์ž ํ˜ธ์ถœ");
    		this.name = name;
    		this.age = age;
    		this.address = address;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public String getAddress() {
    		return address;
    	}
    
    	public void setAddress(String address) {
    		this.address = address;
    	}
    
    }
    public class Constructor_Test {
    
    	public static void main(String[] args) {
    		
    		Constructor cons = new Constructor("ํ™๊ธธ๋™", 20, "๊ฐ•๋‚จ๊ตฌ");
    		Constructor cons2 = new Constructor();
            
    		System.out.println(cons.getName());
    		System.out.println(cons.getAge());
    		System.out.println(cons.getAddress());
            
    		System.out.println(cons2.getName());
    		System.out.println(cons2.getAge());
    		System.out.println(cons2.getAddress());
    	}
    
    }

    ์ธ์ž๋ฅผ ๊ฐ€์ง„ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•œ cons๋Š” (ํ™๊ธธ๋™, 100, ๊ฐ•๋‚จ๊ตฌ)๋ฅผ ์ถœ๋ ฅํ–ˆ์œผ๋ฉฐ, ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•œ cons2๋Š” ๊ฐ ๋ฐ์ดํ„ฐํƒ€์ž…์˜ ๊ธฐ๋ณธ๊ฐ’์ธ (null, 0, null)์„ ์ถœ๋ ฅํ–ˆ๋‹ค.

     

     

    ๋ชจ๋“ˆํ™”3. Student์™€ Bookํด๋ž˜์Šค 4๊ฐœ์˜ ๋ชจ๋“ˆ ์—ฐ๊ฒฐ

    public class Book {
    	private String title;
    	private int price;
    
    	public Book() {}
    
    	public void setTitle(String title) {
    		this.title = title;
    	}
    
    	public String getTitle() {
    		return title;
    	}
    
    	public void setPrice(int price) {
    		this.price = price;
    	}
    
    	public int getPrice() {
    		return price;
    	}
    }
    public class BookTest {
    
    	public static void main(String[] args) {
        
    		Book book1 = new Book();
    		Book book2 = new Book();
    		book1.setTitle("1984");
    		book1.setPrice(1000);
    		book2.setTitle("์˜ค๋งŒ๊ณผ ํŽธ๊ฒฌ");
    		book2.setPrice(2000);
    		System.out.println(book1.getTitle());
    		System.out.println(book1.getPrice());
    		System.out.println(book2.getTitle());
    		System.out.println(book2.getPrice());
    	}
    
    }

    Book ๊ฐ์ฒด๋Š” ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋กœ ์ƒ์„ฑํ•˜๊ณ  setter์™€ getter ๋ฉ”์†Œ๋“œ๋กœ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋ฅผ ํ”„๋ฆฐํŠธํ•œ๋‹ค.

    public class Student {
    	private String name;
    	private int age;
    	private String address;
    	private Book writtenBook;  // Book ํด๋ž˜์Šค ์ถ”๊ฐ€
    	
    	public Student() {}
    	public Student(String name, int age, String address) {
    		this.name = name;
    		this.age = age;
    		this.address = address;
    	}
    	
    	public void setWrittenBook(Book writtenBook) {
    		this.writtenBook = writtenBook;
    	}
    	
    	public Book getWrittenBook() {
    		return writtenBook;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	public String getAddress() {
    		return address;
    	}
    	public void setAddress(String address) {
    		this.address = address;
    	}
    }
    
    public class StudentTest {
    
    	public static void main(String[] args) {
    
    		Student stu1 = new Student("ํ™๊ธธ๋™", 100, "๊ฐ•๋‚จ๊ตฌ");
    		Book book1 = new Book();
    		book1.setTitle("ํ•ด๋ฆฌํฌํ„ฐ");
    		book1.setPrice(3000);
    		stu1.setWrittenBook(book1);
    
    		System.out.println(stu1.getName());
    		System.out.println(stu1.getAge());
    		System.out.println(stu1.getAddress());
       	        System.out.println(book1.getTitle());
    		System.out.println(book1.getPrice());
    
    	}
    
    }

    StudentTest ๋ฉ”์ธ๋ฉ”์†Œ๋“œ์— Book ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ฐธ์กฐ๋ณ€์ˆ˜ book1์— setํ•จ์ˆ˜์™€ getํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์ธ์ž์— title๊ณผ price ๊ฐ’์„ ์ง€์ •ํ•ด์„œ Book ํด๋ž˜์Šค๋กœ ๋ณด๋‚ธ๋‹ค. Student ํด๋ž˜์Šค์— ์žˆ๋Š” ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋กœ ์ƒ์„ฑ๋œ writtenBook ๋ฉค๋ฒ„๋ณ€์ˆ˜์—๋Š” book1์— ์ €์žฅํ•œ ๊ฐ’์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ฐธ์กฐ๋ณ€์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ณด๋‚ธ๋‹ค.

    stu1์˜ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋Š” ์‰ฝ๊ฒŒ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ stu1.getWrittenBook์€ ์ฃผ์†Œ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. book1์„ set ์ธ์ž๋กœ ๋ณด๋ƒˆ์œผ๋‹ˆ๊นŒ Book ํด๋ž˜์Šค๋ฅผ ์ฐธ์กฐํ•ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ book1์˜ get ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์œ„์—์„œ ์ง€์ •ํ•œ (ํ•ด๋ฆฌํฌํ„ฐ, 3000) ๊ฐ’์„ ํ”„๋ฆฐํŠธํ•  ์ˆ˜ ์žˆ๋‹ค.

     

     

     

    ๐Ÿ˜œ NEXT : ์ž๋ฐ” ํด๋ž˜์Šค ๋ชจ๋“ˆํ™” ์—ฐ์Šตํ•˜๊ธฐ

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

๊ฐœ๋ฐœ๊ณต๋ถ€