Object oriented, Procedural Oridented
Procedural Oridented π Object oriented
- Procedural Oridented: μμ μ€μ, how, λ©μλ λΆλ¦¬
- Object oriented: κ°μ²΄, what, κ°μ²΄μμ λ©μλ ν¬ν¨
β example of procedural programming
example of Music Player
βοΈ music player class
1
2
3
4
public class MusicPlayerData {
int volume;
boolean isOn;
}
- βοΈ class with method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class MusicPlayerMain {
public static void main(String[] args) {
MusicPlayerData data = new MusicPlayerData();
turnOn(data);
raiseVolume(data);
}
static void turnOn(MusicPlayerData data){
data.isOn = true;
System.out.println("music player is on");
}
static void raiseVolume(MusicPlayerData data){
data.volume++;
System.out.println("volume: " + data.volume);
}
- more focused on the order (turn on, then raise volume)
- ππ» music player
field
andmethod
are in different classes - one in
music player class
and other inmain class
field
andmethod
of music player are very very relevent- can we find a way to put
field
andmethod
in one class?
β change into OOP
- βοΈ music player class
- has both
field
andmethod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class MusicPlayer {
int volume; //field
boolean isOn; //field
void turnOn(){ //method
isOn = true;
System.out.println("music player is on");
}
void raiseVolume(){ //method
volume++;
System.out.println("volume: " + volume);
}
}
- βοΈ main class
- only create instance, calls the method
1
2
3
4
5
6
7
8
public class MusicPlayerMain4 {
public static void main(String[] args) {
MusicPlayer player = new MusicPlayer();
player.turnOn();
player.raiseVolume();
}
}
more focused on how the music player works
- ππ»
field
andmethod
are in one class, easier for the programmer to understand - ππ» programming looks more like real life
- ππ» easier to change code
- only has to change
music player class
,main class
does not change
- only has to change
β Encapsulation
OOP
μμμ²λΌfield
andmethod
λ₯Ό ν ν΄λμ€μ μΊ‘μλ‘ κ°μΈλ―μ΄ λͺ¨μ κ²μμΊ‘μν
λΌκ³ νλ€.μΊ‘μν
:field
andmethod
λ₯Ό λ¬Άμ΄μ μΈλΆμ μ 곡νλ κ²
This post is licensed under CC BY 4.0 by the author.