【C++】类型擦除 + 工厂模式,告别 if-else

文章目录

  • 一、使用std::variant和std::visit
  • 二、使用虚函数
  • 三、使用工厂模式管理第三方库的Msg
  • 四、尾随逗号
    • 1.初始化列表
    • 2.枚举
    • 3.数组
    • 4.结构体或类的成员初始化列表
  • 参考

需求点:假设一个第三方库有提供了很多Msg类,这些Msg类都提供了固定的一个成员函数,但是却没有虚基函数。如何在自己的项目代码中更好的使用这些Msg类内?

一、使用std::variant和std::visit

#include <iostream>
#include <memory>  // for std::unique_ptr and std::make_unique
#include <string>
#include <utility>  // for std::move
#include <variant>
#include <vector>// 外部库 Start
struct MoveMsg {int x;int y;void speak() { std::cout << "Move " << x << ", " << y << '\n'; }
};struct JumpMsg {int height;void speak() { std::cout << "Jump " << height << '\n'; }
};struct SleepMsg {int time;void speak() { std::cout << "Sleep " << time << '\n'; }
};struct ExitMsg {void speak() { std::cout << "Exit" << '\n'; }
};
// 外部库 Endint main() {using Msg = std::variant<MoveMsg, JumpMsg, SleepMsg, ExitMsg>;std::vector<Msg> msgs;msgs.push_back(MoveMsg{1, 2});msgs.push_back(JumpMsg{1});for (auto&& msg : msgs) {std::visit([](auto& msg) { msg.speak(); }, msg);}return 0;
}

二、使用虚函数

C++20语法

#include <iostream>
#include <memory>  // for std::unique_ptr and std::make_unique
#include <string>
#include <utility>  // for std::move
#include <variant>
#include <vector>// 外部库 Start
struct MoveMsg {int x;int y;void speak() { std::cout << "Move " << x << ", " << y << '\n'; }
};struct JumpMsg {int height;void speak() { std::cout << "Jump " << height << '\n'; }void happy() { std::cout << "happy " << height << '\n'; }
};struct SleepMsg {int time;void speak() { std::cout << "Sleep " << time << '\n'; }
};struct ExitMsg {void speak() { std::cout << "Exit" << '\n'; }
};
// 外部库 Endstruct MsgBase {virtual void speak() = 0;virtual void happy() = 0;virtual std::shared_ptr<MsgBase> clone() const = 0;virtual ~MsgBase() = default;
};//内部代码使用MsgBase去包装库的speak()和happy()函数
template <class Msg>
struct MsgImpl : MsgBase {Msg msg;template <class ...Ts>MsgImpl(Ts &&...ts) : msg{std::forward<Ts>(ts)...} {}void speak() override {msg.speak();}void happy() override {if constexpr (requires {msg.happy();} ){msg.happy();}else{std::cout<< "no happy\n";}}std::shared_ptr<MsgBase> clone() const override {return std::make_shared<MsgImpl<Msg>>(msg);}
};template <class Msg, class ...Ts>
std::shared_ptr<MsgBase> makeMsg(Ts &&...ts) {return std::make_shared<MsgImpl<Msg>>(std::forward<Ts>(ts)...);
}int main() {std::vector<std::shared_ptr<MsgBase>> msgs;msgs.push_back(makeMsg<MoveMsg>(1, 2));msgs.push_back(makeMsg<JumpMsg>(1));for (auto&& msg : msgs) {msg->speak(); msg->happy(); }return 0;
}

C++14写法1

