Contents
<중간 점검>상태에 Private 붙이는 이유 → 상태에 직접 접근하지 못하게 하기 위해
메서드에 Public 붙이는 이유 → 상태를 반드시 행위로 변경하기 위해
DeskLamp 클래스 작성하고 객체 생성해보기
1.
package ex04;
class DeskLamp {
private boolean isOn; // 컨벤션(약속), 변수의 선언 private은 상태가 행위를 통해 변경되는 것을 강제한다
public void turnOn() {
isOn = true;
}
public void turnOff() {
isOn = false;
}
public String toString() {
return "현재 상태는 " + (isOn == true ? "켜짐" : "꺼짐");
}
}
public class DeskLampTest {
public static void main(String[] args) {
DeskLamp myLamp = new DeskLamp(); // 인스턴스, 객체 (Object)
myLamp.turnOn();
System.out.println(myLamp);
myLamp.turnOff();
System.out.println(myLamp);
}
}
C:\workspace\tools\jdk-21\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\lib\idea_rt.jar=51741:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\workspace\java_lec\study\out\production\study ex04.DeskLampTest
현재 상태는 켜짐
현재 상태는 꺼짐
Process finished with exit code 0
2.
package ex04;
class DeskLamp {
private boolean isOn; // 컨벤션(약속), 변수의 선언 private은 상태가 행위를 통해 변경되는 것을 강제한다
public boolean getIsOn() { // 상태 확인 - 컨벤션 (get으로 시작한다)-카멜표기법(낙타표기법-소문자로 시작, 띄어쓰기 대문자)
return isOn;
}
public void turnOn() { // 행위 -- 상태변경 (이름은 의미있게)
isOn = true;
}
public void turnOff() { // 행위 -- 상태변경
isOn = false;
}
}
public class DeskLampTest {
public static void main(String[] args) {
DeskLamp myLamp = new DeskLamp(); // 인스턴스, 객체 (Object)
myLamp.turnOn();
System.out.println(myLamp.getIsOn());
myLamp.turnOff();
System.out.println(myLamp.getIsOn());
}
}
C:\workspace\tools\jdk-21\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\lib\idea_rt.jar=51946:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\workspace\java_lec\study\out\production\study ex04.RadioTest
true
false
Process finished with exit code 0
Radio 클래스 정의하고 객체 생성하기
package ex04;
class Radio {
// 상태 : isPress
private boolean isPress;
// 행위: 책임을 생각하고 만든다.
// 행위는 하나의 책임만 가지는 게 좋다 (관리하기 쉬움)
// 하나의 책임만 가질 수 없으면 최대한 적게 !
// 책임1. 켜다
public void turnOn() {
isPress = true;
}
// 책임2. 끄다
public void turnOff() {
isPress = false;
}
// 상태확인
public boolean getIsPress() {
return isPress;
}
}
public class RadioTest {
public static void main(String[] args) {
Radio myRadio = new Radio();
myRadio.turnOn();
System.out.println(myRadio.getIsPress());
myRadio.turnOff();
System.out.println(myRadio.getIsPress());
}
}
C:\workspace\tools\jdk-21\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\lib\idea_rt.jar=51980:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\workspace\java_lec\study\out\production\study ex04.RadioTest
true
false
Process finished with exit code 0
Box 클래스 정의하고 객체 생성하기
package ex04;
class Box {
int width;
int length;
int height;
double getVoume() {
return (double) width * height * length;
}
}
public class BoxTest {
public static void main(String[] args) {
Box b;
b = new Box();
b.width = 20;
b.length = 20;
b.height = 30;
System.out.println("상자의 가로, 세로, 높이는 " + b.width + "," + b.length + "," + b.height + "입니다.");
System.out.println("상자의 부피는 " + b.getVoume() + "입니다.");
}
}
C:\workspace\tools\jdk-21\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\lib\idea_rt.jar=52602:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\workspace\java_lec\study\out\production\study ex04.BoxTest
상자의 가로, 세로, 높이는 20,20,30입니다.
상자의 부피는 12000.0입니다.
Process finished with exit code 0
Television 클래스 정의하고 객체 생성하기
package ex04;
public class Television01 {
int channel;
int volume;
boolean onOff;
public static void main(String[] args) {
Television01 myTv = new Television01();
myTv.channel = 7;
myTv.volume = 10;
myTv.onOff = true;
Television01 yourTv = new Television01();
yourTv.channel = 9;
yourTv.volume = 12;
yourTv.onOff = true;
System.out.println("나의 텔레비전의 채널은 " + myTv.channel + "이고 볼륨은 " + myTv.volume + "입니다.");
System.out.println("너의 텔레비전의 채널은 " + yourTv.channel + "이고 볼륨은 " + yourTv.volume + "입니다.");
}
}
C:\workspace\tools\jdk-21\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\lib\idea_rt.jar=52739:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\workspace\java_lec\study\out\production\study ex04.Television01
나의 텔레비전의 채널은 7이고 볼륨은 10입니다.
너의 텔레비전의 채널은 9이고 볼륨은 12입니다.
Process finished with exit code 0
<중간 점검>
1. 객체를 생성시키는 연산자는 무엇인가?
→
2. 클래스와 객체와의 관계는 무엇이라고 할 수 있는가?
→
3. 각 객체가 가지고 있는 데이터는 모두 동일한가? 아니면 객체마다 다른가?
→
4. 자바에서 객체를 참조하려면 변수를 어떻게 선언하여야 하는가?
→
5. (Circle odj; / odg.radius = 100; / obj.color = “blue”;) 다음의 코드에서 잘못된 부분은 어디인가?
→
Share article