1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | package com.test; import java.util.Calendar; public class access { public static void main(String[] args) { //접근 지정자, Access Modifier // - 클래스 멤버(변스, 메소드)의 보안 제어(접근 권한) // - 캡슐화, 은닉화, 인터페이스 등., //1. public // - 클래스 멤버를 공개할 때 사용(100%) // - 어디서든 접근 가능 //2. private // - 클래스 멤버를 비공개할 때 사용(100%) // - 외부에서는 접근 불가능 //ex] item 인스턴스 만들기 Item item1 = new Item(); item1.a =10;//공개 //item1//.b 는 비공개 //ex] emloyee 인스턴스 만들기 // Employee e1 = new Employee(); // e1.name = "홍길동"; // e1.age = 30; // // Employee e2 = new Employee(); // e2.name = "오늘은 날씨가 참 좋네요~ 반갑습니다.~"; // e2.age = 100000000; -> public으로 열어놓으면 nono.. //->private로 Employee e3 = new Employee(); e3.test("홍길동길동길동홍길동홍길동"); e3.setAge(20); System.out.println(e3.getName()); //3번째 사람의 이름을 알고싶다? System.out.println(e3.getAge()); //ex) baby 인스턴스 Baby b1 = new Baby(); System.out.println(b1.getParent()); //b1.setParent("하하하"); b1.setName("호호호"); b1.setAge(3); System.out.println(b1.getAge()); System.out.println(b1.getBirthYear()); } } class Item { //영역, 교실의 개념 public int a; private int b; //외부에서는 볼 수 없다. 캡슐환 public void test() { System.out.println(a); System.out.println(b); } } class Baby { private String name; private int age; private String parent = "홍길동"; //getter/setter 활용 //1. 읽기 전용 멤버 public String getParent() { return this.parent; } //2. 쓰기 전용 멤버 public void setName(String name) { this.name = name; } //3. 읽기/쓰기 멤버 public void setAge(int age) { this.age = age; } public int getAge() { return this.age; } //4. 가공(계산), 가상 멤버 public int getBirthYear() { Calendar now = Calendar.getInstance(); return now.get(Calendar.YEAR) - age - 1; } } class Employee { //접근 지정자 사용시., //1. 무조건 멤버 변수는 private //2. 1번이 외부와 소통할 일이 있으면., setter/getter 생성 // -> 필요한 유효성 검사 + 가공 //3. 멤버 메소드는 private //4. 필요에 따라 일부 public //멤버 변수는 절대로 public을 사용하지 말것!!** -> private에., //public String name; //public int age; private String name; //파란색:멤버변수 private int age; private String address; //setter, getter의 역할 -> 인터페이스 //setter public void Setaddress(String address) { //클래스 멤버변수와 지역변수 충돌 -> 허용o, 해결?-> this붙은애가 멤버 변수가 됨 //this : 객체 지정(접근) 연산자 // -> 현재 this를 사용중인 메소드가 소속된 인스턴스를 가르키는 표현 this.address = address; //this. } //getter public String getAddress() { return this.address; } public void test(String str) { if (str.length() >= 2 && str.length() <= 5) this.name = str; } public String getName() { return name; } public void setAge(int n) { if (n >= 1 && n <= 120) age = n; } public int getAge() { return age; } } | cs |
'WEB > JAVA' 카테고리의 다른 글
STATIC(정적)2 (0) | 2015.05.22 |
---|---|
STATIC(정적)1 (0) | 2015.05.22 |
CLASS (0) | 2015.05.22 |
PACKAGE (0) | 2015.05.22 |
RANDOM(난수생성) (0) | 2015.05.22 |