#include <iostream>
#include <memory>  // for std::unique_ptr and std::make_unique
#include <string>
#include <utility>  // for std::move
#include <variant>
#include <vector>// 外部库 Start
struct MoveMsg {int x;int y;void speak() { std::cout << "Move " << x << ", " << y << '\n'; }
};struct JumpMsg {int height;void speak() { std::cout << "Jump " << height << '\n'; }void happy() { std::cout << "happy " << height << '\n'; }
};struct SleepMsg {int time;void speak() { std::cout << "Sleep " << time << '\n'; }
};struct ExitMsg {void speak() { std::cout << "Exit" << '\n'; }
};
// 外部库 Endstruct MsgBase {virtual void speak() = 0;virtual void happy() = 0;virtual std::shared_ptr<MsgBase> clone() const = 0;virtual ~MsgBase() = default;
};// 特质,用于检测 happy 方法是否存在
template<typename T>
struct has_happy {
private:typedef char YesType[1];typedef char NoType[2];template <typename C> static YesType& test(decltype(&C::happy));template <typename C> static NoType& test(...);public:enum { value = sizeof(test<T>(0)) == sizeof(YesType) };
};// 函数重载用于调用 happy 或者输出 "no happy"
template <typename T>
typename std::enable_if<has_happy<T>::value>::type call_happy(T& t) {t.happy();
}template <typename T>
typename std::enable_if<!has_happy<T>::value>::type call_happy(T&) {std::cout << "no happy\n";
}// MsgImpl 用于包装库的消息类型
template <class Msg>
struct MsgImpl : MsgBase {Msg msg;template <class ...Ts>MsgImpl(Ts &&...ts) : msg{std::forward<Ts>(ts)...} {}void speak() override {msg.speak();}void happy() override {call_happy(msg);}std::shared_ptr<MsgBase> clone() const override {return std::make_shared<MsgImpl<Msg>>(msg);}
};template <class Msg, class ...Ts>
std::shared_ptr<MsgBase> makeMsg(Ts &&...ts) {return std::make_shared<MsgImpl<Msg>>(std::forward<Ts>(ts)...);
}int main() {std::vector<std::shared_ptr<MsgBase>> msgs;msgs.push_back(makeMsg<MoveMsg>(1, 2));msgs.push_back(makeMsg<JumpMsg>(1));for (auto&& msg : msgs) {msg->speak(); msg->happy(); }return 0;
}

has_happy 模板:

  • has_happy 模板用于检测 Msg 类中是否有 happy 方法。
  • SFINAE 技术用于检查特定方法是否存在。
  • test 函数用于确定 happy 方法的存在性,如果存在,返回 sizeof(char),否则返回 sizeof(int)。

call_happy 函数:

  • 根据 std::integral_constant 的值,选择调用 happy 方法或输出 “no happy”。
  • std::true_type 和 std::false_type 是 std::integral_constant 的特例化,分别表示 true 和 false。

C++14写法2:

#include <iostream>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <type_traits>// 外部库 Start
struct MoveMsg {int x;int y;void speak() { std::cout << "Move " << x << ", " << y << '\n'; }
};struct JumpMsg {int height;void speak() { std::cout << "Jump " << height << '\n'; }void happy() { std::cout << "happy " << height << '\n'; }
};struct SleepMsg {int time;void speak() { std::cout << "Sleep " << time << '\n'; }
};struct ExitMsg {void speak() { std::cout << "Exit" << '\n'; }
};
// 外部库 Endstruct MsgBase {virtual void speak() = 0;virtual void happy() = 0;virtual std::shared_ptr<MsgBase> clone() const = 0;virtual ~MsgBase() = default;
};// 检测 happy 方法存在性的模板
template<typename T>
struct has_happy {// 利用 SFINAE 技术检测 happy 方法template<typename U>static auto test(int) -> decltype(std::declval<U>().happy(), std::true_type{});template<typename>static std::false_type test(...);static constexpr bool value = decltype(test<T>(0))::value;
};// MsgImpl 包装库的消息类型
template <class Msg>
struct MsgImpl : MsgBase {Msg msg;template <class ...Ts>MsgImpl(Ts&&... ts) : msg{std::forward<Ts>(ts)...} {}void speak() override {msg.speak();}void happy() override {call_happy(msg, std::integral_constant<bool, has_happy<Msg>::value>{});}std::shared_ptr<MsgBase> clone() const override {return std::make_shared<MsgImpl<Msg>>(msg);}private:// 当 Msg 有 happy 方法时调用template<typename T>void call_happy(T& t, std::true_type) {t.happy();}// 当 Msg 没有 happy 方法时调用template<typename T>void call_happy(T&, std::false_type) {std::cout << "no happy\n";}
};template <class Msg, class ...Ts>
std::shared_ptr<MsgBase> makeMsg(Ts&&... ts) {return std::make_shared<MsgImpl<Msg>>(std::forward<Ts>(ts)...);
}int main() {std::vector<std::shared_ptr<MsgBase>> msgs;msgs.push_back(makeMsg<MoveMsg>(1, 2));msgs.push_back(makeMsg<JumpMsg>(1));for (auto&& msg : msgs) {msg->speak(); msg->happy(); }return 0;
}

