基石之力:掌握 C++ 继承的核心奥秘

基石之力:掌握 C++ 继承的核心奥秘

目录

1:继承的概念和定义

1.1:继承的概念

1.2:继承定义

1.2.1:继承的格式

1.2.2:继承基类成员访问方式的变化

2:基类和派生类对象赋值转换

2.1:代码1(派生类对象赋值给基类对象)

2.2:代码2(派生类对象赋值给基类对象的引用)

2.3:代码3(派生类对象赋值给基类对象的指针)

3:继承中的作用域

3.1:代码1

3.2:代码2

3.3:代码3

4:派生类的默认成员函数

4.1:父类和子类均有默认构造函数

4.2:子类没有默认构造函数

4.3:子类有默认构造构造函数

4.4:子类有无默认拷贝构造函数

4.5:子类有默认的赋值运算符重载

4.6:子类无默认的赋值运算符重载

4.7:子类中的析构函数

4.8:构造与析构函数的顺序

4.9:总结

5:继承与友元

6:继承与静态成员

7:菱形继承与菱形虚拟继承

7.1:单继承

7.2:多继承

7.3:菱形继承

7.3.1:菱形继承造成数据冗余

7.3.2:菱形继承会造成二义性的问题

7.3.3:解决菱形继承的数据冗余与二义性问题的方案(虚拟继承)

7.4:虚拟继承解决数据冗余和二义性的原理

8:继承的反思与总结


1:继承的概念和定义

1.1:继承的概念

继承机制是面向对象程序设计使代码可以复用的最重要的手段,它允许程序员在保持原有类特性的基础上进行扩展,增加功能,这样产生新的类,简称派生类.继承呈现了面向对象程序设计的层次结构,体现了由简单到复杂的认知过程.以前我们接触的复用都是函数复用,继承是类设计层次的复用.

1.2:继承定义

1.2.1:继承的格式

1.2.2:继承基类成员访问方式的变化

//父类/基类 class Person { public: void Print() { cout << "name:>" << _name << endl; cout << "age:>" << _age << endl; } //基类的成员私有 private: string _name = "李华"; int _age = 25; }; //公有继承 class Student: public Person { public: int _stuid; int _major; }; int main() { Student s1; s1.Print(); s1._name = "张三"; return 0; }

//父类/基类 class Person { public: void Print() { cout << "name:>" << _name << endl; cout << "age:>" << _age << endl; } protected: string _name = "李华"; private: int _age = 25; }; //公有继承 class Student : public Person { public: void Function() { _name = "张三"; //不能够直接访问基类的私有成员,就像不能够直接用父亲的私房钱 _age = 25; //但是可以间接使用 Print(); } private: int _stuid; int _major; }; int main() { Student s1; s1.Function(); }

#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; //使用关键字class时默认的继承方式是private class Person { public: string _name = "张三"; // 姓名 int _age; // 年龄 }; class Student : Person { }; int main() { Student s1; s1._name = "李四"; } 
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; //使用struct时默认的继承方式是public class Person { public: string _name = "张三"; // 姓名 int _age; // 年龄 }; struct Student : Person { }; int main() { Student s1; s1._name = "李四"; }
基类private成员在派生类中无论以什么方式继承都是不可见的。这里的不可见是指基类的私有成员还是被继承到了派生类对象中,但是语法上限制派生类对象不管在类里面还是类外面都不能去访问它。基类private成员派生类中是不能被访问,如果基类成员不想在类外直接被访问,但需要在派生类中能访问,就定义为protected。可以看出保护成员限定符是因继承才出现的。基类的私有成员子类都是不可见基类的其他成员子类的访问方式 == Min(成员在基类的访问限定符,继承方式)public > protected>private。使用关键字class时默认的继承方式是private,使用struct时默认的继承方式是public,不过最好显示的写出继承方式。在实际运用中一般使用都是public继承,几乎很少使用protetced/private继承,也不提倡使用protetced/private继承,因为protetced/private继承下来的成员都只能在派生类的类里面使用,实际中扩展维护性不强

