티스토리 뷰
반응형
객체지향 프로그래밍(OOP, Object-Oriented Programming)은 객체라는 개념을 중심으로 소프트웨어를 설계하고 개발하는 방법입니다. 객체는 상태(속성)와 행동(메서드)을 가지는 독립적인 단위입니다. OOP는 코드의 재사용성, 유지보수성, 확장성을 높이는 데 큰 장점이 있음..
✅ OOP의 4대 핵심 개념
- 캡슐화 (Encapsulation)
- 객체 내부의 데이터(속성)를 외부에서 직접 접근하지 못하게 하고, 메서드를 통해서만 접근하도록 제한함
- → private, protected, getter/setter 등을 사용
- 상속 (Inheritance)
- 기존 클래스(부모 클래스)의 속성과 메서드를 새로운 클래스(자식 클래스)가 물려받는 것
- 코드 중복을 줄이고, 계층적 관계를 표현
- 다형성 (Polymorphism)
- 같은 인터페이스나 부모 클래스를 통해 다양한 방식으로 동작 가능
- 메서드 오버라이딩, 오버로딩
- 추상화 (Abstraction)
- 복잡한 내부 구현은 숨기고, 필요한 기능만을 인터페이스로 제공
- 인터페이스나 추상 클래스 활용
✅ 간단한 예시 (Python)
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError("Subclass must implement abstract method")
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
# 다형성 예시
animals = [Dog("Buddy"), Cat("Whiskers")]
for animal in animals:
print(animal.speak())
✅ OOP의 장점
- 코드 재사용성 (Inheritance)
- 유지보수 용이 (Encapsulation)
- 확장성 우수 (Polymorphism)
- 현실 세계와 유사한 구조로 설계 가능 (객체 모델링)
✅ PHP OOP 핵심 구성 예시
1. 클래스 및 객체 생성
<?php
class Animal {
public $name;
// 생성자
public function __construct($name) {
$this->name = $name;
}
public function speak() {
echo "$this->name makes a sound.";
}
}
// 객체 생성
$dog = new Animal("Buddy");
$dog->speak(); // 출력: Buddy makes a sound.
?>
2. 상속
<?php
class Dog extends Animal {
public function speak() {
echo "$this->name says Woof!";
}
}
$dog = new Dog("Max");
$dog->speak(); // 출력: Max says Woof!
?>
3. 캡슐화 (접근 제한자)
<?php
class User {
private $password;
public function setPassword($pwd) {
if (strlen($pwd) >= 6) {
$this->password = $pwd;
} else {
echo "비밀번호는 최소 6자 이상이어야 합니다.";
}
}
public function getPassword() {
return str_repeat("*", strlen($this->password));
}
}
$user = new User();
$user->setPassword("secure123");
echo $user->getPassword(); // 출력: *********
?>
4. 추상 클래스와 인터페이스
<?php
abstract class Animal {
public $name;
public function __construct($name) {
$this->name = $name;
}
abstract public function speak();
}
class Cat extends Animal {
public function speak() {
echo "$this->name says Meow!";
}
}
?>
✅ PHP OOP의 장점
- 코드 재사용: 상속 및 트레잇을 통해 기능 공유
- 코드 가독성 향상: 클래스로 기능을 구분
- 보안성 증가: 접근 제한자 사용으로 데이터 보호
- 테스트 용이: 의존성 주입과 클래스 단위 테스트 가능
반응형
'이것저것' 카테고리의 다른 글
wsl2 ubuntu + nginx + php8.3 + mysql8 설치 (0) | 2025.05.29 |
---|---|
ps 프로세스 관리 (0) | 2025.05.29 |
WSL2 설치 (1) | 2025.05.16 |
WSL 설치 (0) | 2025.05.15 |
종합소득세 ? (0) | 2025.05.02 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- WSL2
- laravel 12
- strict_types
- 비동기
- php
- array_combine
- ob_get_contents
- 설정
- laravel 11
- facades
- wsl
- createfromformat
- 설치
- uniqid
- curl_multi_init
- call_user_func
- swagger
- privatechannel
- eloquent
- Laravel
- jp:a
- PYTHON
- ubuntu
- laravel 테스트
- #collect
- 명령어
- mysql
- flask
- researcher
- reflectionclass
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함