使用特质 has_happy 来检测 happy 方法是否存在。
使用 enable_if 和函数重载来根据特质的值调用不同的实现。

三、使用工厂模式管理第三方库的Msg

需求:假设需要通过不同的输入给这些第三方库的不同Msg构造函数输入值,该怎么办?无法为 Msg 们增加成员函数,只能以重载的形式,外挂追加

#include <iostream>
#include <memory>  // for std::unique_ptr and std::make_unique
#include <string>
#include <utility>  // for std::move
#include <variant>
#include <vector>
#include <map>// 外部库 Start
struct MoveMsg {int x;int y;void speak() { std::cout << "Move " << x << ", " << y << '\n'; }
};struct JumpMsg {int height;void speak() { std::cout << "Jump " << height << '\n'; }void happy() { std::cout << "happy " << height << '\n'; }
};struct SleepMsg {int time;void speak() { std::cout << "Sleep " << time << '\n'; }
};struct ExitMsg {void speak() { std::cout << "Exit" << '\n'; }
};
// 外部库 Endstruct MsgBase {virtual void speak() = 0;virtual void load() = 0;virtual ~MsgBase() = default;using Ptr = std::shared_ptr<MsgBase>;
};// 无法为 Msg们增加成员函数,只能以重载的形式,外挂追加
namespace msg_extra_funcs {  void load(MoveMsg &msg) { std::cin >> msg.x >> msg.y; }void load(JumpMsg &msg) { std::cin >> msg.height; }void load(SleepMsg &msg) { std::cin >> msg.time; }void load(ExitMsg &) {}
}  // namespace msg_extra_funcstemplate <class Msg>
struct MsgImpl : MsgBase {Msg msg;void speak() override { msg.speak(); }void load() override { msg_extra_funcs::load(msg); }
};struct MsgFactoryBase {virtual MsgBase::Ptr create() = 0;virtual ~MsgFactoryBase() = default;using Ptr = std::shared_ptr<MsgFactoryBase>;
};template <class Msg>
struct MsgFactoryImpl : MsgFactoryBase {MsgBase::Ptr create() override { return std::make_shared<MsgImpl<Msg>>(); }
};template <class Msg>
MsgFactoryBase::Ptr makeFactory() {return std::make_shared<MsgFactoryImpl<Msg>>();
}struct RobotClass {inline static const std::map<std::string, MsgFactoryBase::Ptr> factories = {{"Move", makeFactory<MoveMsg>()},{"Jump", makeFactory<JumpMsg>()},{"Sleep", makeFactory<SleepMsg>()},{"Exit", makeFactory<ExitMsg>()},};void recv_data() {std::string type;std::cin >> type;try {//建议使用at,而不是[],是因为后者的key如果不存在,则创建一个value=nullptr的itemmsg = factories.at(type)->create();} catch (std::out_of_range &) {std::cout << "no such msg type!\n";return;}msg->load();}void update() {if (msg) msg->speak();}MsgBase::Ptr msg;
};int main() {RobotClass robot;robot.recv_data();robot.update();return 0;
}

工厂常见使用宏

#include <iostream>
#include <memory>  // for std::unique_ptr and std::make_unique
#include <string>
#include <utility>  // for std::move
#include <variant>
#include <vector>
#include <map>// 外部库 Start
struct MoveMsg {int x;int y;void speak() { std::cout << "Move " << x << ", " << y << '\n'; }
};struct JumpMsg {int height;void speak() { std::cout << "Jump " << height << '\n'; }void happy() { std::cout << "happy " << height << '\n'; }
};struct SleepMsg {int time;void speak() { std::cout << "Sleep " << time << '\n'; }
};struct ExitMsg {void speak() { std::cout << "Exit" << '\n'; }
};
// 外部库 Endstruct MsgBase {virtual void speak() = 0;virtual void load() = 0;virtual ~MsgBase() = default;using Ptr = std::shared_ptr<MsgBase>;
};// 无法为 Msg们增加成员函数,只能以重载的形式,外挂追加
namespace msg_extra_funcs {  void load(MoveMsg &msg) { std::cin >> msg.x >> msg.y; }void load(JumpMsg &msg) { std::cin >> msg.height; }void load(SleepMsg &msg) { std::cin >> msg.time; }void load(ExitMsg &) {}
}  // namespace msg_extra_funcstemplate <class Msg>
struct MsgImpl : MsgBase {Msg msg;void speak() override { msg.speak(); }void load() override { msg_extra_funcs::load(msg); }
};struct MsgFactoryBase {virtual MsgBase::Ptr create() = 0;virtual ~MsgFactoryBase() = default;using Ptr = std::shared_ptr<MsgFactoryBase>;
};template <class Msg>
struct MsgFactoryImpl : MsgFactoryBase {MsgBase::Ptr create() override { return std::make_shared<MsgImpl<Msg>>(); }
};template <class Msg>
MsgFactoryBase::Ptr makeFactory() {return std::make_shared<MsgFactoryImpl<Msg>>();
}struct RobotClass {inline static const std::map<std::string, MsgFactoryBase::Ptr> factories = {//define这里的宏以后,用完就undef掉,否则会如果其他人引入了该头文件,会影响别人的宏
#define PRE_MSG(Type) {#Type, makeFactory<Type##Msg>()},PRE_MSG(Move)PRE_MSG(Move)PRE_MSG(Jump)PRE_MSG(Sleep)PRE_MSG(Exit)
#undef PRE_MSG// {"Move", makeFactory<MoveMsg>()},// {"Jump", makeFactory<JumpMsg>()},// {"Sleep", makeFactory<SleepMsg>()},// {"Exit", makeFactory<ExitMsg>()},};void recv_data() {std::string type;std::cin >> type;try {msg = factories.at(type)->create();} catch (std::out_of_range &) {std::cout << "no such msg type!\n";return;}msg->load();}void update() {if (msg) msg->speak();}MsgBase::Ptr msg;
};int main() {RobotClass robot;robot.recv_data();robot.update();return 0;
}

四、尾随逗号

在C++中,初始化列表中的最后一个元素后面可以有一个逗号,这被称为“尾随逗号”(trailing comma)。这种语法在初始化列表、枚举、和数组中都是允许的。使用尾随逗号有几个好处:

  • 方便添加新元素:当你需要在列表末尾添加新的元素时,不需要修改前一行的逗号。这减少了编辑错误的可能性。
  • 更清晰的版本控制:在使用版本控制系统(如Git)时,如果每一行元素后都有逗号,添加新元素时只会新增一行变更,而不是修改和新增两行。
  • 一致性和可读性:在某些情况下,尾随逗号可以使代码更加一致和易读,尤其是在长列表中。

1.初始化列表

在初始化列表中,你可以在最后一个元素后添加一个逗号。

#include <iostream>
#include <map>
#include <string>int main() {// 使用{}初始化mapstd::map<std::string, int> myMap = {{"apple", 1},{"banana", 2},{"orange", 3}, // 尾随逗号};// 访问map中的元素std::cout << "apple: " << myMap["apple"] << std::endl;std::cout << "banana: " << myMap["banana"] << std::endl;std::cout << "orange: " << myMap["orange"] << std::endl;return 0;
}

2.枚举

在定义枚举类型时,也可以在最后一个枚举成员后加上逗号。

#include <iostream>enum Fruit {Apple,Banana,Orange, // 尾随逗号
};int main() {Fruit myFruit = Apple;std::cout << "Fruit value: " << myFruit << std::endl;return 0;
}

