迅捷cad_迅捷结构

迅捷cad

In this tutorial, we’ll be discussing an important building block in Swift, namely Structures. Swift struct keyword is used to create structures in swift programming.

在本教程中,我们将讨论Swift中的一个重要构建模块,即Structures 。 Swift struct关键字用于在Swift编程中创建结构。

迅捷结构 (Swift Struct)

On a high level, Structures are custom data types that let us store data in the form of properties and functions. One of the highlights of Structures is that they are value types and not reference types.
Let’s look at the basic syntax of a struct.

从高层次上讲,结构是自定义数据类型,可让我们以属性和函数的形式存储数据。 结构的亮点之一是它们是值类型而不是引用类型。
让我们看一下struct的基本语法。

struct Name {
  // properties here
}

A Structure is defined using the keyword struct followed by the name and curly braces.

使用关键字struct定义struct后跟名称和花括号。

Properties are normal variables/constants that are defined in a struct. We’ll look more into Properties in a later tutorial.

属性是在struct中定义的普通变量/常量。 我们将在以后的教程中深入研究“属性”。

Unlike other languages, Struct in Swift is fully featured. They allow properties, functions, initializers, protocols etc. Unlike Classes, Structs in Swift doesn’t support inheritance.

与其他语言不同,Swift中的Struct具有完整功能。 它们允许属性,函数,初始化器,协议等。与类不同,Swift中的结构不支持继承。

Let’s open up the playground and play around with structures in it.

让我们打开操场并在其中玩耍。

Swift结构示例 (Swift Struct Example)

Here’s an example of a Struct definition with properties.

这是带有属性的Struct定义的示例。

struct Rectangle {
    var width = 0
    var height = 0
}

An instance of the above struct can be created in the following way.

可以通过以下方式创建上述struct的实例。

var rectangle = Rectangle()

To access or modify the properties of a structure we use the dot operator on the instance as shown below.

要访问或修改结构的属性,我们在实例上使用点运算符,如下所示。

print(rectangle.width) // prints 0
print(rectangle.height) //prints 0

rectangle.width = 10
rectangle.height = 10

If the instance was initialized as let, changing the properties won’t be possible.

如果实例初始化为let ,则无法更改属性。

let rectangle = Rectangle()

rectangle.width = 10 //compile-time error
rectangle.height = 10 //compile-time error

Defining functions inside a Structure

在结构内部定义功能

struct Rectangle {
    var width = 0
    var height = 0
    
    func area(width :Int, height: Int) -> String
    {
         return "Area is  \(width*height)"
    }
    
}

Calling the function using an instance of the structure as shown below:

使用结构实例调用函数,如下所示:

var rectangle = Rectangle()

rectangle.width = 10
rectangle.height = 10

rectangle.area(width: rectangle.width, height: rectangle.height) //prints 100

在函数内部修改结构属性 (Modifying struct properties inside functions)

To modify a property inside a function we need to mark the function with the keyword mutating.

要在函数内部修改属性,我们需要使用关键字mutating标记该函数。

struct Rectangle {
    var width = 0
    var height = 0
    
    mutating func printModifiedWidth() -> String
    {
     
        width = width*2
        return "Modified width is \(width)"
    }
    
}

var rectangle = Rectangle()
rectangle.width = 10
rectangle.printModifiedWidth() //prints "Modified width is 20"

Note: Properties inside a structure can’t be changed inside a simple function.
Declaring function as mutating inside struct allows us to alter properties in Structures.

注意 :结构内的属性无法在简单函数内更改。
声明功能内变异 struct使我们能够在结构ALTER性能。

Swift结构初始化器 (Swift Struct Initialisers)

Default initializer takes 2 forms. Either an empty initializer() or the memberwise initializer that lists the structure’s properties inside its parenthesis so that you can assign new values to them.

默认的初始化程序有2种形式。 一个空的 initializer()或在该结构的圆括号内列出该结构的属性的成员初始化程序,以便可以为其分配新值。

A memberwise initializer for the above structure would look as follows:

上述结构的成员初始化器如下所示:

var rectangle = Rectangle(width: 10, height: 10)

A custom initialiser can be defined as follows:

可以将自定义初始化程序定义如下:

struct Rectangle {
    var width = 0
    var height = 0
    
    init(width: Int, height: Int) {
        self.width = width*2
        self.height = height*2
    }
}

var rectangle = Rectangle(width: 10, height: 10)
print(rectangle.width) //prints 20
print(rectangle.height) //prints 20

Initialisers Delegation is also possible as shown below.

如下所示,也可以进行初始化程序委托

struct Rectangle {
    var width = 0
    var height = 0
    
    init(width: Int, height: Int) {
        self.width = width
        self.height = height
    }
    
    init(width: Int) {
    self.init(width: width, height: width)
    }
}

var square = Rectangle(width: 10)
print(square.width) //prints 10
print(square.height) //prints 10

Swift结构静态函数 (Swift Struct Static Functions)

Static Functions can be called without creating an instance of Structure as shown below:

可以在不创建Structure实例的情况下调用静态函数,如下所示:

struct Rectangle {
    var width = 0
    var height = 0
    
    static func printString() -> String
    {

    return "Hello how you're doing"
    }
}

Rectangle.printString() //prints "Hello how you're doing"

Swift Struct计算属性 (Swift Struct Computed Properties)

Properties can be computed dynamically using the following syntax.

