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
- snowpack
- 미니게임
- 숫자
- java#왕초보
- degit
- git
- webpack
- Android
- 함수
- SQL
- 코틀린
- 왕초보
- kotlin
- FIle
- java
- 스프링
- 답글
- 쿠키
- 시큐어코딩
- 상속
- Spinner
- 오버라이드
- parcel
- 버전일치
- 게시판
- 안드로이드
- Spring
- sub query
- 스타일보험
- React
Archives
- Today
- Total
YSHUSH
OOP(Object Oriented Programing) 본문
구조, 관리
Object Oriented Programing
객체 지향
흐름 -> 절차지향
1. 은닉성(캡슐화) -> 요즘은 사라지는 추세
2. 상속성
3. 다형성
형식:
class 클래스명 {
variable(변수)
method(함수)
}
// class, array -> allocation(동적 할당) -> Heap 영역에 저장
MyClass cls = new MyClass();
// 자료형 클래스의 변수 == 객체, object,
// instance(주체)
cls.number = 1; // cls, number = stack에 넘어가고 MyClass는 heap영역에 들어감
cls.name = "홍길동";
cls.method();
MyClass cls1 = new MyClass();
cls1.number = 2;
cls1.name = "성춘향";
cls1.method();
// 절차지향
boolean[] isPowerOn = new boolean[2];
int[] channel = new int[2];
int[] volume = new int[2];
String[] maker = new String[2];
isPowerOn[0] = true;
channel[0] = 23;
volume[0] = 10;
maker[0] = "삼성";
isPowerOn[1] = false;
channel[1] = 50;
volume[1] = 0;
maker[1] = "LG";
for (int i = 0; i < channel.length; i++) {
System.out.println("Tv 회사는 " + maker[i] + "상태는" + isPowerOn[i] + "이고 채널: " + channel[i] + "번을 보고 있으며 볼륨:" + volume[i] + "입니다.");
}
/*
TV tv1 = new TV();
tv1.isPowerOn = true;
tv1.channel = 23;
tv1.volume = 10;
tv1.method();
TV tv2 = new TV();
tv2.isPowerOn = false;
tv2.channel = 50;
tv2.volume = 0;
tv2.method();
*/
TV tvArr[] = new TV[2]; // 객체 생성이 아님!!! 배열 생성이다.(변수만 생성한 것) == TV tv1, tv2, tv3;
for (int i = 0; i < tvArr.length; i++) {
tvArr[i] = new TV(); // 객체 생성
}
tvArr[0].isPowerOn = true;
tvArr[0].channel = 23;
tvArr[0].volume = 10;
tvArr[0].maker = "삼성";
tvArr[1].isPowerOn = false;
tvArr[1].channel = 50;
tvArr[1].volume = 0;
tvArr[1].maker = "LG";
for (int i = 0; i < tvArr.length; i++) {
tvArr[i].method();
}
}
}
class TV {
boolean isPowerOn;
int channel;
int volume;
String maker;
void method() {
System.out.println("Tv 회사는 " + maker + " 상태는 " + isPowerOn + "이고 채널: " + channel + "번을 보고 있으며 볼륨:" + volume + "입니다.");
}
}
class MyClass {
// 멤버변수
int number;
String name;
// 멤버메소드
void method() {
System.out.println("MyClass method()");
}
}
'Coding > Java' 카테고리의 다른 글
Class 예제 (0) | 2021.12.14 |
---|---|
Constructor (0) | 2021.12.14 |
File writer (0) | 2021.12.14 |
File reader (0) | 2021.12.14 |
File I/O (0) | 2021.12.14 |