2:基类和派生类对象赋值转换

  • 派生类对象可以赋值给基类的对象/基类的指针/基类的引用.形象说法:切片或者切割即将派生类中的父类那一部分切割下来然后进行赋值.
  • 基类对象不能赋值给派生类对象.
  • 基类的指针或者引用可以通过强制类型转换赋值给派生类的指针或者引用。但是必须是基类的指针是指向派生类对象时才是安全的.

2.1:代码1(派生类对象赋值给基类对象)

class Person { //使用保护令其能够在派生类中访问 protected: string _name = "李华"; string _sex = "男"; int _age = 18; }; //公有继承 class Student : public Person { public: private: int _Number = 1; }; int main() { Student s1; Person p1 = s1; }

2.2:代码2(派生类对象赋值给基类对象的引用)

#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Person { public: void Change() { _name = "李红"; _sex = "女"; _age = 17; } //使用保护令其能够在派生类中访问 protected: string _name = "李华"; string _sex = "男"; int _age = 18; }; //公有继承 class Student : public Person { private: int _Number = 1; }; int main() { //派生类对象赋值给基类对象的引用 Student s2; Person& p2 = s2; p2.Change(); }

2.3:代码3(派生类对象赋值给基类对象的指针)

#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Person { public: void Change() { _name = "李红"; _sex = "女"; _age = 17; } //使用保护令其能够在派生类中访问 protected: string _name = "李华"; string _sex = "男"; int _age = 18; }; //公有继承 class Student : public Person { private: int _Number = 1; }; int main() { //派生类对象赋值给基类对象的指针 Student s3; Person* p3 = &s3; p3->Change(); return 0; }

3:继承中的作用域

在继承体系中基类与派生类都有独立的作用域.子类与父类中有同名成员,子类成员将屏蔽父类对同名成员的直接访问,该情况叫做隐藏,也叫重定义.成员函数隐藏,只要函数名相同即可.注意在实际中在继承体系里面最好不要定义同名的成员

PS:在子类成员函数中,可以通过使用域作用限定符在访问基类成员.

3.1:代码1

#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Person { protected: string _name; string _sex; int _age; int _number = 125; }; class Student : public Person { public: void Print() { //由于_number同名,因此构成了隐藏 cout << "class Student:>" << _number << endl; //访问基类中的同名成员则需要指定域 cout << "class Person:>" << Person::_number << endl; } protected: int _number = 128; }; int main() { Student s1; s1.Print(); }

3.2:代码2

下面这段代码是一道面试题

A:编译报错   B:运行报错   C:两个func函数构成函数重载   D:两个func函数构成隐藏.
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class A { public: void func() { cout << "func()" << endl; } }; class B : public A { public: void func(int i) { A::func(); cout << "func(int i)->" << i << endl; } }; int main() { B b; b.func(10); return 0; }
这道题的答案选D,因为在继承关系中,只要基类和派生类中的成员函数名一致,就会构成隐藏.

3.3:代码3

#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class A { public: void func() { cout << "func()" << endl; } }; class B : public A { public: void func(int i) { A::func(); cout << "func(int i)->" << i << endl; } }; int main() { B b; b.func(); b.A::func(); }
当这道面试题将b.func(10)的中的参数去去掉用了以后,那么此时将是多选,选择AD,因为由于创建了对象b,在继承关系中,派生类与基类存在同名成员函数的话,则派生类会屏蔽对基类成员的访问.若要访问基类成员函数的话,则需要指定域.

4:派生类的默认成员函数

4.1:父类和子类均有默认构造函数

#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; #include <string> class Person { public: //默认构造函数 Person(const char * name = "父亲") :_name(name) { cout << "Person()" << endl; } //拷贝构造函数 Person(const Person &p) :_name(p._name) { cout << "Person(const Person & p)" << endl; } //赋值运算符重载函数 Person& operator=(const Person & p) { cout << "Person & operator=(const Person & p)" << endl; if (this != &p) { this->_name = p._name; } return *this; } //析构函数 ~Person() { cout << "~Person()" << endl; } protected: string _name; }; class Student : public Person { public: protected: int _numerber; string s; }; int main() { Student s; return 0; } 

4.2:子类没有默认构造函数

#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; #include <string> class Person { public: //默认构造函数 Person(const char * name = "父亲") :_name(name) { cout << "Person()" << endl; } //拷贝构造函数 Person(const Person &p) :_name(p._name) { cout << "Person(const Person & p)" << endl; } //赋值运算符重载函数 Person& operator=(const Person & p) { cout << "Person & operator=(const Person & p)" << endl; if (this != &p) { this->_name = p._name; } return *this; } //析构函数 ~Person() { cout << "~Person()" << endl; } protected: string _name; }; class Student : public Person { public: Student(int number, const char* str, const char* name) :_name(name) ,_number(number) ,_str(str) {} protected: int _number; string _str; }; int main() { Student s; return 0; } 
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; #include <string> class Person { public: //默认构造函数 Person(const char * name = "父亲") :_name(name) { cout << "Person()" << endl; } //拷贝构造函数 Person(const Person &p) :_name(p._name) { cout << "Person(const Person & p)" << endl; } //赋值运算符重载函数 Person& operator=(const Person & p) { cout << "Person & operator=(const Person & p)" << endl; if (this != &p) { this->_name = p._name; } return *this; } //析构函数 ~Person() { cout << "~Person()" << endl; } protected: string _name; }; class Student : public Person { public: Student(int number, const char* str, const char* name) :Person(name) ,_number(number) ,_str(str) {} protected: int _number; string _str; }; int main() { Student s1(25,"孩子", "父亲"); return 0; } 
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; #include <string> class Person { public: //默认构造函数 Person(const char * name = "父亲") :_name(name) { cout << "Person()" << endl; } //拷贝构造函数 Person(const Person &p) :_name(p._name) { cout << "Person(const Person & p)" << endl; } //赋值运算符重载函数 Person& operator=(const Person & p) { cout << "Person & operator=(const Person & p)" << endl; if (this != &p) { this->_name = p._name; } return *this; } //析构函数 ~Person() { cout << "~Person()" << endl; } protected: string _name; }; class Student : public Person { public: Student(int number, const char* str, const char* name) :Person(name) ,_number(number) ,_str(str) {} protected: int _number; string _str; }; int main() { Student s1; return 0; } 

4.3:子类有默认构造构造函数

#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; #include <string> class Person { public: //默认构造函数 Person(const char * name = "父亲") :_name(name) { cout << "Person()" << endl; } //拷贝构造函数 Person(const Person &p) :_name(p._name) { cout << "Person(const Person & p)" << endl; } //赋值运算符重载函数 Person& operator=(const Person & p) { cout << "Person & operator=(const Person & p)" << endl; if (this != &p) { this->_name = p._name; } return *this; } //析构函数 ~Person() { cout << "~Person()" << endl; } protected: string _name; }; class Student : public Person { public: Student(int number, const char* str, const char* name) :Person(name) ,_number(number) ,_str(str) {} //拷贝构造函数----->默认生成的拷贝构造函数 protected: int _number; string _str; }; int main() { Student s1(25, "孩子", "父亲"); Student s2(s1); return 0; } 

4.4:子类有无默认拷贝构造函数

#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; #include <string> class Person { public: //默认构造函数 Person(const char * name = "父亲") :_name(name) { cout << "Person()" << endl; } //拷贝构造函数 Person(const Person &p) :_name(p._name) { cout << "Person(const Person & p)" << endl; } //赋值运算符重载函数 Person& operator=(const Person & p) { cout << "Person & operator=(const Person & p)" << endl; if (this != &p) { this->_name = p._name; } return *this; } //析构函数 ~Person() { cout << "~Person()" << endl; } protected: string _name; }; class Student : public Person { public: Student(int number, const char* str, const char* name) :Person(name) ,_number(number) ,_str(str) {} Student(const Student & s) :Person(s) ,_number(_number) ,_str(_str) { } protected: int _number; string _str; }; int main() { Student s1(25, "孩子", "父亲"); Student s2(s1); return 0; } 

4.5:子类有默认的赋值运算符重载

#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; #include <string> #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> using namespace std; //拷贝构造函数----->无默认的拷贝构造函数 class Person { public: //构造函数 Person(const char* name = "父亲") //初始化列表 : _name(name) { cout << "Person()" << endl; } //拷贝构造 Person(const Person& p) : _name(p._name) { cout << "Person(const Person& p)" << endl; } //赋值运算符重载 Person& operator=(const Person& p) { cout << "Person operator=(const Person& p)" << endl; if (this != &p) _name = p._name; return *this; } protected: string _name; // 姓名 }; class Student : public Person { public: //构造函数 Student(int number, const char* str, const char* name) //父类会调用自己的构造函数去初始化 :Person(name) , _number(number) , _str(str) { } //拷贝构造函数 Student(const Student& s) :Person(s) , _number(_number) , _str(_str) { } //赋值运算符重载---->默认的赋值运算符 protected: int _number; string _str; }; int main() { Student s1(25, "孩子1", "父亲1"); Student s2(31, "孩子2", "父亲2"); s2 = s1; return 0; }

4.6:子类无默认的赋值运算符重载

#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; #include <string> #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> using namespace std; //拷贝构造函数----->无默认的拷贝构造函数 class Person { public: //构造函数 Person(const char* name = "父亲") //初始化列表 : _name(name) { cout << "Person()" << endl; } //拷贝构造 Person(const Person& p) : _name(p._name) { cout << "Person(const Person& p)" << endl; } //赋值运算符重载 Person& operator=(const Person& p) { cout << "Person operator=(const Person& p)" << endl; if (this != &p) _name = p._name; return *this; } protected: string _name; // 姓名 }; class Student : public Person { public: //构造函数 Student(int number, const char* str, const char* name) //父类会调用自己的构造函数去初始化 :Person(name) , _number(number) , _str(str) { } //拷贝构造函数 Student(const Student& s) :Person(s) , _number(_number) , _str(_str) { } Student& operator=(const Student& s) { if (this != &s) { this->Person::operator=(s); //调用父类的赋值运算符重载 _number = s._number; _str = s._str; } return *this; } protected: int _number; string _str; }; int main() { Student s1(25, "孩子1", "父亲1"); Student s2(31, "孩子2", "父亲2"); s2 = s1; return 0; }

4.7:子类中的析构函数

#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> using namespace std; class Person { public: //构造函数 Person(const char* name = "父亲") //初始化列表 : _name(name) { cout << "Person()" << endl; } //析构函数 ~Person() { cout << " ~Person()" << endl; } protected: string _name; // 姓名 }; class Student : public Person { public: //构造函数 Student(int number, const char* str, const char* name) //父类会调用自己的构造函数去初始化 :Person(name) , _number(number) , _str(str) { cout << "Student()" << endl; } //拷贝构造函数 Student(const Student& s) :Person(s) , _number(_number) , _str(_str) { } //析构函数 ~Student() { //子类的析构也会隐藏父类的析构 //因为后续多态的需要,析构函数的名字会被统一处理成destructor //Person::~Person(); cout << "~Student()" << endl; } protected: int _number; string _str; }; int main() { Student s(25, "父亲", "孩子"); return 0; } 

4.8:构造与析构函数的顺序

#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> using namespace std; class Person { public: //构造函数 Person(const char* name = "父亲") //初始化列表 : _name(name) { cout << "Person()" << endl; } //析构函数 ~Person() { cout << " ~Person()" << endl; } protected: string _name; // 姓名 }; class Student : public Person { public: //构造函数 Student(int number, const char* str, const char* name) //父类会调用自己的构造函数去初始化 :Person(name) , _number(number) , _str(str) { cout << "Student()" << endl; } //拷贝构造函数 Student(const Student& s) :Person(s) , _number(_number) , _str(_str) { } //析构函数 ~Student() { //子类的析构也会隐藏父类的析构 //因为后续多态的需要,析构函数的名字会被统一处理成destructor //Person::~Person(); cout << _name << endl; cout << "~Student()" << endl; } protected: int _number; string _str; }; int main() { Student s(25, "父亲", "孩子"); return 0; }

#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> using namespace std; class Person { public: //构造函数 Person(const char* name = "父亲") //初始化列表 : _name(name) { cout << "Person()" << endl; } //析构函数 ~Person() { cout << " ~Person()" << endl; } protected: string _name; // 姓名 }; class Student : public Person { public: //构造函数 Student(int number, const char* str, const char* name) //父类会调用自己的构造函数去初始化 :Person(name) , _number(number) , _str(str) { cout << "Student()" << endl; } //拷贝构造函数 Student(const Student& s) :Person(s) , _number(_number) , _str(_str) { } //析构函数 ~Student() { //子类的析构也会隐藏父类的析构 //因为后续多态的需要,析构函数的名字会被统一处理成destructor Person::~Person(); cout << _name << endl; cout << "~Student()" << endl; } protected: int _number; string _str; }; int main() { Student s(25, "父亲", "孩子"); return 0; }

4.9:总结

1. 派生类的构造函数必须调用基类的构造函数初始化基类的那一部分成员。如果基类没有默认的构造函数,则必须在派生类构造函数的初始化列表阶段显示调用。2. 派生类的拷贝构造函数必须调用基类的拷贝构造完成基类的拷贝初始化。3. 派生类的operator=必须要调用基类的operator=完成基类的复制。4. 派生类的析构函数会在被调用完成后自动调用基类的析构函数清理基类成员。因为这样才能保证派生类对象先清理派生类成员再清理基类成员的顺序。5. 派生类对象初始化先调用基类构造再调派生类构造。6. 派生类对象析构清理先调用派生类析构再调基类的析构。7. 因为后续一些场景析构函数需要构成重写,重写的条件之一是函数名相同(这个我们后面会讲解)。那么编译器会对析构函数名进行特殊处理,处理成destrutor(),所以父类析构函数不加virtual的情况下,子类析构函数和父类析构函数构成隐藏关系

5:继承与友元

#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> using namespace std; //前向声明 class Student; class Person { //进行友元声明 friend void Display(const Person& p, const Student& s); public: protected: //姓名 string _name = "张三"; }; //公有继承 class Student : public Person { protected: int _number; }; void Display(const Person& p, const Student& s) { cout << "姓名:" << p._name << endl; cout << "学号:" << s._number << endl; } 

6:继承与静态成员

#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> using namespace std; class Person { public: Person() { cout << "Person()" << endl; ++_count; } protected: string _name; public: static int _count; }; class Student : public Person { public: Student() { cout << "Student()" << endl; ++_count; } protected: string _name; }; class Graduate : public Student { public: Graduate() { cout << "Graduate()" << endl; ++_count; } protected: string _seminarcourse; }; int Person::_count = 0; int main() { Person p; Student s; Graduate g; cout << "Person:>" << p._count << endl; cout << "Student:>" << s._count << endl; cout << "Graduate:>" << g._count << endl; return 0; } 

7:菱形继承与菱形虚拟继承

7.1:单继承

#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> using namespace std; class Person { public: Person() { cout << "Person()" << endl; ++_count; } protected: string _name; public: static int _count; }; class Student : public Person { public: Student() { cout << "Student()" << endl; ++_count; } protected: string _name; }; class Graduate : public Student { public: Graduate() { cout << "Graduate()" << endl; ++_count; } protected: string _seminarcourse; };

7.2:多继承

#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> using namespace std; class Student { public: Student() :_number(2024) ,_name("张三") { cout << "Student()" << endl; } protected: int _number; string _name; }; class Teacher { public: Teacher() :_subject("高等数学") { cout << "Teachar()" << endl; } protected: string _subject; }; //多继承的公有继承 class Assistant : public Student, public Teacher { public: Assistant() { cout << "Assistant()" << endl; } }; int main() { Assistant a; return 0; }

7.3:菱形继承

7.3.1:菱形继承造成数据冗余

#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> using namespace std; class School { public: School() :_name("清华大学") { cout << "School()" << endl; } public: string _name; }; class Student :public School { public: Student() :_id(20222356) { cout << "Student()" << endl; } protected: //学号 int _id; }; class Teacher :public School { public: Teacher() :_subject("高等数学") { cout << "Teacher()" << endl; } protected: string _subject; }; class Assistant :public Student, public Teacher { public: Assistant() :_majorcourse("离散数学") { cout << "Assistant()" << endl; } protected: string _majorcourse; }; int main() { Assistant a1; return 0; }

7.3.2:菱形继承会造成二义性的问题

#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class School { public: School() :_name("清华大学") { cout << "School" << endl; } public: string _name; }; class Student : public School { public: Student() :_id(20222356) { cout << "Student()" << endl; } protected: //学号 int _id; }; class Teacher :public School { public: Teacher() :_subject("高等数学") { cout << "Teacher" << endl; } protected: string _subject; }; class Assistant :public Student, public Teacher { public: Assistant() :_majorcourse("离散数学") { cout << "Assistant()" << endl; } protected: string _majorcourse; }; int main() { Assistant a1; a1.Student::_name = "北京大学"; a1.Teacher::_name = "兰州大学"; return 0; }

7.3.3:解决菱形继承的数据冗余与二义性问题的方案(虚拟继承)

虚拟继承可以解决菱形继承的二义性与数据冗余问题.在Student和Teacher的继承School时使用虚拟继承,即可解决问题.

PS:虚拟继承不要在其他地方去使用.

#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; //解决菱形继承的数据冗余与二义性问题 class School { public: School() :_name("清华大学") { cout << "School" << endl; } public: string _name; }; class Student : virtual public School { public: Student() :_id(20222356) { cout << "Student()" << endl; } protected: //学号 int _id; }; class Teacher : virtual public School { public: Teacher() :_subject("高等数学") { cout << "Teacher" << endl; } protected: string _subject; }; class Assistant :public Student, public Teacher { public: Assistant() :_majorcourse("离散数学") { cout << "Assistant()" << endl; } protected: string _majorcourse; }; int main() { Assistant a1; a1._name = "北京大学"; return 0; } 

7.4:虚拟继承解决数据冗余和二义性的原理

#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; //解决菱形继承的数据冗余与二义性问题 class A { public: int _a; }; class B :public A { public: int _b; }; class C :public A { public: int _c; }; class D :public B, public C { public: int _d; }; int main() { D d; d.B::_a = 1; d._b = 3; d.C::_a = 2; d._c = 4; d._d = 5; return 0; }

8:继承的反思与总结

public继承是is-a的关系即每一个派生类对象都一个基类对象.组合是一种has-a的关系

优先使用对象组合,而不是类继承.继承允许你根据基类的实现来定义派生类的实现。这种通过生成派生类的复用通常被称 为白箱复用(white-box reuse)。术语“白箱”是相对可视性而言:在继承方式中,基类的内部细节对子类可见,继承一定程度破坏了基类的封装,基类的改变,对派生类有很大的影响。派生类和基类间的依赖关系很强,耦合度高。对象组合是类继承之外的另一种复用选择。新的更复杂的功能可以通过组装或组合对象来获得。对象组合要求被组合的对象具有良好定义的接口。这种复用风格被称为黑箱复用(black-box reuse),因为对象的内部细节是不可见的。对象只以“黑箱”的形式出现,组合类之间没有很强的依赖关系,耦合度低。优先使用对象组合有助于你保持每个类被封装.实际尽量多去用组合组合的耦合度低,代码维护性好.不过继承也有用武之地的,有些关系就适合继承那就用继承,另外要实现多态,也必须要继承。类之间的关系可以用继承,可以用组合,就用组合。

Read more

MCP客户端与服务端初使用——让deepseek调用查询天气的mcp来查询天气

MCP客户端与服务端初使用——让deepseek调用查询天气的mcp来查询天气

本系列主要通过调用天气的mcp server查询天气这个例子来学习什么是mcp,以及怎么设计mcp。话不多说,我们开始吧。主要参考的是B站的老哥做的一个教程,我把链接放到这里,大家如果有什么不懂的也可以去看一下。 https://www.bilibili.com/video/BV1NLXCYTEbj?spm_id_from=333.788.videopod.episodes&vd_source=32148098d54c83926572ec0bab6a3b1d https://blog.ZEEKLOG.net/fufan_LLM/article/details/146377471 最终的效果:让deepseek-v3使用天气查询的工具来查询指定地方的天气情况 技术介绍 MCP,即Model Context Protocol(模型上下文协议),是由Claude的母公司Anthropic在2024年底推出的一项创新技术协议。在它刚问世时,并未引起太多关注,反响较为平淡。然而,随着今年智能体Agent领域的迅猛发展,MCP逐渐进入大众视野并受到广泛关注。今年2月,

By Ne0inhk
可以在命令行通过大模型使用上下文协议(MCP)与外部工具交互的软件:小巧的MCPHost

可以在命令行通过大模型使用上下文协议(MCP)与外部工具交互的软件:小巧的MCPHost

小巧的MCPHost MCPHost 可以在命令行下使用,使大型语言模型(LLM)能够通过模型上下文协议(MCP)与外部工具进行交互。目前支持Claude 3.5 Sonnet和Ollama等。本次实践使用自己架设的Deepseek v3模型,跑通了Time MCP服务。  官网:GitHub - mark3labs/mcphost: A CLI host application that enables Large Language Models (LLMs) to interact with external tools through the Model Context Protocol (MCP). 下载安装 使用非常方便,直接下载解压即可使用。官网提供Windows、Linux和MacOS三个系统的压缩包: https://github.com/

By Ne0inhk
实战篇:Python开发monogod数据库mcp server看完你就会了

实战篇:Python开发monogod数据库mcp server看完你就会了

原创不易,请关注公众号:【爬虫与大模型开发】,大模型的应用开发之路,整理了大模型在现在的企业级应用的实操及大家需要注意的一些AI开发的知识点!持续输出爬虫与大模型的相关文章。 前言 目前mcp协议是给deepseek大模型插上工具链的翅膀,让大模型不仅拥有超高的推理和文本生成能力,还能具备执行大脑意识的工具能力! 如何开发一个mcp? mcp是一种协议,指的是模型上下文协议 (Model Context Protocol)。 官方结成的mcp https://github.com/modelcontextprotocol/python-sdk mcp库 pip install mcp from mcp.server.fastmcp import FastMCP 我们先来做一个简单的案例 from mcp.server.fastmcp import FastMCP import requests mcp = FastMCP("spider") @mcp.tool() def crawl(

By Ne0inhk
【大模型实战篇】基于Claude MCP协议的智能体落地示例

【大模型实战篇】基于Claude MCP协议的智能体落地示例

1. 背景         之前我们在《MCP(Model Context Protocol) 大模型智能体第一个开源标准协议》一文中,介绍了MCP的概念,虽然了解了其概念、架构、解决的问题,但还缺少具体的示例,来帮助进一步理解整套MCP框架如何落地。         今天我们基于claude的官方例子--获取天气预报【1】,来理解MCP落地的整条链路。 2. MCP示例         该案例是构建一个简单的MCP天气预报服务器,并将其连接到主机,即Claude for Desktop。从基本设置开始,然后逐步发展到更复杂的使用场景。         大模型虽然能力非常强,但其弊端就是内容是过时的,这里的过时不是说内容很旧,只是表达内容具有非实时性。比如没有获取天气预报和严重天气警报的能力。因此我们将使用MCP来解决这一问题。         构建一个服务器,该服务器提供两个工具:获取警报(get-alerts)和获取预报(get-forecast)。然后,将该服务器连接到MCP主机(在本例中为Claude for Desktop)。         首先我们配置下环

By Ne0inhk