Appearance
L6_05 面向对象
一、面向对象思想
1.1 面向对象的概念
面向对象编程(Object-Oriented Programming,OOP)是一种编程范式,将数据和操作数据的方法封装在一起。
1.2 面向对象的基本特征
- 封装:将数据和方法封装在类中
- 继承:一个类可以继承另一个类的属性和方法
- 多态:不同对象对同一消息做出不同响应
1.3 面向对象 vs 面向过程
| 特性 | 面向对象 | 面向过程 |
|---|---|---|
| 关注点 | 对象和交互 | 步骤和过程 |
| 代码组织 | 类和对象 | 函数和数据 |
| 可复用性 | 高 | 低 |
| 扩展性 | 好 | 差 |
二、类的创建和初始化
2.1 类的定义
cpp
class Person {
private:
string name;
int age;
public:
// 构造函数
Person() : name(""), age(0) {}
Person(string n, int a) : name(n), age(a) {}
// 析构函数
~Person() {}
// 成员方法
void setName(string n) { name = n; }
string getName() const { return name; }
void setAge(int a) { age = a; }
int getAge() const { return age; }
void introduce() const {
cout << "My name is " << name << ", I'm " << age << " years old." << endl;
}
};2.2 对象的创建和使用
cpp
int main() {
// 创建对象
Person p1;
Person p2("Alice", 25);
// 使用对象
p1.setName("Bob");
p1.setAge(30);
p1.introduce();
p2.introduce();
return 0;
}2.3 构造函数的类型
cpp
class Rectangle {
private:
int width;
int height;
public:
// 默认构造函数
Rectangle() : width(0), height(0) {}
// 参数化构造函数
Rectangle(int w, int h) : width(w), height(h) {}
// 拷贝构造函数
Rectangle(const Rectangle& other) {
width = other.width;
height = other.height;
}
// 移动构造函数
Rectangle(Rectangle&& other) noexcept {
width = other.width;
height = other.height;
other.width = 0;
other.height = 0;
}
int area() const { return width * height; }
};三、类的特性
3.1 封装
cpp
class BankAccount {
private:
// 私有成员,外部无法直接访问
double balance;
public:
BankAccount(double initial) : balance(initial) {}
// 通过公共方法访问私有成员
void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
bool withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return true;
}
return false;
}
double getBalance() const { return balance; }
};3.2 继承
cpp
// 基类
class Animal {
protected:
string name;
public:
Animal(string n) : name(n) {}
virtual void speak() const {
cout << name << " makes a sound." << endl;
}
};
// 派生类
class Dog : public Animal {
public:
Dog(string n) : Animal(n) {}
// 重写基类方法
void speak() const override {
cout << name << " barks: Woof! Woof!" << endl;
}
};
class Cat : public Animal {
public:
Cat(string n) : Animal(n) {}
void speak() const override {
cout << name << " meows: Meow! Meow!" << endl;
}
};3.3 多态
cpp
void makeSound(const Animal& animal) {
animal.speak(); // 根据实际类型调用相应方法
}
int main() {
Dog dog("Buddy");
Cat cat("Whiskers");
makeSound(dog); // 输出: Buddy barks: Woof! Woof!
makeSound(cat); // 输出: Whiskers meows: Meow! Meow!
return 0;
}四、访问控制
4.1 访问修饰符
| 修饰符 | 访问范围 |
|---|---|
public | 任何地方都可访问 |
protected | 类内部和派生类可访问 |
private | 仅类内部可访问 |
4.2 友元函数
cpp
class MyClass {
private:
int value;
public:
MyClass(int v) : value(v) {}
// 声明友元函数
friend void printValue(const MyClass& obj);
};
// 友元函数可以访问私有成员
void printValue(const MyClass& obj) {
cout << "Value: " << obj.value << endl;
}4.3 友元类
cpp
class FriendClass;
class MyClass {
private:
int secret;
// 声明友元类
friend class FriendClass;
public:
MyClass(int s) : secret(s) {}
};
class FriendClass {
public:
void accessSecret(MyClass& obj) {
// 友元类可以访问私有成员
cout << "Secret: " << obj.secret << endl;
}
};五、静态成员
5.1 静态成员变量
cpp
class Counter {
private:
static int count; // 静态成员变量
public:
Counter() { count++; }
~Counter() { count--; }
static int getCount() { return count; }
};
// 静态成员变量必须在类外定义
int Counter::count = 0;5.2 静态成员函数
cpp
class MathUtils {
public:
// 静态成员函数,不需要对象即可调用
static int add(int a, int b) { return a + b; }
static int multiply(int a, int b) { return a * b; }
};
// 使用静态成员函数
int sum = MathUtils::add(3, 5);
int product = MathUtils::multiply(4, 6);