当前位置: 首页 > news >正文

C++学习第五天

创作过程中难免有不足,若您发现本文内容有误,恳请不吝赐教。


提示:以下是本篇文章正文内容,下面案例可供参考


一、构造函数

问题1

        关于编译器生成的默认成员函数,很多童鞋会有疑惑:不实现构造函数的情况下,编译器会
生成默认的构造函数。但是看起来默认构造函数又没什么用? d 对象调用了编译器生成的默认构造函数,但是d 对象 _year/_month/_day ,依旧是随机值。也就说在这里 编译器生成的 默认构造函数并没有什么用??
        
        解答:C++ 把类型分成内置类型 ( 基本类型 ) 和自定义类型。内置类型就是语言提供的数据类
型,如: int/char... ,自定义类型就是我们使用 class/struct/union 等自己定义的类型,看看下面的程序,就会发现编译器生成默认的构造函数会对自定类型成员_t 调用的它的默认构造函数。

示例1:

#include<iostream>
using namespace std;class A
{
public:A(int a){cout << "Aint(a)" << endl;_a = a;}
private:int _a;
};class Date
{
public://Date(int year, int month, int day)//{//	_year = year;//	_month = month;//	_day = day;//}
private://内置类型int _year;int _month;int _day;//自定义类型A _aa;
};int main()
{Date d1;
}

class A没有提供默认构造函数(不用参数就可以调用的构造).


示例2:

#include<iostream>
using namespace std;class A
{
public:A(int a = 0){cout << "A int(a)" << endl;_a = a;}
private:int _a;
};class Date
{
public://Date(int year, int month, int day)//{//	_year = year;//	_month = month;//	_day = day;//}
private://内置类型int _year;int _month;int _day;//自定义类型A _aa;
};int main()
{Date d1;
}

如果A不提供默认构造,需要使用初始化列表才能解决。当前的学习暂时先通过提供全缺省( A(int a = 0) )来解决这个问题.


简单来说:我们不写编译器生成的默认构造函数,对内置类型不做处理,对自定义类型会调用它的默认构造.


二、默认构造函数

  无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。 注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认构造函数。

       特点:不传参就可以调用的构造就是默认构造.


三、析构函数

   可以简单理解为与构造函数相反的函数,类比Destroy函数.


日期类不需要写析构函数,因为日期类没有资源需要清理,年、月、日属于对象,不需要清理。而像栈这种才需要清理,有指向资源才需要清理。


示例1:

  内置类型的成员,不做处理,造成堆上的空间没有释放,造成内存泄漏。

#include<iostream>
using namespace std;typedef int DataType;
class Stack
{
public:Stack(size_t capacity = 3){_array = (DataType*)malloc(sizeof(DataType) * capacity);if (NULL == _array){perror("malloc申请空间失败!!!");return;}_capacity = capacity;_size = 0;}void Push(DataType data){_array[_size] = data;_size++;}/*~Stack(){cout << "~Stack()" << endl;free(_array);_array = nullptr;_size = _capacity = 0;}*/
private:// 内置类型DataType* _array;int _capacity;int _size;};void TestStack()
{Stack s;s.Push(1);s.Push(2);
}int main()
{TestStack();return 0;
}

示例2:

自定义类型的成员,会去调用他的析构。

#include<iostream>
using namespace std;class A
{
public:A(int a = 0){cout << "A(int a)" << endl;_a = a;}~A(){cout << "~A()" << endl;}
private:int _a;
};typedef int DataType;
class Stack
{
public:Stack(size_t capacity = 3){_array = (DataType*)malloc(sizeof(DataType) * capacity);if (NULL == _array){perror("malloc申请空间失败!!!");return;}_capacity = capacity;_size = 0;}void Push(DataType data){_array[_size] = data;_size++;}/*~Stack(){cout << "~Stack()" << endl;free(_array);_array = nullptr;_size = _capacity = 0;}*/
private:// 内置类型DataType* _array;int _capacity;int _size;// 自定义类型A _aa;
};void TestStack()
{Stack s;s.Push(1);s.Push(2);
}int main()
{TestStack();return 0;
}


四、拷贝构造

日期类拷贝:

#include<iostream>
using namespace std;
typedef int DataType;
class Stack
{
public:Stack(size_t capacity = 3){_array = (DataType*)malloc(sizeof(DataType) * capacity);if (NULL == _array){perror("malloc申请空间失败!!!");return;}_capacity = capacity;_size = 0;}void Push(DataType data){_array[_size] = data;_size++;}~Stack(){cout << "~Stack()" << endl;free(_array);_array = nullptr;_size = _capacity = 0;}
private:// 内置类型DataType* _array;int _capacity;int _size;
};class Date
{
public:Date(int year, int month, int day){_year = year;_month = month;_day = day;}void Print(){cout << _year << "/" << _month << "/" << _day << "/" << endl;}
private://内置类型int _year;int _month;int _day;
};void func1(Date d)
{d.Print();
}int main()
{Date d1(2025,1,8);func1(d1);return 0;
}


栈拷贝:

#include<iostream>
using namespace std;
typedef int DataType;
class Stack
{
public:Stack(size_t capacity = 3){_array = (DataType*)malloc(sizeof(DataType) * capacity);if (NULL == _array){perror("malloc申请空间失败!!!");return;}_capacity = capacity;_size = 0;}void Push(DataType data){_array[_size] = data;_size++;}~Stack(){cout << "~Stack()" << endl;free(_array);_array = nullptr;_size = _capacity = 0;}
private:// 内置类型DataType* _array;int _capacity;int _size;
};class Date
{
public:Date(int year, int month, int day){_year = year;_month = month;_day = day;}void Print(){cout << _year << "/" << _month << "/" << _day << "/" << endl;}
private://内置类型int _year;int _month;int _day;
};void func1(Date d)
{d.Print();
}void func2(Stack s)
{}int main()
{Date d1(2025,1,8);func1(d1);Stack s1;func2(s1);return 0;
}


解决方法:

void func2(Stack& s)
{//使用引用
}

拷贝构造函数