3.数组

在初始化数组时,可以在最后一个元素后添加一个逗号。

#include <iostream>int main() {int myArray[] = {1, 2, 3, 4, 5,}; // 尾随逗号// 访问数组中的元素for (int i = 0; i < 5; ++i) {std::cout << "myArray[" << i << "]: " << myArray[i] << std::endl;}return 0;
}

4.结构体或类的成员初始化列表

在初始化结构体或类的成员时,也可以使用尾随逗号。

#include <iostream>struct Point {int x, y;
};int main() {Point p = {1, 2,}; // 尾随逗号std::cout << "Point: (" << p.x << ", " << p.y << ")" << std::endl;return 0;
}

参考

  • 【C++】类型擦除 + 工厂模式,告别 if-else
  • 代码

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/867837.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

swiftui中常用组件picker的使用,以及它的可选样式

一个可选项列表就是一个picker组件搞出来的&#xff0c;它有多个样式可以选择&#xff0c;并且可以传递进去一些可选数据&#xff0c;有点像前端页面里面的seleted组件&#xff0c;但是picker组件的样式可以更多。可以看官方英文文档&#xff1a;PickerStyle | Apple Developer…

【对接支付宝支付详细流程】

下面示例使用的是支付宝的网页支付&#xff0c;最终的效果如图&#xff1a; 1.前置条件 对接支付宝你需要了解的知识点 1.加密算法 对称加密和非对称加密&#xff0c;RSA2加密算法&#xff0c;签名验证 2.支付宝平台openid unionId的概念 https://opendocs.alipay.com/pre…

