面向对象的基本特性:封装,继承,多态
1.0 面向过程概念
当我们在编写程序时,通常采用以下步骤:
2.0 程序案例
#define _CRT_SECURE_NO_WARNINGS
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
struct student
{
int id;
char name[20];
int gender;
int mark;
};
int MakeStudentId(int year, int classNum, int serialNum)
{
// 创建一个char类型的数组
char buffer[20];
// 将三个变量转换为指定格式的字符串,存储在buffer数组中
sprintf(buffer, "%d%d%d", year, classNum, serialNum);
// 将字符串转换为整数
int id = atoi(buffer);
// 返回项目的id值
return id;
}
const char* NumGenderToStrGender(int numGender)
{
if (numGender == 0)
{
return "女";
}
else if (numGender == 1)
{
return "男";
}
return "NULL";
}
int StrGenderToNumGender(const char* strGender)
{
int numGender;
if (strcmp("男", strGender) == 0)
{
numGender = 1;
}
else if (strcmp("女", strGender) == 0)
{
numGender = 0;
}
else
{
numGender = -1;
}
return numGender;
}
int main()
{
// 创建结构体变量
struct student stu;
stu.id = MakeStudentId(2024, 123, 26);
strcpy(stu.name, "小明");
stu.gender = StrGenderToNumGender("男");
stu.mark = 98;
printf("学号:%d\n", stu.id);
printf("姓名: %s\n", stu.name);
const char* gender = NumGenderToStrGender(stu.gender);
printf("性别:%s\n", gender);
printf("分数:%s\n", stu.mark);
return 0;
}
3.0 面向对象
成员id的表示方式:
1. 将函数的第一个参数设置为 struct student * ,让函数直接操作 student 结构体。
4.0 封装特性
typedef struct
{
int id;
char name[20];
int gender;
int mark;
}StudentInfo_t;
注:将需要的信息封装为一个结构体内部包含学生的姓名,学号,性别,分数。
通过函数设置学生的id编号,函数可以通过结构体指针,直接操作结构体中的数据
void SetStudentId(StudentInfo_t* s, int year, int classNum, int serialNum)
{
char buffer[20];
sprintf(buffer, "%d%d%d", year, classNum, serialNum);
int id = atoi(buffer);
s->id = id;
}
获取学生的性别函数,在面向对象的编程方法中获取数据的函数被我们设置为GetXXX,设置数据的函数被我们设置为SetXXX。
const char* GetGender(StudentInfo_t* s)
{
if (s->gender == 0)
{
return "女";
}
else if (s->gender == 1)
{
return "男";
}
return "未知";
}
设置数据的方法
void SetGender(StudentInfo_t* s, const char* strGender)
{
int numGender;
if (strcmp("男", strGender) == 0)
{
numGender = 1;
}
else if (strcmp("女", strGender) == 0)
{
numGender = 0;
}
else
{
numGender = -1;
}
s->gender = numGender;
}
通过主函数进行调用
int main()
{
StudentInfo_t stu;
SetStudentId(&stu, 2022, 123, 26);
strcpy(stu.name, "小明");
SetGender(&stu, "男");
stu.mark = 98;
// 打印这些数值
printf("学号:%d\n", stu.id);
printf("姓名:%s\n", stu.name);
const char* gender = GetGender(&stu);
printf("性别:%s\n", gender);
printf("分数:%d\n", stu.mark);
}
完整函数代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int id;
char name[20];
int gender;
int mark;
}StudentInfo_t;
void SetStudentId(StudentInfo_t* s, int year, int classNum, int serialNum)
{
char buffer[20];
sprintf(buffer, "%d%d%d", year, classNum, serialNum);
int id = atoi(buffer);
s->id = id;
}
const char* GetGender(StudentInfo_t* s)
{
if (s->gender == 0)
{
return "女";
}
else if (s->gender == 1)
{
return "男";
}
return "未知";
}
void SetGender(StudentInfo_t* s, const char* strGender)
{
int numGender;
if (strcmp("男", strGender) == 0)
{
numGender = 1;
}
else if (strcmp("女", strGender) == 0)
{
numGender = 0;
}
else
{
numGender = -1;
}
s->gender = numGender;
}
int main()
{
StudentInfo_t stu;
SetStudentId(&stu, 2022, 123, 26);
strcpy(stu.name, "小明");
SetGender(&stu, "男");
stu.mark = 98;
// 打印这些数值
printf("学号:%d\n", stu.id);
printf("姓名:%s\n", stu.name);
const char* gender = GetGender(&stu);
printf("性别:%s\n", gender);
printf("分数:%d\n", stu.mark);
}
对象.方法(对象指针,参数1,参数2, 参数3...)
stu.setGender(&stu, "男");
const char* gender = stu.getGender(&stu);
5.0 面向对象
在结构体中声明函数指针
struct student {
void (*setStudentId)(struct student* s, int year, int classNum, int serialNum);
const char* (*getGender)(struct student* s);
void (*setGender)(struct student* s, const char* strGender);
int id; // 学号
char name[20]; // 姓名
int gender; // 性别
int mark; // 分数
};
这个时候可以将结构体作为一个对象看待,使用对象(结构体变量). 方法(函数)的方式进行参数的赋值和调用。
生成学生的id
void setStudentId(struct student* s, int year, int classNum, int serialNum)
{
char buffer[20];
sprintf(buffer, "%d%d%d", year, classNum, serialNum);
int id = atoi(buffer);
s->id = id;
}
获取学生的性别
const char* getGender(struct student* s)
{
if (s->gender == 0)
{
return "女";
}
else if (s->gender == 1)
{
return "男";
}
return "未知";
}
设置学生的性别
void setGender(struct student* s, const char* strGender)
{
int numGender;
if (strcmp("男", strGender) == 0)
{
numGender = 1;
}
else if (strcmp("女", strGender) == 0)
{
numGender = 0;
}
else
{
numGender = -1;
}
s->gender = numGender;
}
初始化函数指针【对象初始化之后才能被调用】
void initStudent(struct student* s)
{
s->setStudentId = setStudentId;
s->getGender = getGender;
s->setGender = setGender;
}
主函数相关代码
int main()
{
struct student stu;
// 初始化student
initStudent(&stu);
stu.setStudentId(&stu, 2022, 123, 26);
strcpy(stu.name, "小明");
stu.setGender(&stu, "男");
stu.mark = 98;
// 打印这些数值
printf("学号:%d\n", stu.id);
printf("姓名:%s\n", stu.name);
const char* gender = stu.getGender(&stu);
printf("性别:%s\n", gender);
printf("分数:%d\n", stu.mark);
}
完整代码
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student {
void (*setStudentId)(struct student* s, int year, int classNum, int serialNum);
const char* (*getGender)(struct student* s);
void (*setGender)(struct student* s, const char* strGender);
int id; // 学号
char name[20]; // 姓名
int gender; // 性别
int mark; // 分数
};
void setStudentId(struct student* s, int year, int classNum, int serialNum)
{
char buffer[20];
sprintf(buffer, "%d%d%d", year, classNum, serialNum);
int id = atoi(buffer);
s->id = id;
}
const char* getGender(struct student* s)
{
if (s->gender == 0)
{
return "女";
}
else if (s->gender == 1)
{
return "男";
}
return "未知";
}
void setGender(struct student* s, const char* strGender)
{
int numGender;
if (strcmp("男", strGender) == 0)
{
numGender = 1;
}
else if (strcmp("女", strGender) == 0)
{
numGender = 0;
}
else
{
numGender = -1;
}
s->gender = numGender;
}
void initStudent(struct student* s)
{
s->setStudentId = setStudentId;
s->getGender = getGender;
s->setGender = setGender;
}
int main()
{
struct student stu;
// 初始化student
initStudent(&stu);
stu.setStudentId(&stu, 2022, 123, 26);
strcpy(stu.name, "小明");
stu.setGender(&stu, "男");
stu.mark = 98;
// 打印这些数值
printf("学号:%d\n", stu.id);
printf("姓名:%s\n", stu.name);
const char* gender = stu.getGender(&stu);
printf("性别:%s\n", gender);
printf("分数:%d\n", stu.mark);
}
6.0 继承基本概念
- 工号
- 姓名
- 性别
- 任课科目
struct teacher
{
int id; // 工号
char name[20]; // 姓名
int gender; // 性别
char subject[20]; // 任课科目
};
struct teacher {
int id; // 工号
char name[20]; // 姓名
int gender; // 性别
char subject[20]; // 任课科目
};
struct student {
int id; // 学号
char name[20]; // 姓名
int gender; // 性别
int mark; // 分数
};
- 编号
- 姓名
- 性别
- 学生有考试分数
- 老师有任课科目
struct person{
int id; // 编号
char name[20]; // 姓名
int gender; // 性别
};
struct teacher {
struct person super;
char subject[20]; // 任课科目
};
struct student {
struct person super;
int mark; // 分数
};
// 原有代码
struct teacher {
int id; // 工号
char name[20]; // 姓名
int gender; // 性别
char subject[20]; // 任课科目
};
struct student {
int id; // 学号
char name[20]; // 姓名
int gender; // 性别
int mark; // 分数
};
// 现有代码
struct person{
int id; // 编号
char name[20]; // 姓名
int gender; // 性别
};
struct teacher {
struct person super;
char subject[20]; // 任课科目
};
struct student {
struct person super;
int mark; // 分数
};
- 1. 减少重复代码
- 2. 代码层次更清晰
刚刚我们只讨论了数据,现在我们结合上方法一起讨论
struct person{
int id; // 编号
char name[20]; // 姓名
int gender; // 性别
};
struct teacher {
struct person super;
char subject[20]; // 任课科目
};
struct student {
struct person super;
int mark; // 分数
void (*setStudentId)(struct student* s, int year, int classNum, int serialNum);
const char* (*getGender)(struct student* s);
void (*setGender)(struct student* s, const char* strGender);
};
- 设置性别
- 获取性别
- 设置学号
创建一个Person方法,内部包含
struct person
{
int id;
char name[20];
int gender;
const char* (*getGender)(struct student* s);
void (*setGender)(struct student* s, const char* strGender);
};
struct teacher
{
// 创建结构体成员变量
struct person super;
char subject[20];
};
struct student {
void (*setStudentId)(struct student* s, int year, int classNum, int serialNum);
struct person super;
int mark; // 分数
};
const char* getGender(struct person* p)
{
if (p->gender == 0)
{
return "女";
}
else if (p->gender == 1)
{
return "男";
}
return "未知";
}
void setGender(struct person* p, const char* strGender)
{
int numGender;
if (strcmp("男", strGender) == 0)
{
numGender = 1;
}
else if (strcmp("女", strGender) == 0)
{
numGender = 0;
}
else
{
numGender = -1;
}
p->gender = numGender;
}
void setStudentId(struct student* s, int year, int classNum, int serialNum)
{
char buffer[20];
sprintf(buffer, "%d%d%d", year, classNum, serialNum);
int id = atoi(buffer);
s->super.id = id;
}
void initPerson(struct person* p)
{
p->getGender = getGender;
p->setGender = setGender;
}
void initStudent(struct student* s)
{
initPerson(&(s->super));
s->setStudentId = setStudentId;
}
void initTeacher(struct teacher* t)
{
initPerson(&(t->super));
}
main函数调用
int main()
{
struct student stu;
// 初始化student
initStudent(&stu);
stu.setStudentId(&stu, 2022, 123, 26);
strcpy(stu.super.name, "小明");
stu.super.setGender(&stu.super, "男");
stu.mark = 98;
// 打印这些数值
printf("学号:%d\n", stu.super.id);
printf("姓名:%s\n", stu.super.name);
const char* gender = stu.super.getGender(&stu.super);
printf("性别:%s\n", gender);
printf("分数:%d\n", stu.mark);
putchar('\n');
struct teacher t;
// 初始化teacher
initTeacher(&t);
t.super.id = 12345;
strcpy(t.super.name, "林老师");
t.super.setGender(&t.super, "男");
strcpy(t.subject, "C语言");
// 打印这些数值
printf("学号:%d\n", t.super.id);
printf("姓名:%s\n", t.super.name);
gender = t.super.getGender(&t.super);
printf("性别:%s\n", gender);
printf("科目:%s\n", t.subject);
}
完整代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct person
{
int id;
char name[20];
int gender;
const char* (*getGender)(struct student* s);
void (*setGender)(struct student* s, const char* strGender);
};
struct teacher
{
// 创建结构体成员变量
struct person super;
char subject[20];
};
struct student {
void (*setStudentId)(struct student* s, int year, int classNum, int serialNum);
struct person super;
int mark; // 分数
};
void setStudentId(struct student* s, int year, int classNum, int serialNum)
{
char buffer[20];
sprintf(buffer, "%d%d%d", year, classNum, serialNum);
int id = atoi(buffer);
s->super.id = id;
}
const char* getGender(struct person* p)
{
if (p->gender == 0)
{
return "女";
}
else if (p->gender == 1)
{
return "男";
}
return "未知";
}
void setGender(struct person* p, const char* strGender)
{
int numGender;
if (strcmp("男", strGender) == 0)
{
numGender = 1;
}
else if (strcmp("女", strGender) == 0)
{
numGender = 0;
}
else
{
numGender = -1;
}
p->gender = numGender;
}
void initPerson(struct person* p)
{
p->getGender = getGender;
p->setGender = setGender;
}
void initStudent(struct student* s)
{
initPerson(&(s->super));
s->setStudentId = setStudentId;
}
void initTeacher(struct teacher* t)
{
initPerson(&(t->super));
}
int main()
{
struct student stu;
// 初始化student
initStudent(&stu);
stu.setStudentId(&stu, 2022, 123, 26);
strcpy(stu.super.name, "小明");
stu.super.setGender(&stu.super, "男");
stu.mark = 98;
// 打印这些数值
printf("学号:%d\n", stu.super.id);
printf("姓名:%s\n", stu.super.name);
const char* gender = stu.super.getGender(&stu.super);
printf("性别:%s\n", gender);
printf("分数:%d\n", stu.mark);
putchar('\n');
struct teacher t;
// 初始化teacher
initTeacher(&t);
t.super.id = 12345;
strcpy(t.super.name, "林老师");
t.super.setGender(&t.super, "男");
strcpy(t.subject, "C语言");
// 打印这些数值
printf("学号:%d\n", t.super.id);
printf("姓名:%s\n", t.super.name);
gender = t.super.getGender(&t.super);
printf("性别:%s\n", gender);
printf("科目:%s\n", t.subject);
}
程序运行结果
7.0 多态
struct Rect {
void (*draw)(struct Rect *);
int left;
int top;
int right;
int bottom;
};
struct Circle {
void (*draw)(struct Circle *);
int x;
int y;
int r;
};
struct Triangle {
void (*draw)(struct Triangle *);
POINT p1;
POINT p2;
POINT p3;
};
struct Shape {
void (*draw)(struct Shape *);
};
这是共性的结构体,可以称之为结构体对象
struct Rect {
struct Shape super;
int left;
int top;
int right;
int bottom;
};
struct Circle {
struct Shape super;
int x;
int y;
int r;
};
struct Triangle {
struct Shape super;
POINT p1;
POINT p2;
POINT p3;
};
如果父对象和子对象的内存排布不重合会出现错误
// 正确
struct Rect {
struct Shape super;
int left;
int top;
int right;
int bottom;
};
// 错误
struct Rect {
int left;
int top;
int right;
int bottom;
struct Shape super;
};
void initRect(struct Rect* r)
{
r->super.draw = drawRect;
}
void initCircle(struct Circle* c)
{
c->super.draw = drawCircle;
}
void initTriangle(struct Triangle* t)
{
t->super.draw = drawTriangle;
}
- void (*)(struct Rect*)
- void (*)(struct Circle*)
- void (*)(struct Triangle*)
void initRect(struct Rect* r)
{
r->super.draw = (void (*)(struct Shape*))drawRect;
}
void initCircle(struct Circle* c)
{
c->super.draw = (void (*)(struct Shape*))drawCircle;
}
void initTriangle(struct Triangle* t)
{
t->super.draw = (void (*)(struct Shape*))drawTriangle;
}
struct Rect r = { {}, - 200, 200, 200, 0 };
struct Circle c = { {},0, 0, 100 };
struct Triangle t = { {}, {0, 200}, {-200, 0}, {200, 0} };
initRect(&r);
initCircle(&c);
initTriangle(&t);
让三个对象分别调用各自的初始化函数,给各自对象 super 成员中的 draw 设置为各自对应的绘图函数。
r.super.draw 设置为 drawRect
c.super.draw 设置为 drawCircle
t.super.draw 设置为 drawRTriangle
struct Shape *arrShape[3] = {
(struct Shape *)&r, (struct Shape*)&c, (struct Shape*)&t};
for (int i = 0; i < 3; i++)
{
arrShape[i]->draw(arrShape[i]);
}
到了关键的一步,使用循环,依次调用 draw 函数。由于3次循环中的 draw 函数分别为各个图形各自的 绘图函数。所以,虽然统一调用的是 draw ,但是,却可以执行它们各自的绘图函数。至此,不同实现 的方法,在此得到统一。
完整代码实现
#define _CRT_SECURE_NO_WARNINGS
#include <stdint.h>
#include <stdio.h>
#include <easyx.h>
#include <stdlib.h>
struct Shape
{
void (*draw)(struct Shape*);
};
struct Rect
{
struct Shape super;
int left;
int top;
int right;
int bottom;
};
struct Circle
{
struct Shape super;
int x;
int y;
int r;
};
struct Triangle
{
struct Shape super;
POINT p1;
POINT p2;
POINT p3;
};
void drawRect(struct Rect* r)
{
rectangle(r->left, r->top, r->right, r->bottom);
}
void drawCircle(struct Circle* c)
{
circle(c->x, c->y, c->r);
}
void drawTriangle(struct Triangle* t)
{
line(t->p1.x, t->p1.y, t->p2.x, t->p2.y);
line(t->p2.x, t->p2.y, t->p3.x, t->p3.y);
line(t->p3.x, t->p3.y, t->p1.x, t->p1.y);
}
void InitRect(struct Rect* r)
{
r->super.draw = (void(*)(struct Shape*)) drawRect;
}
void InitCircle(struct Circle* c)
{
c->super.draw = (void(*)(struct Shape*))drawCircle;
}
void InitTriangle(struct Triangle* t)
{
t->super.draw = (void(*)(struct Shape*))drawTriangle;
}
int main()
{
initgraph(800, 600);
setaspectratio(1, -1);
setorigin(400, 300);
setbkcolor(WHITE);
setlinecolor(BLACK);
cleardevice();
struct Rect r = { {}, - 200, 200, 200, 0 };
struct Circle c = { {},0, 0, 100 };
struct Triangle t = { {}, {0, 200}, {-200, 0}, {200, 0} };
InitRect(&r);
InitCircle(&c);
InitTriangle(&t);
struct Shape* arrShape[3] =
{
(struct Shape*)&r,
(struct Shape*)&c,
(struct Shape*)&t
};
for (int i = 0; i < 3; i++)
{
arrShape[i]->draw(arrShape[i]);
}
getchar();
closegraph();
return 0;
}
让我们回顾一下在之前实现多态的步骤:
注:参考你好编程C语言教程编写,仅供学习参考