        定义:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存

在的类类型对象创建新对象时由编译器自动调用
自定义类型传值传参要调用拷贝构造。

特征1:

      拷贝构造函数是构造函数的一个重载形式


特征2:

        拷贝构造函数的参数只有一个必须是类类型对象的引用,使用传值方式编译器直接报错, 因为会引发无穷递归调用。

#include<iostream>
using namespace std;class Date
{
public:Date(int year, int month, int day){cout << "Date(Date& d)" << endl;_year = year;_month = month;_day = day;}Date(Date& d){_year = d._year;_month = d._month;_day = d._day;}void Print(){cout << _year << "/" << _month << "/" << _day << "/" << endl;}
private://内置类型int _year;int _month;int _day;
};int main()
{Date d1(2025,1,8);Date d2(d1);return 0;
}

	Date(Date d){//没有引用会引发无穷递归调用_year = d._year;_month = d._month;_day = d._day;}


#include<iostream>
using namespace std;typedef int DataType;
class Stack
{
public:Stack(size_t capacity = 3){_array = (DataType*)malloc(sizeof(DataType) * capacity);if (NULL == _array){perror("malloc申请空间失败!!!");return;}_capacity = capacity;_size = 0;}Stack(Stack& s){cout << "Stack(Stack& s)" << endl;// 深拷贝_array = (DataType*)malloc(sizeof(DataType) * s._capacity);if (NULL == _array){perror("malloc申请空间失败!!!");return;}memcpy(_array, s._array, sizeof(DataType) * s._size);_size = s._size;_capacity = s._capacity;}void Push(DataType data){_array[_size] = data;_size++;}~Stack(){cout << "~Stack()" << endl;free(_array);_array = nullptr;_size = _capacity = 0;}
private:// 内置类型DataType* _array;int _capacity;int _size;
};void func2(Stack s)
{s.Push(1);
}int main()
{Stack s1;func2(s1);Stack s2(s1);return 0;
}


问题:

        如果不写拷贝构造函数,系统会自动生成吗?

#include<iostream>
using namespace std;class Date
{
public:Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}void Print(){cout << _year << "/" << _month << "/" << _day << endl;}private:int _year;int _month;int _day;
};typedef int DataType;
class Stack
{
public:Stack(size_t capacity = 3){cout << "Stack()" << endl;_array = (DataType*)malloc(sizeof(DataType) * capacity);if (NULL == _array){perror("malloc申请空间失败!!!");return;}_capacity = capacity;_size = 0;}void Push(DataType data){_array[_size] = data;_size++;}~Stack(){cout << "~Stack()" << endl;free(_array);_array = nullptr;_size = _capacity = 0;}
private:DataType* _array;int _capacity;int _size;
};int main()
{Date d1(2025, 1, 1);Date d2 = d1;Stack st1;Stack st2(st1);return 0;
}


特征3:

        若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按 字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。

我们不写,编译默认生成的拷贝构造,跟之前的构造函数特性不一样

1、内置类型, 值拷贝

 2、自定义的类型,调用他的拷贝

 总结:Date不需要我们实现拷贝构造,默认生成就可以用

 Stack需要我们自己实现深拷贝的拷贝构造,默认生成会出问题


MyQueue对于默认生成的几个函数非常受用,人生赢家

#include<iostream>
using namespace std;typedef int DataType;
class Stack
{
public:Stack(size_t capacity = 3){cout << "Stack()" << endl;_array = (DataType*)malloc(sizeof(DataType) * capacity);if (NULL == _array){perror("malloc申请空间失败!!!");return;}_capacity = capacity;_size = 0;}Stack(const Stack& s){cout << "Stack(Stack& s)" << endl;// 深拷贝_array = (DataType*)malloc(sizeof(DataType) * s._capacity);if (NULL == _array){perror("malloc申请空间失败!!!");return;}memcpy(_array, s._array, sizeof(DataType) * s._size);_size = s._size;_capacity = s._capacity;}void Push(DataType data){_array[_size] = data;_size++;}~Stack(){cout << "~Stack()" << endl;free(_array);_array = nullptr;_size = _capacity = 0;}
private:DataType* _array;int _capacity;int _size;
};class MyQueue
{
private:Stack _pushst;Stack _popst;
};int main()
{MyQueue mq1;MyQueue mq2 = mq1;return 0;
}

注意:

	Date(const Date& d){//一般情况都加上constcout << "Date(Date& d)" << endl;_year = d._year;_month = d._month;_day = d._day;}Date(const Date& d){cout << "Date(Date& d)" << endl;//若不小心写反了,很容易忽视这里的错误d._year = _year ;d._month = _month;d._day = _day;}

五、运算符重载

    问题1:

          日期对象如何比较大小?能否用 < .

简单的代码实现:

#include<iostream>
using namespace std;class Date
{
public:Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}void Print(){cout << _year << "/" << _month << "/" << _day << endl;}//private:int _year;int _month;int _day;
};bool DateLess (const Date& x1,const Date& x2)
{if (x1._year < x2._year){return true;}else if (x1._year == x2._year && x1._month < x2._month){return true;}else if (x1._year == x2._year && x1._month == x2._month && x1._day < x2._day){return true;}else{return false;}
}int main()
{Date d1(2025, 1, 1);Date d2(2025, 1, 11);cout<<DateLess(d1, d2)<<endl;return 0;
}


#include<iostream>
using namespace std;class Date
{
public:Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}void Print(){cout << _year << "/" << _month << "/" << _day << endl;}//private:int _year;int _month;int _day;
};bool operator<(const Date& x1, const Date& x2)
{if (x1._year < x2._year){return true;}else if (x1._year == x2._year && x1._month < x2._month){return true;}else if (x1._year == x2._year && x1._month == x2._month && x1._day < x2._day){return true;}else{return false;}
}int main()
{Date d1(2025, 1, 1);Date d2(2025, 1, 11);cout << (d1 < d2) << endl;cout << (operator<(d1, d2)) << endl;return 0;
}

写为成员函数:

// d1 < d2  
// d1.operator<(d2)
//操作数顺序不能随便换
bool operator<(const Date& d){if (_year < d._year){return true;}else if (_year == d._year && _month < d._month){return true;}else if (_year == d._year && _month == d._month && _day < d._day){return true;}else{return false;}}

运算符复用

class Date
{
public:Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}void Print(){cout << _year << "/" << _month << "/" << _day << endl;}bool operator<(const Date& d){if (_year < d._year){return true;}else if (_year == d._year && _month < d._month){return true;}else if (_year == d._year && _month == d._month && _day < d._day){return true;}else{return false;}}bool operator==(const Date& d){return _year == d._year&& _month == d._month&& _day == d._day;}// d1 <= d2bool operator<=(const Date& d){return *this < d || *this == d;}bool operator>(const Date& d){return !(*this <= d);}bool operator>=(const Date& d){return !(*this < d);}bool operator!=(const Date& d){return !(*this == d);}
}

实现日期+运算

#include<iostream>
using namespace std;class Date
{
public:Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}void Print(){cout << _year << "/" << _month << "/" << _day << endl;}int GetMonthDay(int year, int month){int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (month == 2&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){return 29;}return monthArray[month];}//如果写成Date& operator+(int day)会发现Date ret = d1 + 50;中d1也会改变,所以该写成+=Date& operator+=(int day){_day += day;while (_day > GetMonthDay(_year, _month)){// 月进位_day -= GetMonthDay(_year, _month);++_month;// 月满了if (_month == 13){++_year;_month = 1;}}return *this;}Date operator+(int day)   //出了作用域对象不在了不能用引用返回{Date tmp(*this);tmp += day;return tmp;//tmp._day += day;//while (tmp._day > GetMonthDay(tmp._year, tmp._month))//{//	// 月进位//	tmp._day -= GetMonthDay(tmp._year, tmp._month);//	++_month;//	// 月满了//	if (tmp._month == 13)//	{//		++tmp._year;//		tmp._month = 1;//	}//}//return tmp;}private:// 内置类型int _year;int _month;int _day;
};int main()
{Date d1(2023, 7, 21);Date d2(2022, 8, 21);Date ret = d1 + 50;ret.Print();d1.Print();return 0;
}


