pybind11—类(继承,重载,枚举)
2019-02-12 本文已影响1人
侠之大者_7d3f
前言
在C++类中, 继承, 函数重载是重要的特征。
- 继承
/*
继承
class_<子类, 父类>
*/
py::class_<Teacher, Person>(m, "Teacher")
.def(py::init())
-
重载 overload
py::overload_cast<Typenname>( Func)
.def("eat", py::overload_cast<std::string>(&Teacher::eat))
.def("eat", py::overload_cast<std::string, int>(&Teacher::eat))
.def("eat", py::overload_cast<>(&Teacher::eat));
-
枚举
py::enum_<>
/*
枚举
*/
py::enum_<Items>(m, "Items")
.value("Chinese", Items::Chinese)
.value("Math", Items::Math)
.value("English", Items::English)
.value("Science", Items::Science)
.value("Bialogy", Items::Bialogy)
.export_values();
开发测试环境
- windows10, 64bit
- Anaconda3, with python 3.7
- Visual Studio 2017
- pycharm
- pybind11
pybind11
类,继承,函数重载
定义父类:Person
class Person
{
public:
std::string habbit; //兴趣爱好
private:
std::string name; //姓名
std::string gender; //性别
int age; //年龄
std::string idCardNum; //身份证号码
std::string address; //住址
public:
void setName(std::string name) {
this->name = name;
}
void setAge(int age) {
this->age = age;
}
void setGender(std::string gender) {
this->gender = gender;
}
void setAddress(std::string address) {
this->address = address;
}
void setIdCardNum(std::string idCardNum) {
this->idCardNum = idCardNum;
}
std::string getName() {
return this->name;
}
std::string getGender() {
return this->gender;
}
int getAge() {
return this->age;
}
std::string getAddress() {
return this->address;
}
std::string getIdCardNum() {
return this->idCardNum;
}
public:
//
static std::string university;
static std::string getUniversity() {
return university;
}
public:
Person() {};
Person(std::string name, std::string gender, int age) {
this->name = name;
this->gender = gender;
this->age = age;
}
~Person() {};
};
std::string Person::university = "xidian University";
定义子类 : Teacher
包含重载函数
/*
Teacher类, 继承于Person
*/
class Teacher: public Person
{
private:
float salary;
std::string subject;
std::string level;
public:
Teacher(){
Person("xiaoMing", "male", 35);
this->salary = 5000;
this->subject = "Math";
this->level = "Professor";
};
~Teacher() {};
Teacher(std::string name, std::string gender, int age, float salary, std::string subject, std::string level) {
this->setName(name);
this->setAge(age);
this->setGender(gender);
this->salary = salary;
this->subject = subject;
this->level = level;
}
public:
//setter
void setSalary(float salary) {
this->salary = salary;
}
void setSubject(std::string subject) {
this->subject = subject;
}
void setLevel(std::string level) {
this->level = level;
}
std::string getSubject() {
return this->subject;
}
float getSalary() {
return this->salary;
}
std::string getLevel() {
return this->level;
}
public:
//重载 overload
void eat(std::string food) {
std::cout << this->getName() + " " + "eat" + " " + food << std::endl;
}
void eat() {
std::cout << this->getName() + " " + "eat" + " " + "rice(default)" << std::endl;
}
void eat(std::string food, int time) {
std::cout << this->getName() + " " + "eat" + " " + food + ". At "+ std::to_string(time) + "clock."<< std::endl;
}
};
编写python扩展
PYBIND11_MODULE(demo5, m) {
m.doc() = "Simple Class";
py::class_<Person>(m, "Person")
.def(py::init())
.def(py::init<std::string, std::string, int>())
.def("setName", &Person::setName)
.def("setAge", &Person::setAge)
.def("setGender", &Person::setGender)
.def("setAddress", &Person::setAddress)
.def("setIdCardNum", &Person::setIdCardNum)
.def("getName", &Person::getName)
.def("getAge", &Person::getAge)
.def("getGender", &Person::getGender)
.def("getAddress", &Person::getAddress)
.def("getIdCardNum", &Person::getIdCardNum)
.def_static("getUniversity", &Person::getUniversity)
.def_readonly_static("university", &Person::university)
.def_readwrite("habbit", &Person::habbit)
.def_property("name", &Person::getName, &Person::setName)
.def_property("age", &Person::getAge, &Person::setAge)
.def_property("gender", &Person::getGender, &Person::setGender)
.def_property("address", &Person::getAddress, &Person::setAddress)
.def_property("idCardNum", &Person::getIdCardNum, &Person::setIdCardNum);
/*
继承
class_<子类, 父类>
*/
py::class_<Teacher, Person>(m, "Teacher")
.def(py::init())
.def(py::init<std::string, std::string, int, float, std::string, std::string>())
.def("setSalary", &Teacher::setSalary)
.def("setSubject", &Teacher::setSubject)
.def("setLevel", &Teacher::setLevel)
.def_property("salary", &Teacher::getSalary, &Teacher::setSalary)
.def_property("subject", &Teacher::getSubject, &Teacher::setSubject)
.def_property("level", &Teacher::getLevel, &Teacher::setLevel)
.def("eat", py::overload_cast<std::string>(&Teacher::eat))
.def("eat", py::overload_cast<std::string, int>(&Teacher::eat))
.def("eat", py::overload_cast<>(&Teacher::eat));
}
打开pycharm,编写python测试代码

