Java基础之常用类

Java基础之常用类

  • 一、包装类
    • 1.1、Java基本数据类型及其对应的包装类
    • 1.2、包装类的自动装箱、自动拆箱机制
    • 1.3、包装类的优点
  • 二、String类
  • 三、StringBuffer类和StringBuilder类
    • 3.1、主要区别:
    • 3.2、StringBuffer/StringBuilder用法(两者用法一致)
  • 四、日期类
    • 4.1、Date类
    • 4.2、SimpleDateFormate和DateFormate类
    • 4.3、Calender类日期类
  • 五、JDK8日期类
    • 5.1、LocalDate
    • 5.2、LocalTime
    • 5.3、LocalDateTime
    • 5.4、ZoneDateTime
  • 六、Math类和Random类
    • 6.1、Math类
    • 6.2、Random类
  • 七、枚举类
  • 八、System类

一、包装类

Java包装类(Wrapper Classes)是一组用于将基本数据类型转换为对象的类。均位于java.lang包内

1.1、Java基本数据类型及其对应的包装类

基本数据类型包装类
整数类型byteByte
shortShort
intInteger
longLong
浮点类型floatFloat
doubleDouble
字符类型charCharacter
布尔类型booleanBoolean

1.2、包装类的自动装箱、自动拆箱机制

  • 创建一个包装类
Integer num = new Integer(0);    //创建一个数值为0的Integer对象
  • 基本类型数据和包装类对象互换
Integer num1= new Integer(8);//基本数据类型-->包装类
int num2=num1.intValue();//包装类转换成基本数据类型
System.out.println(num1+" "+num2);//1、包装类中的自动装箱拆箱机制
Integer  num1 = 1;		//自动装箱
int num2 = num1;		//自动拆箱
System.out.println(num1 +"	"+ num2);

1.3、包装类的优点

  • 某些方法参数必须为对象,包装类使数据类型转换成对象
  • 获得类型的最大值:Integer.MAX_VALUE
  • 最小值Integer.MIN_VALUE
  • 将一个类型转为另一个类型integer.doubleValue()
  • 将字符串转化为数字Integer.parseInt(“100”)
  • 将数字转化为字符串Integer.toString(100)

二、String类

String 是 Java 中的一个类,用于表示字符串。在 Java 中,字符串是不可变的,这意味着一旦创建了一个字符串对象,就不能再修改它的内容。String 类位于 java.lang 包中,因此在大多数情况下不需要显式导入。