总结

        以上就是今天要讲的内容,本文仅仅简单介绍了c++的基础知识。

相关文章:

C++学习第五天

创作过程中难免有不足&#xff0c;若您发现本文内容有误&#xff0c;恳请不吝赐教。 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考 一、构造函数 问题1 关于编译器生成的默认成员函数&#xff0c;很多童鞋会有疑惑&#xff1a;不实现构造函数的情况下…...

openharmony标准系统方案之瑞芯微RK3568移植案例

标准系统方案之瑞芯微RK3568移植案例 ​本文章是基于瑞芯微RK3568芯片的DAYU200开发板&#xff0c;进行标准系统相关功能的移植&#xff0c;主要包括产品配置添加&#xff0c;内核启动、升级&#xff0c;音频ADM化&#xff0c;Camera&#xff0c;TP&#xff0c;LCD&#xff0c…...

深入理解 SSH 端口转发:本地 vs 远程 vs 动态转发

&#x1f31f; 简介 SSH 端口转发&#xff08;SSH Port Forwarding&#xff09;作为一种强大而灵活的技术&#xff0c;不仅可以帮助我们安全地访问远程服务&#xff0c;还能轻松突破网络限制。本文将带你深入了解 SSH 端口转发的原理、类型和实战应用。 &#x1f308; 目录 &a…...