文件管理下:文件函数的学习

前言 Hello,小伙伴们你们的作者君又来了&#xff0c;上次我们简单介绍了文件的坐拥并简单提到了数据的读取&#xff0c;和C语言的默认流的作用&#xff0c;今天我将继续带领大家探索文件的奥秘&#xff0c;大家准别好了吗&#xff1f; 在内容开始之前还是按照惯例&#xff0c…

初识java—jdk17的一些新增特性

文章目录 前言一 &#xff1a; yield关键字二 &#xff1a;var关键字三 &#xff1a;密封类四 &#xff1a;空指针异常&#xff1a;五&#xff1a;接口中的私有方法&#xff1a;六&#xff1a;instanceof关键字 前言 这里介绍jdk17相对于jdk1.8的部分新增特性。 一 &#xff…

QT使用QGraphicsView绘图 重写QGraphicsObject类实现点在QPainterPath路径上移动动画效果

闲谈&#xff1a;眨眼间&#xff0c;2024年就过去了一半了&#xff0c;年前定下的计划一个都没完成&#xff0c;乘着有空&#xff0c;把之前学习的内容和示例先总结了。 目录 导读SVG 转QPainterPath 路径获取QPainterPath指定长度时的坐标。重写QGraphicsObject类 实现点图元Q…

stm32精密控制步进电机(基础篇)

众所周知&#xff0c;步进电机由于使用脉冲控制&#xff0c;会比直流电机的控制稍难一些&#xff0c;但开环控制时也更加稳定。 落到做项目的时候&#xff0c;目前来说我都会先考虑步进电机&#xff0c;再去考虑直流&#xff0c;无刷这样的电机。包括毕设时所用的机械臂也是用…

并发处理 优先图和多重图

优先图(Precedence Graph)视图可串性多重图(Polygraph) 优先图(Precedence Graph) 优先图用于冲突可串性的判断。 优先图结构&#xff1a; 结点 (Node)&#xff1a;事务&#xff1b;有向边 (Arc): Ti → Tj &#xff0c;满足 Ti <s Tj&#xff1b; 存在Ti中的操作A1和Tj…

开源全新H5充值系统源码/自定义首页+充值页面/灵活对接上游渠道接口

开源全新H5充值系统源码&#xff0c;系统基于thinkphp框架开发&#xff0c;功能已全完善&#xff0c;可灵活对接其他上游渠道接口&#xff0c;默认对接了大猿人接口&#xff0c;另外可无限制自定义创建充值页面&#xff0c;首页支持后台自定义修改&#xff0c;支持三级分销&…

史上最全的自抗扰控制(ADRC)学习资料

史上最全的自抗扰控制&#xff08;ADRC&#xff09;学习资料 需要的私信我~ 需要的私信我~ 需要的私信我~ ​ 本文将作者近些年来学习ADRC算法的学习资料进行汇总&#xff0c;整理了这一版相对较全的学习资料&#xff0c;包含参考文献以及仿真案例&#xff0c;适合初学者入门&…

6、Redis系统-数据结构-05-整数

五、整数集合&#xff08;Intset&#xff09; 整数集合是 Redis 中 Set 对象的底层实现之一。当一个 Set 对象只包含整数值元素&#xff0c;并且元素数量不大时&#xff0c;就会使用整数集合这个数据结构作为底层实现。整数集合通过紧凑的内存布局和升级机制&#xff0c;实现了…

HiAI Foundation开发平台,加速端侧AI应用的智能革命