可以使用以下语法动态计算属性。

struct Rectangle {
    var width = 0
    var height = 0
    
    var area: Int {
        get {
            return width*height
        }
        set {
            area = 0
        }
    }
    
}

set is used to give an initial value to the property.
get is the one that actually returns the computed property.
Using the computed properties the area property would be automatically calculated when the width or height is altered.

set用于为属性赋予初始值。
get是实际返回计算属性的对象。
使用计算出的属性,当widthheight更改时,将自动计算area属性。

var rectangle = Rectangle()
print(rectangle.area) //prints 0
rectangle.width = 20
rectangle.height = 20
print(rectangle.area) //prints 400

Swift结构是通过值而不是通过引用传递的 (Swift Structures are passed by values and not by reference)

struct Rectangle {
    var width = 0
    var height = 0
    
    var area: Int {
        get {
            return width*height
        }
        set {
            area = 0
        }
    }
    
}

var rectangle = Rectangle()

print(rectangle.area)
rectangle.width = 20
rectangle.height = 20

var rect = rectangle
rect.width = 30
print(rectangle.width) //prints 20 and NOT 30

This brings an end to swift struct tutorial. We’ll be discussing Classes in our next Swift Tutorial.

这样就结束了快速的结构教程。 我们将在下一个Swift教程中讨论类。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/15881/swift-struct

迅捷cad

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 迅捷CAD编辑器5.0专业版是一种强大的CAD编辑器软件,在设计和制图领域有着广泛的应用。 该软件具有丰富的功能和工具,可以帮助用户轻松创建、编辑和查看CAD图纸。它支持各种CAD文件格式,包括DWG、DXF、DGN等,可以实现与其他CAD软件的无缝兼容。 迅捷CAD编辑器5.0专业版拥有直观友好的界面,使用户能够快速上手并高效地完成工作。它提供了多种绘图工具,如线段、圆、多边形等,还支持实体编辑、文本编辑、添加注释等操作,可以满足不同绘图需求。 此外,该编辑器还集成了强大的渲染引擎,可以生成高质量的渲染图像,使设计师能够更好地展示作品。同时,它还支持3D建模功能,可以创建复杂的立体模型,并进行动画渲染。 迅捷CAD编辑器5.0专业版还具备快速的批量处理功能,可以对多个图纸文件进行自动处理,提高工作效率。另外,它还支持脚本编程,用户可以通过编写脚本实现自动化操作,进一步提升工作效率。 总的来说,迅捷CAD编辑器5.0专业版是一款功能强大、操作简便、兼容性好的CAD编辑器软件,适用于各行各业的设计人员和制图人员,能够满足他们在绘图、编辑和渲染等方面的需求,提高设计效率和作品质量。 ### 回答2: 迅捷CAD编辑器5.0专业版是一款功能强大的CAD设计软件。它提供了完整的二维和三维设计工具,能够满足建筑设计、机械设计、电气设计等各个领域的需求。 该软件具有简单易用的界面,操作方便快捷。它支持多种常见的CAD文件格式,可以方便地导入和导出文件,与其他CAD软件无缝衔接。同时,它还支持自动保存和恢复功能,确保设计文件不会丢失。 迅捷CAD编辑器5.0专业版提供了丰富的绘图工具,如直线、折线、圆弧、多段线等,能够精确地绘制各种形状。此外,它还具备编辑和修改设计的功能,如修改尺寸、旋转、缩放、复制等。同时,它还提供了图层管理功能,能够对设计图层进行独立控制,方便用户进行编辑和修改。 该软件还支持三维建模和渲染功能。用户可以在软件中创建三维模型,并进行纹理贴图、光影效果的设置,实现真实感的渲染效果。此外,它还支持多视图显示功能,用户可以同时查看和编辑多个视图,方便设计人员进行多方位的设计。 总的来说,迅捷CAD编辑器5.0专业版是一款功能强大、操作简便的CAD设计软件。它提供了丰富的二维和三维设计工具,能够满足各种设计需求。无论是建筑设计师、机械工程师还是电气工程师,都可以通过该软件轻松完成高质量的设计工作。 ### 回答3: 迅捷CAD编辑器5.0专业版是一款功能强大的CAD设计软件。它具有丰富的绘图工具和编辑功能,可以帮助用户快速准确地进行CAD设计和编辑工作。 首先,迅捷CAD编辑器5.0专业版提供了全面的绘图工具,包括直线、圆弧、多边形等,用户可以根据需要选择合适的绘图工具进行绘图操作。同时,还支持各种常用的CAD绘图命令,如拉伸、旋转、偏移等,方便用户进行复杂的设计操作。 其次,该软件支持多种文件格式的导入和导出,用户可以方便地将设计文件与其他CAD软件进行兼容,与同事进行文件的共享和交流。此外,还支持将CAD设计文件输出为图片或PDF格式,方便用户进行打印和展示。 此外,迅捷CAD编辑器5.0专业版还具有强大的编辑功能。用户可以对绘图对象进行精确的编辑和修改,如变换、裁剪、镜像等。还支持图层管理功能,可以根据需要将绘图对象分层管理,便于用户对设计进行组织和调整。 总体而言,迅捷CAD编辑器5.0专业版具有丰富的绘图工具和编辑功能,能够满足用户对CAD设计和编辑的各种需求。无论是专业设计师还是初学者,都可以通过使用该软件进行准确、高效的CAD设计工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值