postman请求参数化

postman界面介绍 一、使用环境变量(Environment Variables)进行参数化 1、在请求中使用环境变量 在请求的url、请求头(Headers)、请求体(Body)等部分都可以使用环境变量。 URL 部分示例 点击 Postman 界面右上角的 “眼睛” 图标(Environment Quick Look)打开环境管理…...

基于 WEB 开发的汽车养护系统设计与实现

标题:基于 WEB 开发的汽车养护系统设计与实现 内容:1.摘要 本文介绍了基于 WEB 开发的汽车养护系统的设计与实现。文章首先阐述了系统的背景和目的&#xff0c;即随着汽车保有量的增加&#xff0c;汽车养护需求日益增长&#xff0c;传统的汽车养护方式已经无法满足人们的需求&…...

Nginx正向代理配置

Nginx 正向代理默认只支持 http 协议&#xff0c;不支持 https 协议&#xff0c;需借助 "ngx_http_proxy_connect_module" 模块实现 https 正向代理&#xff0c;详情请参考&#xff1a; https://github.com/chobits/ngx_http_proxy_connect_module 安装Nginx某些模块…...

本地仓库管理之当前分支内的操作

以刚搭建好的git仓库为例&#xff0c;刚搭建完的仓库只有master分支&#xff0c;使用git branch查看当前的分支情况。 elfubuntu:~/work/example/hello$ git branch *所在分支为当前分支&#xff0c;即master分支 当前分支进行源码修改时简单流程图如下&#xff1a; 在当前分…...

《内网穿透:网络拓展与安全防护的平衡艺术》

一、引言&#xff1a;开启内网穿透的大门 在当今数字化浪潮席卷全球的时代&#xff0c;网络已成为人们生活和工作中不可或缺的一部分。我们日常使用的网络&#xff0c;如同一个庞大而复杂的生态系统&#xff0c;其中内网和外网犹如两个相互关联却又有所区别的世界。 想象一下…...

【python写个可以运行的2048小游戏】