如果您是一名开发者&#xff0c;正在寻找一种高效、灵活且易于使用的端侧AI开发框架&#xff0c;那么HarmonyOS SDKHiAI Foundation服务&#xff08;HiAI Foundation Kit&#xff09;就是您的理想选择。 作为一款AI开发框架&#xff0c;HiAI Foundation不仅提供强大的NPU计算能…

直击2024 WAIC现场:关于大模型,热情、焦虑与冷静同在

世博展览馆内人们的热情&#xff0c;与世博中心内参与论坛的人们&#xff0c;心情似乎并不成正比。 展馆内人们看到的大模型加速落地是表象&#xff0c;也是结果&#xff1b;而论坛里的企业家和人工智能学者们则更关注大模型的未来发展方向和商业化进程&#xff0c;以及AI安全…

计算机的错误计算(二十三)

摘要 计算机的错误计算&#xff08;二十二&#xff09;阐明&#xff1a;对于结果应该为 0的算式&#xff0c;即使增加计算精度&#xff0c;也得不出0. 针对 &#xff0c;本节给出一种解决方案。 计算机的错误计算&#xff08;十九&#xff09;展示了计算机对 的错误计算&…

【0基础学爬虫】爬虫框架之 feapder 的使用

前言 大数据时代&#xff0c;各行各业对数据采集的需求日益增多&#xff0c;网络爬虫的运用也更为广泛&#xff0c;越来越多的人开始学习网络爬虫这项技术&#xff0c;K哥爬虫此前已经推出不少爬虫进阶、逆向相关文章&#xff0c;为实现从易到难全方位覆盖&#xff0c;特设【0…

Python遥感开发之时序数据的线性插值

Python遥感开发之时序数据的线性插值 0 历史博客1 实现思路2 代码实现3 效果展示 前言&#xff1a;在遇到空间数据的时候&#xff0c;尤其是哨兵、Landsat或者MODIS数据会出现局部值的空缺&#xff0c;为了解决这些值的空缺&#xff0c;通常采用插值的方法&#xff0c;本博客使…

Python编程学习笔记(1)--- 变量和简单数据类型

1、变量 在学习编程语言之前&#xff0c;所接触的第一个程序&#xff0c;绝大多数都是&#xff1a; print("Hello world!") 接下来尝试使用一个变量。在代码中的开头添加一行代码&#xff0c;并对第二行代码进行修改&#xff0c;如下&#xff1a; message "…

中国星坤X1224系列线对板连接器:小巧稳定,助力物联网终端高效运行

在物联网、电器和消防等领域&#xff0c;终端设备的安全稳定运行至关重要。为了满足这些领域对连接器高可靠性、小巧轻便和耐高温的需求&#xff0c;X1224系列线对板连接器应运而生。这款连接器以其独特的设计和卓越的性能&#xff0c;成为了终端设备中不可或缺的一部分。 一、…

Ubantu22.04 通过FlatPak安装微信

Ubuntu22.04 下使用Flatpak稳定安装微信&#xff01; 国际惯例&#xff0c;废话不多说&#xff0c;先上效果图。为啥使用Flatpak,因为Wechat官方只在FlatPak发布了最新的版本。之前使用了Wine以及Dock安装Wechat,效果都不是很理想&#xff0c;bug很多。所以使用了FlatPak。 Fl…

免费的鼠标连点器电脑版教程!官方正版!专业鼠标连点器用户分享教程!2024最新

电脑技术的不断发展&#xff0c;许多用户在日常工作和娱乐中&#xff0c;需要用到各种辅助工具来提升效率或简化操作&#xff0c;而电脑办公中&#xff0c;鼠标连点器作为一种能够模拟鼠标点击的软件&#xff0c;受到了广大用户的青睐。本文将为大家介绍一款官方正版的免费鼠标…

一.2.(3)放大电路的图解分析方法和微变等效电路分析方法;

放大电路的主要分析方法:图解法、微变等效电路法 这里以共射放大电路为例 (1) 图解法: 1.静态分析 首先确定静态工作点Q,然后根据电路的特点,做出直流负载线,进而画出交流负载线,最后,画出各极电流电压的波形。求出最大不失真输出电压。 估算IBQ&#xff0c;然后根据数据手册里…