3.3 EncapsulationÀÛ¼ºÀÚ : ÁøÀº¿µ ( 2004-07-21 )
¸ñÂ÷
3.3.1 Encapsulation À̶õĸ½¶È´Â °´Ã¼¸¦ ¸¸µé ¶§, °´Ã¼ ³»ºÎ¿¡ µé¾îÀÖ´Â º¯¼ö³ª ¸Þ¼ÒµåµîÀÇ Á¤º¸¸¦ °´Ã¼ ¹ÛÀ¸·Î °ø°³ÇÏÁö ¾Ê°í, µû·Î °ø°³ÇÏ´Â º¯¼ö³ª ¸Þ½îµå¸¦ ÅëÇؼ °´Ã¼¸¦ »ç¿ëÇÒ ¼ö ÀÖ°Ô ÇØÁÖ´Â °³³äÀ» ¸»ÇÑ´Ù. ĸ½¶È´Â ÇÁ·Î±×·¥À» º¹ÀâÇÏÁö¾Ê°Ô ¸¸µé ¼ö Àִµ¥ µµ¿òÀ» ÁÖ¸ç, ¼ÒÇÁÆ®¿þ¾î °³¹ßÀÚ¿¡°Ô ¸î°¡Áö ÀåÁ¡À» Á¦°øÇÑ´Ù.(½ÇÇàÀº ±×¸² À§¿¡¼ ¸¶¿ì½º ¿À¸¥ÂÊ ¹öÆ°À» Ŭ¸¯ÇÏ¿© Àç»ýÀ» ´©¸¥´Ù.)
À§ ±×¸²À» º¸¸é »ç¶÷ÀÌ µ¿»ç¹«¼Ò¿¡ µé¾î°¡ µîº» ÇÑÅëÀ» ¶¼·ÁÇÑ´Ù. ±×·¯³ª Á÷Á¢ Á¤º¸°¡ µé¾îÀÖ´Â °÷À¸·Î °¡¼ ÀÚ½ÅÀÌ µîº»À» ¶¿¼ö´Â ¾ø´Ù.
±× Á¤º¸´Â º¸È£µÇ°í Àֱ⠶§¹®ÀÌ´Ù. ¿øÇÏ´Â Á¤º¸¿¡ Á¢±ÙÇÏ·Á¸é Á¤º¸¿¡ Á¢±ÙÀÌ °¡´ÉÇÑ µ¿»ç¹«¼Ò Á÷¿ø¿¡°Ô ¾ê±âÇؼ µîº»À» ¶¼¾ß ÇÑ´Ù.
3.3.2 EncapsulationÇÏ·Á¸é
¸ðµç ¸â¹öº¯¼ö¿¡ ĸ½¶È¸¦ ÇÒ ÇÊ¿ä´Â ¾ø´Ù. Ãß»óÈ ÇÒ¶§ ÇÊ¿äÇÏ´Ù°í »ý°¢ÇÏ´Â ¸â¹öº¯¼ö¿¡¸¸ ĸ½¶È¸¦ ÇÏ¸é µÈ´Ù. class Car { //¸â¹öº¯¼ö ¼±¾ð String name ; String brand ; String color ; } À§¿Í °°Àº Ŭ·¡½º¿¡¼ name º¯¼ö´Â ¿ÜºÎ¿¡¼ Á÷Á¢ Á¢±ÙÇÏÁö ¸øÇÏ°Ô ¸·°í ½Í´Ù. ±×·¯³ª °ªÀ» °¡Á®°¥ ¼ö ÀÖ°Ô´Â ÁöÁ¤ÇÏ°í ½Í´Ù¸é
public String getName(){
return name ; }
brand¸â¹öº¯¼ö¿¡´Â °ªÀ» ´ëÀÔÇÏ°í ½Í´Ù¸é
public void setBrand( String b ){
brand = b ; }
3.3.3 ¿¹Á¦3_1 °Á¿¡¼ ´Ù·ð´ø Employees¿¡¼ ¸â¹öº¯¼ö¿¡ ´ëÇÑ Ä¸½¶È¸¦ ÁöÁ¤Çغ¸ÀÚ.public class Employees { //¸â¹öº¯¼ö ¼±¾ð private String empID ; private String name ; private String position ; private int salary ; //¸Þ¼Òµå ¼±¾ð public void print(){} public int calSalary(){} } public class Employees { //¸â¹öº¯¼ö ¼±¾ð private String empID ; private String name ; private String position ; private int salary ; //¸Þ¼Òµå ¼±¾ð //public void print(){} // public int calSalary(){} // set Method public void setEmpID( String id ){ empID = id ; } public void setName( String n ){ name = n ; } public void setPosition( String p ){ position = p ; } public void setSalary( int s ){ salary = s ; } //getMethod public String getEmpID(){ return empID ; } public String getName(){ return name ; } public String getPosition(){ return position ; } public int getSalary(){ return salary ; } } |