写一个完整的2048小游戏在Python中是一个相对复杂的任务&#xff0c;因为它涉及到用户界面、事件处理、游戏逻辑等多个方面。为了简化这个过程&#xff0c;我们可以使用Python的图形库&#xff0c;比如pygame。下面是一个简单的2048游戏的实现。 首先&#xff0c;确保你已经安…...

【Flink系列】9. Flink容错机制

9. 容错机制 在Flink中&#xff0c;有一套完整的容错机制来保证故障后的恢复&#xff0c;其中最重要的就是检查点。 9.1 检查点&#xff08;Checkpoint&#xff09; 9.1.1 检查点的保存 1&#xff09;周期性的触发保存 “随时存档”确实恢复起来方便&#xff0c;可是需要我…...

DETR论文阅读

1. 动机 传统的目标检测任务需要大量的人工先验知识&#xff0c;例如预定义的先验anchor&#xff0c;NMS后处理策略等。这些人工先验知识引入了很多人为因素&#xff0c;且较难处理。如果能够端到端到直接生成目标检测结果&#xff0c;将会使问题变得很优雅。 2. 主要贡献 提…...

关于vite+vue3+ts项目中env.d.ts 文件详解

env.d.ts 文件是 Vite 项目中用于定义全局类型声明的 TypeScript 文件。它帮助开发者向 TypeScript提供全局的类型提示&#xff0c;特别是在使用一些特定于 Vite 的功能时&#xff08;如 import.meta.env&#xff09;。以下是详细讲解及代码示例 文章目录 **1. env.d.ts 文件的…...

如何优化Elasticsearch大文档查询?

记录一次业务复杂场景下DSL优化的过程 背景 B端商城业务有一个场景就是客户可见的产品列表是需要N多闸口及各种其它逻辑组合过滤的&#xff0c;各种闸口数据及产品数据都是存储在ES的(有的是独立索引&#xff0c;有的是作为产品属性存储在产品文档上)。 在实际使用的过程中&a…...

Kotlin Bytedeco OpenCV 图像图像54 透视变换 图像矫正

Kotlin Bytedeco OpenCV 图像图像54 透视变换 图像矫正 1 添加依赖2 测试代码3 测试结果 在OpenCV中&#xff0c;仿射变换&#xff08;Affine Transformation&#xff09;和透视变换&#xff08;Perspective Transformation&#xff09;是两种常用的图像几何变换方法。 变换方…...

Linux中DataX使用第一期

简介 DataX 是阿里云 DataWorks数据集成 的开源版本&#xff0c;在阿里巴巴集团内被广泛使用的离线数据同步工具/平台。DataX 实现了包括 MySQL、Oracle、OceanBase、SqlServer、Postgre、HDFS、Hive、ADS、HBase、TableStore(OTS)、MaxCompute(ODPS)、Hologres、DRDS, databen…...

[Qt]事件-鼠标事件、键盘事件、定时器事件、窗口改变事件、事件分发器与事件过滤器

目录 前言&#xff1a;Qt与操作系统的关系 一、Qt事件 1.事件介绍 2.事件的表现形式 常见的Qt事件&#xff1a; 常见的事件描述: 3.事件的处理方式 处理鼠标进入和离开事件案例 控件添加到对象树底层原理 二、鼠标事件 1.鼠标按下和释放事件&#xff08;单击&#x…...

关于机器学习的一份总结

在之前的文章中分别有详细的关于机器学习中某一学习算法的介绍&#xff0c;但缺少一个总体关于机器学习的总结&#xff0c;所以在这篇文中就是关于机器学习的一份总结。 在最近的日子中&#xff0c;人工智能日益火热起来&#xff0c;而机器学习是其中举足轻重的一部分&#xf…...

推荐一个开源的轻量级任务调度器!TaskScheduler!

大家好&#xff0c;我是麦鸽。 这次推荐一款轻量级的嵌入式任务调度器&#xff0c;目前已经有1.4K的star&#xff0c;这个项目比较轻量化&#xff0c;只有5个源文件&#xff0c;可以作为学习的一个开源项目。 核心文件 项目概述&#xff1a; 这是一个轻量级的协作式多任务处理&…...

【18】Word:明华中学-儿童医保❗

目录 题目​ NO2 NO3 NO4 NO5 NO6 NO7 NO8 NO9 题目 NO2 布局→页面设置对话框→纸张方向&#xff1a;横向→纸张大小&#xff1a;A3 &#xff1b;页面设置对话框&#xff1a;直接输入纸张大小的宽度和高度即可→页面设置对话框&#xff1a;上下左右边距→版式&…...