python测试代码
from demo5 import demo5
person1 = demo5.Person()
print(type(person1))
print(help(demo5.Person()))
teacher = demo5.Teacher()
teacher.setName("Dataintian")
teacher.setGender("female")
teacher.habbit = 'basketball'
teacher.setAge(35)
teacher.setIdCardNum("610522198811267819")
teacher.setAddress("Xian")
teacher.setSalary(6000)
teacher.setLevel("Professor")
teacher.setSubject("GaoShu")
print(type(teacher))
help(teacher)
print(teacher.name)
print(teacher.age)
print(teacher.idCardNum)
print(teacher.salary)
print(teacher.level)
print(teacher.subject)
# 测试C++ overload
teacher.eat()
teacher.eat('Milk')
teacher.eat('Fish', 12)







枚举
在C++中定义一个枚举
enum Items
{
Chinese = 0,
Math,
English,
Science,
Bialogy
};
编写python扩展
PYBIND11_MODULE(demo5, m) {
/*
枚举
*/
py::enum_<Items>(m, "Items")
.value("Chinese", Items::Chinese)
.value("Math", Items::Math)
.value("English", Items::English)
.value("Science", Items::Science)
.value("Bialogy", Items::Bialogy)
.export_values();
}
python测试代码
# 测试枚举
var1 = demo5.Items
print(demo5.Items.__members__)

完整工程
代码
#include<iostream>
#include<pybind11/pybind11.h>
namespace py = pybind11;
class Person
{
public:
std::string habbit; //兴趣爱好
private:
std::string name; //姓名
std::string gender; //性别
int age; //年龄
std::string idCardNum; //身份证号码
std::string address; //住址
public:
void setName(std::string name) {
this->name = name;
}
void setAge(int age) {
this->age = age;
}
void setGender(std::string gender) {
this->gender = gender;
}
void setAddress(std::string address) {
this->address = address;
}
void setIdCardNum(std::string idCardNum) {
this->idCardNum = idCardNum;
}
std::string getName() {
return this->name;
}
std::string getGender() {
return this->gender;
}
int getAge() {
return this->age;
}
std::string getAddress() {
return this->address;
}
std::string getIdCardNum() {
return this->idCardNum;
}
public:
//
static std::string university;
static std::string getUniversity() {
return university;
}
public:
Person() {};
Person(std::string name, std::string gender, int age) {
this->name = name;
this->gender = gender;
this->age = age;
}
~Person() {};
};
std::string Person::university = "xidian University";
/*
Teacher类, 继承于Person
*/
class Teacher: public Person
{
private:
float salary;
std::string subject;
std::string level;
public:
Teacher(){
Person("xiaoMing", "male", 35);
this->salary = 5000;
this->subject = "Math";
this->level = "Professor";
};
~Teacher() {};
Teacher(std::string name, std::string gender, int age, float salary, std::string subject, std::string level) {
this->setName(name);
this->setAge(age);
this->setGender(gender);
this->salary = salary;
this->subject = subject;
this->level = level;
}
public:
//setter
void setSalary(float salary) {
this->salary = salary;
}
void setSubject(std::string subject) {
this->subject = subject;
}
void setLevel(std::string level) {
this->level = level;
}
std::string getSubject() {
return this->subject;
}
float getSalary() {
return this->salary;
}
std::string getLevel() {
return this->level;
}
public:
//重载 overload
void eat(std::string food) {
std::cout << this->getName() + " " + "eat" + " " + food << std::endl;
}
void eat() {
std::cout << this->getName() + " " + "eat" + " " + "rice(default)" << std::endl;
}
void eat(std::string food, int time) {
std::cout << this->getName() + " " + "eat" + " " + food + ". At "+ std::to_string(time) + "clock."<< std::endl;
}
};
enum Items
{
Chinese = 0,
Math,
English,
Science,
Bialogy
};
PYBIND11_MODULE(demo5, m) {
m.doc() = "Simple Class";
py::class_<Person>(m, "Person")
.def(py::init())
.def(py::init<std::string, std::string, int>())
.def("setName", &Person::setName)
.def("setAge", &Person::setAge)
.def("setGender", &Person::setGender)
.def("setAddress", &Person::setAddress)
.def("setIdCardNum", &Person::setIdCardNum)
.def("getName", &Person::getName)
.def("getAge", &Person::getAge)
.def("getGender", &Person::getGender)
.def("getAddress", &Person::getAddress)
.def("getIdCardNum", &Person::getIdCardNum)
.def_static("getUniversity", &Person::getUniversity)
.def_readonly_static("university", &Person::university)
.def_readwrite("habbit", &Person::habbit)
.def_property("name", &Person::getName, &Person::setName)
.def_property("age", &Person::getAge, &Person::setAge)
.def_property("gender", &Person::getGender, &Person::setGender)
.def_property("address", &Person::getAddress, &Person::setAddress)
.def_property("idCardNum", &Person::getIdCardNum, &Person::setIdCardNum);
/*
继承
class_<子类, 父类>
*/
py::class_<Teacher, Person>(m, "Teacher")
.def(py::init())
.def(py::init<std::string, std::string, int, float, std::string, std::string>())
.def("setSalary", &Teacher::setSalary)
.def("setSubject", &Teacher::setSubject)
.def("setLevel", &Teacher::setLevel)
.def_property("salary", &Teacher::getSalary, &Teacher::setSalary)
.def_property("subject", &Teacher::getSubject, &Teacher::setSubject)
.def_property("level", &Teacher::getLevel, &Teacher::setLevel)
.def("eat", py::overload_cast<std::string>(&Teacher::eat))
.def("eat", py::overload_cast<std::string, int>(&Teacher::eat))
.def("eat", py::overload_cast<>(&Teacher::eat));
/*
枚举
*/
py::enum_<Items>(m, "Items")
.value("Chinese", Items::Chinese)
.value("Math", Items::Math)
.value("English", Items::English)
.value("Science", Items::Science)
.value("Bialogy", Items::Bialogy)
.export_values();
}