import java.util.Arrays;
import java.util.StringTokenizer;/*** @BelongsProject: Test* @BelongsPackage: FaceObject* @Author: Jorya* @CreateTime: 2023-11-21  22:26* @Description: TODO* @Version: 1.0*/
public class TestString {public static void main (String[] args){//①字符串的创建// 使用字面值创建字符串String str1 = "Hello, World!";String str2 = "Hello, World!";//这两个的地址是一样的,并没有重新生成一个字符串System.out.println(str1==str2);//true// 使用构造函数创建字符串String str3 = new String("Hello, World!");String str4 = new String("Hello, World!");System.out.println(str3==str4);//判断是否全部一样,因为是不同对象。FalseSystem.out.println(str3.equals(str4));//equals是判断内容是否相等:trueSystem.out.println(str3.equalsIgnoreCase(str4));// 忽略大小写比较System.out.println(str3.compareTo(str4));//判断字符串是否相等,区分大小写//②字符串的链接// 使用加号进行字符串连接String fullName = str1 + " " + str2;// 使用 concat 方法String fullNameConcat = str1.concat(" ").concat(str2);System.out.println(fullName);System.out.println(str1.length());//获取字符串长度System.out.println(str1.isEmpty());//判断字符串是否为空System.out.println(str1.endsWith("!"));//判断字符串是否以!结尾System.out.println(str1.startsWith("H"));//判断字符串是否以H开始System.out.println(str1.toUpperCase());//全部转换为大写System.out.println(str1.toLowerCase());//全部转换为小写System.out.println(str1.charAt(2));//获得字符串的第3个字符l;java的下标是从0开始的System.out.println(str1.toCharArray());//字符串转数组System.out.println(str1.indexOf("l"));//查找字符串第一次出现的位置System.out.println(str1.indexOf("l",2));//从指定位置查找字符串第一次出现的位置System.out.println(str1.lastIndexOf("o"));//从后往前查找字符串第一次出现的位置System.out.println(str1.substring(2));//从指定位置截取字符串System.out.println(str1.substring(2,5));//截取中间某处的字符串System.out.println(str1.substring(7)); // 从索引 7 开始截取到字符串末尾System.out.println(str1.contains("World")); //检查字符串包括System.out.println(str1.replace("World", "Java"));  //字符串替换//字符串分割String[] split = str1.split(" ");//按指定字符串分割字符串System.out.println(Arrays.toString(split));//去掉首尾空格String str5 = "   Trim Me   ";String trimmedStr = str5.trim();//字符串格式化String name = "John";int age = 25;String formattedString = String.format("Name: %s, Age: %d", name, age);}
}

三、StringBuffer类和StringBuilder类

StringBuffer和StringBuilder类用法几乎一模一样,都为抽象类、是AbstractStringBuilder的子类

3.1、主要区别:

StringBufferStringBuilder
线程安全线程不安全
效率较慢效率快、建议使用
方法同步、适用于多线程不提供同步、适用于单线程
//比较String、StringBuilder、StringBuffer的运行时间
public class StringBufferTest {public static void main(String[] args) {long startTime, endTime;// 使用 String 拼接String s1 = "";startTime = System.currentTimeMillis();for (int i = 0; i < 100000; i++) {s1 = s1.concat("拼接");}endTime = System.currentTimeMillis();System.out.println("String 用时: " + (endTime - startTime) + "ms,长度:" + s1.length());// 使用 StringBuilder 拼接StringBuilder s2 = new StringBuilder();startTime = System.currentTimeMillis();for (int i = 0; i < 1000000; i++) {s2.append("拼接");}endTime = System.currentTimeMillis();System.out.println("StringBuilder 用时: " + (endTime - startTime) + "ms,长度:" + s2.length());// 使用 StringBuffer 拼接StringBuffer s3 = new StringBuffer();startTime = System.currentTimeMillis();for (int i = 0; i < 1000000; i++) {s3.append("拼接");}endTime = System.currentTimeMillis();System.out.println("StringBuffer 用时: " + (endTime - startTime) + "ms,长度:" + s3.length());}
}

在这里插入图片描述

3.2、StringBuffer/StringBuilder用法(两者用法一致)

// 创建一个空的 StringBuffer
StringBuffer buffer = new StringBuffer();
// 添加字符串
buffer.append("Hello");
// 在特定位置插入字符串
buffer.insert(5, ", World!");
// 替换字符串
buffer.replace(0, 5, "Hi");
// 删除字符串
buffer.delete(3, 6);
// 反转字符串
buffer.reverse();
// 获取字符串长度
int length = buffer.length();
// 转换为字符串
String result = buffer.toString();

四、日期类

4.1、Date类

在 Java 中,Date 类用于表示日期和时间。请注意,Date类在 Java 8 之后已经过时,更推荐使用 java.time 包中的新日期和时间 API(java.time 包中的类,如 LocalDateLocalTimeLocalDateTime

import java.util.Date;
//日期类
public class TestDate {public static void main(String[] args) {Date currentDate = new Date();System.out.println(currentDate);System.out.println(currentDate.getTime());//获取1970年1月1日到现在的毫秒数System.out.println(currentDate.toLocaleString());//获取当前时间的字符串int year = currentDate.getYear() + 1900;        // 获取年份(从1900开始)int month = currentDate.getMonth() + 1; // 获取月份(从0开始,需要加1)int day = currentDate.getDate();// 获取日期// 获取小时、分钟、秒int hours = currentDate.getHours();int minutes = currentDate.getMinutes();int seconds = currentDate.getSeconds();java.sql.Date date1 = new java.sql.Date(System.currentTimeMillis());System.out.println(date1);//只有年月日String t="2019-8-20";java.sql.Date date2 = java.sql.Date.valueOf(t);//将字符串转化为日期System.out.println(date2);}
}

4.2、SimpleDateFormate和DateFormate类

import java.text.DateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;
//日期类
public class TestDate {public static void main(String[] args) {Date currentDate = new Date();// 创建 SimpleDateFormat 对象并指定日期格式SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化日期String formattedDate = sdf.format(currentDate);System.out.println("SimpleDateFormat Date: " + formattedDate);//创建格式化日期格式为:"yyyy-MM-dd hh:mm:ss";因为DateFormat是一个抽象类不能new对象,所以new一个他的子类;这叫多态DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String format = dateFormat.format(currentDate);//格式化这个日期类,返回String字符串System.out.println("DateFormat Date: "+format);//2022-05-16 10:03:29}
}

SimpleDateFormat sdf = new SimpleDateFormat(“日期格式字符串”);
日期格式字符串,如:“yyyy-MM-dd hh:mm:ss”
yyyy 代表4位的年份
MM 代表2位的月份
dd 代表2位的日期
hh/HH 12小时制/24小时制
mm 代表分钟
ss 代表秒钟
a 代表AM/PM

4.3、Calender类日期类

import java.text.SimpleDateFormat;
import java.util.*;public class TestCalendar {public static void main(String[] args) {//Calendar这也是一个抽象类,需要new他的子类Calendar calendar = new GregorianCalendar();System.out.println(calendar);System.out.println(calendar.get(Calendar.YEAR));//获取年月日System.out.println(calendar.get(Calendar.MARCH)+1);//月是从0开始的System.out.println(calendar.get(Calendar.DATE));System.out.println(calendar.getActualMaximum(Calendar.DATE));//获取当前月最大天数//日期类和日历类相互转化// 1. Date 转 CalendarDate currentDate = new Date();Calendar calendarFrom = Calendar.getInstance();calendarFrom.setTime(currentDate);// 输出 Calendar 对象的日期和时间System.out.println("1. Calendar from Date:");printCalendar(calendarFrom);// 2. Calendar 转 DateCalendar calendarTo = Calendar.getInstance();calendarTo.set(2023, Calendar.JANUARY, 1); // 2023-01-01Date date = calendarTo.getTime();// 输出转换后的 Date 对象SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println("\n2. Date from Calendar: " + sdf.format(date));}private static void printCalendar(Calendar calendar) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println("  Time: " + sdf.format(calendar.getTime()));}
}

五、JDK8日期类

​ 在 Java 8 中,引入了新的日期和时间 API,这个 API 位于 java.time 包中。以下是一些 Java 8 中日期和时间 API 的常用类:

属性含义
Instant代表的是时间相当于Date
LocalDate代表日期,比如2022-01-14
LocalTime代表时间,比如12:20:30
LocalDateTime代表具体时间,比如2022-01-14 12:20:30
ZonedDateTime代表一个包含时区的完整的日期时间
DateTimeFormatter日期和字符串格式转换

5.1、LocalDate

import java.time.LocalDate;public class LocalDateExample {public static void main(String[] args) {// 获取当前日期LocalDate currentDate = LocalDate.now();// 创建指定日期LocalDate specificDate = LocalDate.of(2023, 1, 1);// 获取日期属性int year = specificDate.getYear();int month = specificDate.getMonthValue();int dayOfMonth = specificDate.getDayOfMonth();int dayOfWeek = specificDate.getDayOfWeek().getValue();int dayOfYear = specificDate.getDayOfYear();// 在当前日期基础上加上一天LocalDate nextDay = currentDate.plusDays(1);// 在当前日期基础上减去一周LocalDate lastWeek = currentDate.minusWeeks(1);}
}

5.2、LocalTime

import java.time.LocalTime;public class LocalTimeExample {public static void main(String[] args) {// 获取当前时间LocalTime currentTime = LocalTime.now();// 创建指定时间LocalTime specificTime = LocalTime.of(12, 30, 45);// 获取时间属性int hour = specificTime.getHour();int minute = specificTime.getMinute();int second = specificTime.getSecond();int nano = specificTime.getNano();// 在当前时间基础上加上一小时LocalTime nextHour = currentTime.plusHours(1);// 在当前时间基础上减去半小时LocalTime halfHourAgo = currentTime.minusMinutes(30);}
}

5.3、LocalDateTime

import java.time.LocalDateTime;public class LocalDateTimeExample {public static void main(String[] args) {// 获取当前日期和时间LocalDateTime currentDateTime = LocalDateTime.now();// 创建指定日期和时间LocalDateTime specificDateTime = LocalDateTime.of(2023, 1, 1, 12, 30, 45);// 在当前日期和时间基础上加上一天LocalDateTime nextDay = currentDateTime.plusDays(1);// 在当前日期和时间基础上减去一周LocalDateTime lastWeek = currentDateTime.minusWeeks(1);}
}

5.4、ZoneDateTime

import java.time.ZonedDateTime;
import java.time.ZoneId;public class ZonedDateTimeExample {public static void main(String[] args) {// 获取当前日期和时间(带时区信息)ZonedDateTime currentDateTimeWithZone = ZonedDateTime.now();// 创建指定日期和时间(带时区信息)ZonedDateTime specificDateTimeWithZone = ZonedDateTime.of(2023, 1, 1, 12, 30, 45, 0, ZoneId.of("America/New_York"));// 在当前日期和时间基础上加上一天(带时区信息)ZonedDateTime nextDayWithZone = currentDateTimeWithZone.plusDays(1);// 在当前日期和时间基础上减去一周(带时区信息)ZonedDateTime lastWeekWithZone = currentDateTimeWithZone.minusWeeks(1);}
}

六、Math类和Random类

6.1、Math类

java.lang.Math 类包含用于执行基本数学运算的静态方法。这些方法提供了对常见数学函数的访问,如三角函数、指数函数、对数函数等。

public class MathExample {public static void main(String[] args) {// 基本数学运算int sum = Math.addExact(5, 3); // 返回两个整数的和,溢出时抛出异常。int difference = Math.subtractExact(8, 3); // 返回两个整数的差int product = Math.multiplyExact(4, 7); //返回两个整数的积int incremented = Math.incrementExact(10); // 返回参数的值加 1int decremented = Math.decrementExact(7); //返回参数的值减 1int negated = Math.negateExact(9); // 返回参数的负值// 取整和舍入double ceilValue = Math.ceil(3.14); // 4.0 返回不小于参数的最小整数double floorValue = Math.floor(3.14); // 3.0 返回不大于参数的最大整数long roundedValue = Math.round(3.14); // 3 返回参数最接近的整数// 指数和对数double expValue = Math.exp(2.0); // 7.3890560989306495  返回自然数底数 e 的指数double logValue = Math.log(10.0); // 2.302585092994046   返回参数的自然对数double powValue = Math.pow(2.0, 3.0); // 8.0  返回 a 的 b 次幂// 三角函数double sinValue = Math.sin(Math.toRadians(30)); // 0.49999999999999994 返回角的正弦double cosValue = Math.cos(Math.toRadians(60)); // 0.5000000000000001返回角的余弦double tanValue = Math.tan(Math.toRadians(45)); // 0.9999999999999999 返回角的正切}
}

6.2、Random类

  • 构造

    Random random =new Random();
    
  • 获得随机数

    Random.nextInt(100);
    

    代码示例:

    import java.util.Random;
    public class TestRandom {public static void main(String[] args) {Random random = new Random();System.out.println(random.nextInt(100));//获取100以内的随机整数System.out.println(random.nextFloat());//获取浮点随机}
    }
    

七、枚举类

所有的枚举类型隐性地继承自java.lang.Enum, 枚举的实质还是类。

代码示例:

// 定义一个简单的枚举类型
enum Day {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}public class EnumExample {public static void main(String[] args) {// 使用枚举常量Day today = Day.MONDAY;System.out.println("Today is " + today);// 枚举常量的比较if (today == Day.MONDAY) {System.out.println("It's a workday.");}// 遍历枚举值System.out.println("Days of the week:");for (Day day : Day.values()) {System.out.println(day);}}
}

八、System类

  • System.in : 标准输入流
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
  • System.exit(int status) :退出程序
System.exit(0); // 正常退出
System.exit(1); // 异常退出
  • System.currentTimeMillis() :获取当前时间毫秒数
long currentTimeMillis = System.currentTimeMillis();
System.out.println("Current time in milliseconds: " + currentTimeMillis);

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

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

相关文章

setLineWrapMode 是 QTextEdit 类的成员函数,用于设置文本换行模式(Line Wrap Mode)

setLineWrapMode 是 QTextEdit 类的成员函数&#xff0c;用于设置文本换行模式&#xff08;Line Wrap Mode&#xff09;。 在 Qt 中&#xff0c;文本换行模式指定了文本编辑器中长行文本的显示方式。通过设置不同的换行模式&#xff0c;可以控制是否自动换行、如何换行以及是否…

[DASCTF 2023 0X401七月暑期挑战赛] web刷题记录

文章目录 EzFlask方法一 python原型链污染方法二 flask框架静态文件方法三 pin码计算 MyPicDisk方法一 字符串拼接执行命令方法二 phar反序列化 ez_cms EzFlask 考点&#xff1a;python原型链污染、flask框架理解、pin码计算 源码如下 import uuidfrom flask import Flask, re…

企业源代码防泄密的有什么痛点及难点?

安秉信息作为源代码防泄密的方案的提供商&#xff0c;对企业源代码防泄密有深入的了解。在企业中可以对普通 的文件&#xff0c;图纸进行加密保护这些文件的泄漏。但是企业管理者对于企业的源代码文件防泄密却没有更好的管理方案。 源代码防泄密最大的痛点是&#xff0c;现在企…

近五年—中国十大科技进展(2018年—2022年)

近五年—中国十大科技进展&#xff08;2018-2022&#xff09; 2022年中国十大科技进展1. 中国天眼FAST取得系列重要进展2. 中国空间站完成在轨建造并取得一系列重大进展3. 我国科学家发现玉米和水稻增产关键基因4. 科学家首次发现并证实玻色子奇异金属5. 我国科学家将二氧化碳人…

uni-app 微信小程序 pdf预览

<div click"getPDF">查看体检报告</div>getPDF() {uni.downloadFile({url: ${this.$baseURL}/file/download?id${this.pdfFileId},//编写在线的pdf地址success: function(res) {const filePath res.tempFilePath;uni.openDocument({filePath: filePath…

java stream流map和flatmap的区别

map和flatmap都是用来转换操作。 map()操作后的流与原始流的元素个数一一对应&#xff0c;一对一地进行元素转换&#xff0c;适用于对每个元素进行简单的转换操作&#xff0c;例如将元素的属性提取出来或进行数值计算。 flatMap()操作是一对多的元素转换&#xff0c;对于每个输…

Roll-A-Ball 游戏

Roll-A-Ball 游戏 1&#xff09;学习资料 b站视频教程&#xff1a;https://www.bilibili.com/video/BV18W411671S/文档&#xff1a; * Roll-A-Ball 教程&#xff08;一)&#xff0c; * Roll-A-Ball 教程&#xff08;二)线上体验roll-a-ball成品 * http://www-personal.umich.e…

使用opencv将sRGB格式的图片转换为BT.2020格式【sRGB】【BT.2020】

将sRGB格式的图片转换为BT.2020格式涉及到两个步骤&#xff1a;首先将sRGB转换到线性RGB&#xff0c;然后将线性RGB转换到BT.2020。这是因为sRGB图像通常使用伽马校正&#xff0c;而BT.2020工作在线性色彩空间中。 从sRGB到线性RGB&#xff1a;sRGB图像首先需要进行伽马校正解码…

Spring面向切面编程(AOP);Spring控制反转(IOC);解释一下Spring AOP里面的几个名词;Spring 的 IoC支持哪些功能

文章目录 Spring面向切面编程(AOP)什么是AOPSpring AOP and AspectJ AOP 的区别&#xff1f;Spring AOP中的动态代理如何理解 Spring 中的代理&#xff1f;解释一下Spring AOP里面的几个名词Spring在运行时通知对象Spring切面可以应用5种类型的通知&#xff1a;什么是切面 Aspe…

scoop bucket qq脚本分析(qq绿色安装包制作)

url字段 以qq.json为例&#xff0c;其对应的scoop配置文件在$env:scoop\buckets\extras\bucket\qq.json 其中的url字段 "url":"https://webcdn.m.qq.com/spcmgr/download/QQ9.7.17.29230.exe#/dl.7z"为qq安装包下载地址&#xff0c;后缀#/dl.7z为自行添加…

第八节HarmonyOS @Component自定义组件的生命周期

在开始之前&#xff0c;我们先明确自定义组件和页面的关系&#xff1a; 1、自定义组件&#xff1a;Component装饰的UI单元&#xff0c;可以组合多个系统组件实现UI的复用。 2、页面&#xff1a;即应用的UI页面。可以由一个或者多个自定义组件组成&#xff0c;Entry装饰的自定…

函数递归所应满足的条件

1.递归的概念 递归是学习C语⾔函数绕不开的⼀个话题&#xff0c;那什么是递归呢&#xff1f; 递归其实是⼀种解决问题的⽅法&#xff0c;在C语⾔中&#xff0c;递归就是函数⾃⼰调⽤⾃⼰。 递归的思想&#xff1a; 把⼀个⼤型复杂问题层层转化为⼀个与原问题相似&#xff0c;但…

ArkUI开发进阶—@Builder函数@BuilderParam装饰器的妙用与场景应用【鸿蒙专栏-05】

ArkUI开发进阶—@Builder函数@BuilderParam装饰器的妙用与场景应用 HarmonyOS,作为一款全场景分布式操作系统,为了推动更广泛的应用开发,采用了一种先进而灵活的编程语言——ArkTS。ArkTS是在TypeScript(TS)的基础上发展而来,为HarmonyOS提供了丰富的应用开发工具,使开…

mybatis <include refid=“xxx“></include>

&#xff1c;include refid"xxx"&#xff1e;&#xff1c;/include&#xff1e;使用在查询字段&#xff0c;多个select查询语句&#xff0c;可能查询的字段是相同的&#xff0c;就可以用这个标签把需要查询的字段抽出来。 <sql id"org_id">id,code,…

判断数组里面的元素是否都为某个数——C++ 算法库(std::all_of)

函数功能:检测表达式是否对范围[first, last)中所有元素都返回true,如果都满足,则返回true。 该函数对整个数组元素进行操作,可以节省运行循环来逐一检查每个元素的时间。 它检查每个元素上的给定属性,并在范围内的每个元素满足指定属性时返回 true,否则返回 false。 语…

【LeetCode】203. 移除链表元素

203. 移除链表元素 难度&#xff1a;简单 题目 给你一个链表的头节点 head 和一个整数 val &#xff0c;请你删除链表中所有满足 Node.val val 的节点&#xff0c;并返回 新的头节点 。 示例 1&#xff1a; 输入&#xff1a;head [1,2,6,3,4,5,6], val 6 输出&#xff…

Golang与MongoDB的完美组合

引言 在现代开发中&#xff0c;数据存储是一个至关重要的环节。随着数据量的增加和复杂性的提高&#xff0c;开发人员需要寻找一种高效、可扩展且易于使用的数据库解决方案。MongoDB作为一种NoSQL数据库&#xff0c;提供了强大的功能和灵活的数据模型&#xff0c;与Golang的高…

创建一个带有背景图层和前景图层的渲染窗口

开发环境&#xff1a; Windows 11 家庭中文版Microsoft Visual Studio Community 2019VTK-9.3.0.rc0vtk-example demo解决问题&#xff1a; 创建一个带有背景图层和前景图层的渲染窗口&#xff0c;知识点&#xff1a;1. 画布转image&#xff1b;2. 渲染图层设置&#xff1b;3.…

LeetCode算法题解(动态规划,股票买卖)|LeetCode121. 买卖股票的最佳时机、LeetCode122. 买卖股票的最佳时机 II

一、LeetCode121. 买卖股票的最佳时机 题目链接&#xff1a;121. 买卖股票的最佳时机 题目描述&#xff1a; 给定一个数组 prices &#xff0c;它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。 你只能选择 某一天 买入这只股票&#xff0c;并选择在 未来的某一…

工厂模式是一种创建对象的设计模式,使用工厂类来创建对象,而不是直接使用 new 关键字来创建对象。

文章目录 示例代码virtual std::string Operation() const = 0;如何理解std::string Operation() const override {这句如何理解?Factory 类包含一个静态方法 CreateProduct,它根据传入的类型参数来创建并返回具体的产品实例。这句话理解?std::unique_ptr<Product> pr…