如何用selenium来链接并打开比特浏览器进行自动化操作(1)

前言 本文是该专栏的第76篇,后面会持续分享python爬虫干货知识,记得关注。 本文,笔者将基于“比特浏览器”,通过selenium来实现链接并打开比特浏览器,进行相关的“自动化”操作。 值得一提的是,在本专栏之前,笔者有详细介绍过“使用selenium或者pyppeteer(puppeteer)…...

Go 语言接口详解

Go 语言接口详解 核心概念 接口定义 在 Go 语言中&#xff0c;接口是一种抽象类型&#xff0c;它定义了一组方法的集合&#xff1a; // 定义接口 type Shape interface {Area() float64Perimeter() float64 } 接口实现 Go 接口的实现是隐式的&#xff1a; // 矩形结构体…...

基础测试工具使用经验

背景 vtune&#xff0c;perf, nsight system等基础测试工具&#xff0c;都是用过的&#xff0c;但是没有记录&#xff0c;都逐渐忘了。所以写这篇博客总结记录一下&#xff0c;只要以后发现新的用法&#xff0c;就记得来编辑补充一下 perf 比较基础的用法&#xff1a; 先改这…...

Mac软件卸载指南,简单易懂!

刚和Adobe分手&#xff0c;它却总在Library里给你写"回忆录"&#xff1f;卸载的Final Cut Pro像电子幽灵般阴魂不散&#xff1f;总是会有残留文件&#xff0c;别慌&#xff01;这份Mac软件卸载指南&#xff0c;将用最硬核的方式教你"数字分手术"&#xff0…...

Robots.txt 文件

什么是robots.txt&#xff1f; robots.txt 是一个位于网站根目录下的文本文件&#xff08;如&#xff1a;https://example.com/robots.txt&#xff09;&#xff0c;它用于指导网络爬虫&#xff08;如搜索引擎的蜘蛛程序&#xff09;如何抓取该网站的内容。这个文件遵循 Robots…...

Android15默认授权浮窗权限

我们经常有那种需求&#xff0c;客户需要定制的apk集成在ROM中&#xff0c;并且默认授予其【显示在其他应用的上层】权限&#xff0c;也就是我们常说的浮窗权限&#xff0c;那么我们就可以通过以下方法在wms、ams等系统服务的systemReady()方法中调用即可实现预置应用默认授权浮…...

Java线上CPU飙高问题排查全指南

一、引言 在Java应用的线上运行环境中&#xff0c;CPU飙高是一个常见且棘手的性能问题。当系统出现CPU飙高时&#xff0c;通常会导致应用响应缓慢&#xff0c;甚至服务不可用&#xff0c;严重影响用户体验和业务运行。因此&#xff0c;掌握一套科学有效的CPU飙高问题排查方法&…...

【从零学习JVM|第三篇】类的生命周期(高频面试题)

前言&#xff1a; 在Java编程中&#xff0c;类的生命周期是指类从被加载到内存中开始&#xff0c;到被卸载出内存为止的整个过程。了解类的生命周期对于理解Java程序的运行机制以及性能优化非常重要。本文会深入探寻类的生命周期&#xff0c;让读者对此有深刻印象。 目录 ​…...

Java数值运算常见陷阱与规避方法

整数除法中的舍入问题 问题现象 当开发者预期进行浮点除法却误用整数除法时,会出现小数部分被截断的情况。典型错误模式如下: void process(int value) {double half = value / 2; // 整数除法导致截断// 使用half变量 }此时...

STM32HAL库USART源代码解析及应用

STM32HAL库USART源代码解析 前言STM32CubeIDE配置串口USART和UART的选择使用模式参数设置GPIO配置DMA配置中断配置硬件流控制使能生成代码解析和使用方法串口初始化__UART_HandleTypeDef结构体浅析HAL库代码实际使用方法使用轮询方式发送使用轮询方式接收使用中断方式发送使用中…...

[ACTF2020 新生赛]Include 1(php://filter伪协议)

题目 做法 启动靶机&#xff0c;点进去 点进去 查看URL&#xff0c;有 ?fileflag.php说明存在文件包含&#xff0c;原理是php://filter 协议 当它与包含函数结合时&#xff0c;php://filter流会被当作php文件执行。 用php://filter加编码&#xff0c;能让PHP把文件内容…...