Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- after-throwing
- SpringBoot
- 생성자주입
- @ResponseBody
- 서비스레이어
- jointpoint
- @RequestMapping
- Model
- produces
- @
- 의존주입
- gradle
- 어노테이션
- 바인딩변수
- c:if
- 유효성검사
- springjdbc
- Java
- MVC
- PointCut
- spring
- AOP
- 스프링
- @RequestParam
- 비즈니스레이어
- .xml
- application.properties
- frontController
- @Valid
- springmvc
Archives
- Today
- Total
메모장
(접근)제어자 본문
728x90
반응형
(접근)제어자
일반적으로 public 이 디폴트 -> 단순 연산자로의 (외부로부터의) 접근을 막고싶다 ! -> private
private -> 해당 클래스 내부에서만 접근 가능 !
"모든 멤버변수에게는 private을 설정"
+ getter, setter을 함께 생성
↓
외부에서 직접적으로 멤버변수의 값에 접근할 수는 없으나,
getter, setter 를 인지하고 있으면 값을 바꿀수 있다!
"캡슐화"
코드 보기 ~~~!
package class01;
class Student{
private String name; // 나만 건드릴거야 private // 접근 제어자
private int score; // 외부에서 다이렉트 접근이 불가능해진다.
Student(String name, int score){
this.name = name;
this.score = score;
System.out.println(this.name + "학생 생성완료!");
}
@Override // 프로그램에 영향을 주는 주석
public String toString() {
return this.name + "학생은 " + this.score + "점입니다. ";
//화면에 뭐가 출력될지 여기다가 작성
}
// private된 멤버변수에 접근 가능하도록 하는 "메서드"를 추가로 생성
// private일때 변경하는 방법
// setter : 멤버변수에 값을 설정해줌
void setName(String name) {
this.name = name ;
}
void setScore(int score) {
this.score = score ;
}
// getter : 멤버변수에 값을 받아올수 있도록 설정해줌
String getName() {
return this.name;
}
int getScore() {
return score;
}
}
public class Test01 {
public static void main(String[] args) {
Student s1 = new Student("아리", 89);
// s1.name = "홍"; // 대입연산자(단순 연산자) 는 로그에 안남는다 .
s1.setScore(95); // '로그'에 기록됨
System.out.println(s1);
// [로그] ★★★★★★
// 일종의 히스토리 보드 // 기록 게시판
// 하나의 코드를 여러 개발자가 작업하게 되는데
// 하나의 코드로 여러 개발자들이 동시에 작업 하게 되는데
// : 프로젝트 완성시 기여도 측정
// 어떤사람이 어떤 코드를 건드렸는지에 대한 기록이 필요하다.
// 오류 발생시 어떤 액션 + 어떤 기능에서 발생했는지 기록
// => 메서드 위주로 기록됨
// => 단순 연산자는 할수 있지만 보통 안한다 xxxx
}
}
추상 클래스 (abstract class)
1. 객체화(인스턴스화) 불가능
생성자를 가질수있음에 유의!!!
new 연산자와 함께 사용 xxxxx
2. 추상메서드를 소유할수있음
=> 오버라이딩을 "강제"
=> 엄청 좋은거다 ! // 뭔가 기능을 강제하면 하라는 대로 하면 되서 좋다.
=> 메서드 바디{}를 가질수없음
//포켓몬 < 실습문제 >
// - 이름 String name
// - 이긴횟수 int win
// - 울음소리() hello() : 각각의 포켓몬 마다 고유한 소리를 출력
// - 게임() game() :상대 포켓몬과 대결을 해서 내가 이겼는지 졌는지 boolean game(Pokemon pokemon)
// -> pokemonA.game(pokemonB)
// -> pokemonA.game(pokemonB) // 나를 기준으로 내가 이기면 true, 내가 지면 false.
// 이긴횟수가 홀홀 작은값이 이김
// 이긴횟수가 짝짝 큰값이 이김
// 이긴횟수가 홀짝,짝홀 홀수가 이김
// 이긴횟수가 동일하다면 A가 이김(A > B)
- 게임() game() :상대 포켓몬과 대결을 해서 내가 이겼는지 졌는지 boolean game(Pokemon pokemon)
// -> pokemonA.game(pokemonB)
// -> pokemonA.game(pokemonB) // 나를 기준으로 내가 이기면 true, 내가 지면 false.
// 이긴횟수가 홀홀 작은값이 이김
// 이긴횟수가 짝짝 큰값이 이김
// 이긴횟수가 홀짝,짝홀 홀수가 이김
// 이긴횟수가 동일하다면 A가 이김(A > B)
이 메서드를 한글 코딩으로 작성해 보았다 !!
// 이긴횟수가 홀홀 작은값이 이김
if( 포켓몬 a의 win 홀수){
if(포켓몬 b의 win 홀수){
if(a < b) {
a 승리 !
} else {
a 패배 !
}
}
}
// 이긴횟수가 짝짝 큰값이 이김
else if( 포켓몬 a의 win 짝수){
if(포켓몬 b의 win 짝수){
if(a > b) {
a 승리 !
} else {
a 패배 !
}
}
}
// 이긴횟수가 동일하다면 A가 이김(A>B)
: win이 이긴횟수입니다!
else if( 포켓몬 a의 win == 포켓몬 b의 win ){
a 승리 !
} else {
a 패배 !
}
// 이긴횟수가 홀짝,짝홀 홀수가 이김
else if ( a 의 win 홀수 && b의 win 짝수){
a 승리
}else if(a의 win 짝수 && b의 win 홀수){
b 승리
}
이것을 또 코딩으로 바꾸기 !
boolean game(Pokemon pokemon) {
if (this.win == pokemon.win) {
System.out.println(this.name + " 승리 !");
return true;
// else로 실패를 안만든 이유: 위에서 승리에 안들어가면 다 실패가 뜨기 때문에
} else if (this.win % 2 == 1) {
if (pokemon.win % 2 == 1) {
if (this.win < pokemon.win) {
System.out.println(this.name + " 승리 !");
return true;
} else {
System.out.println(this.name + " 패배 !");
}
}
} else if (this.win % 2 == 0) {
if (pokemon.win % 2 == 0) {
if (this.win < pokemon.win) {
System.out.println(this.name + " 승리 !");
return true;
} else {
System.out.println(this.name + " 패배 !");
}
}
} else if (this.win % 2 == 1 && pokemon.win % 2 == 0) {
System.out.println(this.name + " 승리 !");
return true;
} else if (this.win % 2 == 0 && pokemon.win % 2 == 1) {
System.out.println(pokemon.name + " 승리 !");
return true;
}
return false;
}
전체 코드
출처 입력
package class01;
// class 생성시 고려사항
// 1. abstract 일까?
// -> 판단 방법
// 1) 이 클래스의 객체가 필요한가?
// 2) 추상메서드가 있나? ==> 오버라이딩을 강제해야 하는 메서드가 있나?
// == 재정의 해야하는 메서드가 있나 ? 울음소리 ()
//2. 멤버변수 생성
// 1) private //일단 달자
// 2) getter, setter //일단 달자
//3. 생성자 - new를 통해 못 만들 뿐이지 가질 순 있다 .
//4. 메서드
// 1)오버라이딩을 강제해야되나? => 추상메서드로 만들어야하나 ?
// 2) '기능'을 보고 , "INPUT,OUTPUT"을 생각하여 '메서드 시그니처' 작성
abstract class Pokemon {
private String name;
private int win;
Pokemon(String name) { // 추상클래스 생성자 소유 가능
this.name = name;
this.win = 0;
}
abstract void hello(); // 추상메서드는 {}가 없다. // 오버라이딩 강제
boolean game(Pokemon pokemon) {
if (this.win == pokemon.win) {
System.out.println(this.name + " 승리 !");
return true;
// else로 실패를 안만든 이유: 위에서 승리에 안들어가면 다 실패가 뜨기 때문에
} else if (this.win % 2 == 1) {
if (pokemon.win % 2 == 1) {
if (this.win < pokemon.win) {
System.out.println(this.name + " 승리 !");
return true;
} else {
System.out.println(this.name + " 패배 !");
}
}
} else if (this.win % 2 == 0) {
if (pokemon.win % 2 == 0) {
if (this.win < pokemon.win) {
System.out.println(this.name + " 승리 !");
return true;
} else {
System.out.println(this.name + " 패배 !");
}
}
} else if (this.win % 2 == 1 && pokemon.win % 2 == 0) {
System.out.println(this.name + " 승리 !");
return true;
} else if (this.win % 2 == 0 && pokemon.win % 2 == 1) {
System.out.println(pokemon.name + " 승리 !");
return true;
}
return false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWin() {
return win;
}
public void setWin(int win) {
this.win = win;
}
}
class Pika extends Pokemon {
// 1. 메서드 안만든거 있어~~ => 오버라이딩 강제되고있는 메서드(추상메서드)
// => 1. 나도 추상클래스 하던지
// => 2. 추상메서드 오버라이딩 해주던지 ◀
// 2. super()를 호출하는 중이야~~ => 부모클래스에게는 기본생성자가 현재 없음
// => 1. 부모클래스에 기본생성자 만들던지
// => 2. 부모클래스의 다른생성자를 사용하던지 ◀
Pika() {
this("피카츄");
}
Pika(String name) {
super("피카츄");
}
@Override
void hello() {
System.out.println("피카피카");
}
}
class Charmander extends Pokemon {
Charmander() {
this("파이리");
}
Charmander(String name) {
super("파이리");
}
@Override
void hello() {
System.out.println("파아~~");
}
}
public class Test03 {
public static void main(String[] args) {
// 추상클래스라 객체를 생성할수 없다
// Pokemon pokemonB = new Pokemon("포켓몬",0);
Pika pika = new Pika("피카츄");
Pika pika2 = new Pika("피카츄");
Charmander cm = new Charmander("파이리");
pika.hello();
cm.hello();
pika.game(pika2);
System.out.println(pika.getWin());
pika.game(cm);
System.out.println(pika.getWin());
pika2.game(cm);
System.out.println(pika2.getWin());
cm.game(pika);
System.out.println(cm.getWin());
cm.game(pika2);
System.out.println(cm.getWin());
}
}
728x90
반응형
'JAVA > 개념정리' 카테고리의 다른 글
| 컬렉션 프레임 워크 (0) | 2024.04.06 |
|---|---|
| 객체배열 (1) | 2024.04.06 |
| [상속] Object (0) | 2024.04.06 |
| 상속 (0) | 2024.04.06 |
| class 변수 , 공유자원 (0) | 2024.04.06 |