当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 类型

作者:精品下载站 日期:2024-12-14 02:56:12 浏览:14 分类:玩电脑

类型


在 PowerShell 中,每个值都有一个类型,并且类型属于两个主要类别之一:值类型引用类型。考虑类型 int,它是典型的值类型。 int 类型的值是完全独立的;表示该值所需的所有位都存储在该值中,并且该值中的每个位模式都表示其类型的有效值。现在,考虑数组类型 int[],它是典型的引用类型。所谓的数组类型值可以保存对实际包含数组元素的对象的引用,也可以保存值为 $nullnull 引用。两种类型类别之间的重要区别可以通过赋值期间语义的差异得到最好的证明。例如,

$i = 100 # $i designates an int value 100
$j = $i # $j designates an int value 100, which is a copy

$a = 10,20,30 # $a designates an object[], Length 3, value 10,20,30
$b = $a # $b designates exactly the same array as does $a, not a copy
$a[1] = 50 # element 1 (which has a value type) is changed from 20 to 50
$b[1] # $b refers to the same array as $a, so $b[1] is 50

我们可以看到,引用类型值的赋值涉及到浅拷贝;也就是说,对象引用的副本而不是其实际值。相反,深层复制还需要制作对象的副本。

数字类型是一种允许表示整数或小数值并支持对这些值进行算术运算的类型。数值类型集包括整数(第 4.2.3 节)和实数(第 4.2.4 节)类型,但不包括 bool(第 4.2.1 节)或 char(第 4.2.2 节)类型。实现可以提供其他数字类型(例如有符号字节、无符号整数和其他大小的整数)。

集合是一组一个或多个相关项目,这些项目不必具有相同的类型。集合类型的示例有数组、堆栈、队列、列表和哈希表。程序可以枚举(或迭代)集合中的元素,一次访问每个元素。执行此操作的常见方法是使用 foreach 语句(第 8.4.4 节)和 ForEach-Object cmdlet。表示枚举数的对象类型在第 4.5.16 节中描述。

本章中的表格列出了给定类型的可访问成员。对于方法,类型采用以下形式编写:returnType/argumentTypeList。如果参数类型列表太长,无法容纳在该列中,则会显示在目的列中。

其他整数类型有 SByteInt16UInt16UInt32UInt64,全部都在命名空间System中。

许多集合类被定义为 System.CollectionsSystem.Collections.Generic 命名空间的一部分。大多数集合类都实现接口ICollectionIComparerIEnumerableIListIDictionary 、和 IDictionaryEnumerator 及其通用等效项。

您还可以对某些类型使用简写名称。有关详细信息,请参阅 about_Type_Accelerators。

4.1 特殊类型

4.1.1 void 类型

该类型无法实例化。它提供了一种使用强制转换运算符显式丢弃值的方法(第 7.2.9 节)。

4.1.2 null 类型

null 类型 有一个实例,即自动变量 $null (§2.3.2.2),也称为 null 值。该值提供了一种在参考上下文中表达“无”的方法。此类型的特征未指定。

4.1.3 对象类型

PowerShell 中除 null 类型(第 4.1.2 节)之外的每个类型都是直接或间接从类型 object 派生的,因此 object 是所有非 null 类型的最终基类型。约束为 object 类型的变量(第 5.3 节)实际上根本不受约束,因为它可以包含任何类型的值。

4.2 值类型

4.2.1 布尔值

布尔类型是bool。这种类型只有两个值,FalseTrue,由自动变量$false$true,分别(§2.3.2.2)。

在 PowerShell 中,bool 映射到 System.Boolean

4.2.2 特性

字符值的类型为 char,它能够存储任何 UTF-16 编码的 16 位 Unicode 代码点。

char 类型具有以下可访问成员:

Member Member Kind Type Purpose MaxValue Static property (read-only) char The largest possible value of type char MinValue Static property (read-only) char The smallest possible value of type char IsControl Static method bool/char Tests if the character is a control character IsDigit Static method bool/char Tests if the character is a decimal digit IsLetter Static method bool/char Tests if the character is an alphabetic letter IsLetterOrDigit Static method bool/char Tests if the character is a decimal digit or alphabetic letter IsLower Static method bool/char Tests if the character is a lowercase alphabetic letter IsPunctuation Static method bool/char Tests if the character is a punctuation mark IsUpper Static method bool/char Tests if the character is an uppercase alphabetic letter IsWhiteSpace Static method bool/char Tests if the character is a white space character. ToLower Static method char/string Converts the character to lowercase ToUpper Static method char/string Converts the character to uppercase

Windows PowerShell:char 映射到 System.Char。

4.2.3 整数

有两种有符号整数类型,两者都使用补码表示负值:

  • 类型 int,它使用 32 位,范围为 -2147483648 到 +2147483647(含)。
  • 类型 long,使用 64 位,范围为 -9223372036854775808 到 +9223372036854775807(含)。

int 类型具有以下可访问成员:

Member Member Kind Type Purpose MaxValue Static property (read-only) int The largest possible value of type int MinValue Static property (read-only) int The smallest possible value of type int

long 类型具有以下可访问成员:

Member Member Kind Type Purpose MaxValue Static property (read-only) long The largest possible value of type long MinValue Static property (read-only) long The smallest possible value of type long

有一种无符号整数类型:

  • 类型 byte,使用 8 位,范围为 0 到 255(含)。

byte 类型具有以下可访问成员:

Member Member Kind Type Purpose MaxValue Static property (read-only) byte The largest possible value of type byte MinValue Static property (read-only) byte The smallest possible value of type byte

在 PowerShell 中,byteintlong 映射到 System.ByteSystem.Int32 分别是 和 System.Int64。

4.2.4 实数

4.2.4.1 浮点型和双精度型

有两种实数(或浮点)类型:

  • float 类型使用 32 位 IEEE 单精度表示。
  • double 类型使用 64 位 IEEE 双精度表示。

第三个类型名称 single 是类型 float 的同义词;本规范通篇使用float

尽管 floatdouble 类型的大小和表示是由本规范定义的,但实现可以使用扩展精度来获取中间结果。

float 类型具有以下可访问成员:

Member Member Kind Type Purpose MaxValue Static property (read-only) float The largest possible value of type float MinValue Static property (read-only) float The smallest possible value of type float NaN Static property (read-only) float The constant value Not-a-Number NegativeInfinity Static property (read-only) float The constant value negative infinity PositiveInfinity Static property (read-only) float The constant value positive infinity

double 类型具有以下可访问成员:

Member Member Kind Type Purpose MaxValue Static property (read-only) double The largest possible value of type double MinValue Static property (read-only) double The smallest possible value of type double NaN Static property (read-only) double The constant value Not-a-Number NegativeInfinity Static property (read-only) double The constant value negative infinity PositiveInfinity Static property (read-only) double The constant value positive infinity

在 PowerShell 中,floatdouble 分别映射到 System.SingleSystem.Double

4.2.4.2 十进制

Decimal 类型使用 128 位表示。它至少必须支持一个比例 s ,使得 0

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