迅捷cad_迅捷类

迅捷cad

In this tutorial, we’ll be discussing one of the fundamental building blocks, namely Swift Class.

在本教程中,我们将讨论基本的构建模块之一,即Swift类。

迅捷类 (Swift Class)

Swift Class is a blueprint or template for an instance of that class. The term object is often used to refer to an instance of a class.

Swift类是该类实例的蓝图或模板。 术语“ 对象”通常用于指代类的实例

An instance of a class is traditionally known as an object. However, in Swift classes and structures are much closer in functionality than in other languages. Hence we use the words instance and objects interchangeably.

传统上将类的实例称为对象。 但是,在Swift中,类和结构的功能比其他语言更接近。 因此,我们可以互换地使用实例对象这两个词。

Swift类语法 (Swift Class Syntax)

class A{
    var str = "Hello, playground"
    var number = 1

}

Creating an instance of the class as shown below.

创建该类的实例,如下所示。

var a = A()
print(a.str) //prints "Hello, playground\n"

Swift类初始化器 (Swift Class Initializers)

Unlike Structs, Classes don’t have default Initializers. We’ll need to create our own initializers and assign values to the properties in them.
The code snippet below contains a class with an initializer.

Structs不同,类没有默认的初始化器。 我们需要创建自己的初始化程序,并将值分配给它们中的属性。
下面的代码段包含一个带有初始化程序的类。

class A{

    var str : String
    var number : Int
    
    init(str: String, number: Int) {
        self.str = str
        self.number = number
    }

}
var a = A(str: "Hello World",number: 2)
print("Default String is \(a.str) and Default number is \(a.number)") //prints "Default String is Hello World and Default number is 2\n"

Note: A compiler error would occur if any of the declared properties aren’t initialised in a class.

注意 :如果未在类中初始化任何声明的属性,则将发生编译器错误。

Declare multiple initializers in your class as shown below.

如下所示在类中声明多个初始化器。

class A{

    var str : String
    var number : Int
    
    init(str: String, number: Int) {
        self.str = str
        self.number = number
    }
    
    init() {
        str = "Declare as many initalisers as you want"
        number = 0
    }

}

var a = A(str: "Hello World",number: 2)
print("Default String is \(a.str) and Default number is \(a.number)")

var a1 = A()
print("Default String is \(a1.str) and Default number is \(a1.number)") //prints "Default String is Declare as many initalisers as you want and Default number is 0\n"

Swift类继承 (Swift Classes Inheritance)

Inheritance defines a relationship where a subclass can be defined in terms of a superclass.
Classes support inheritance whereas Structures do not.
Any class that does not inherit from another class is known as a base class.
Subclassing is the act of basing a new class on an existing class. The subclass inherits characteristics from the existing class, which we can then refine as well as add new characteristics.

继承定义了一种关系,在该关系中可以根据超类定义子类。
类支持继承,而结构不支持继承。
任何不从另一个类继承的类都称为基类。
子类化是在现有类的基础上建立新类的行为。 子类继承了现有类的特征,然后我们可以对其进行细化和添加新特征。

Let’s create a base class as shown below.

让我们创建一个基类,如下所示。

class Animal{


    var isVeg : Bool
    var eats : String
    var numberOfLegs : Int
    
    init(isVeg: Bool, eats: String, numberOfLegs: Int) {
        self.isVeg = isVeg
        self.eats = eats
        self.numberOfLegs = numberOfLegs
    }
    
    func printProperties() {
    print("Is Veg? \(isVeg). Eats: \(eats). Number of legs: \(numberOfLegs)")
    }

}

So Animal is the base class that’ll be inherited by our subclasses. Before we get down to them, lets try creating an instance of the above class and calling the instance method over it as shown below.

所以Animal是我们的子类将继承的基类。 在深入了解它们之前,让我们尝试创建上述类的实例并对其进行调用,如下所示。

var anim = Animal(isVeg: false, eats: "Can eat you", numberOfLegs: 4)
anim.printProperties() //prints Is Veg? false. Eats: Can eat you. Number of legs: 4

To extend the superclass in the subclass, we write the subclass name followed by colon and then the superclass name as shown below.

为了在子类中扩展超类,我们编写了子类名称,后跟冒号,然后是超类名称,如下所示。

class Tiger: Animal {
 //Add class specific implementation
}

Without setting anything in the subclass, we can create an instance of Tiger class with the initializer of the superclass. To access the properties and functions of the superclass the dot operator is used.

无需在子类中进行任何设置,就可以使用超类的初始化程序创建Tiger类的实例。 要访问超类的属性和功能,请使用点运算符。

var tiger = Tiger(isVeg: false, eats: "Can eat you", numberOfLegs: 4)
tiger.printProperties() //prints Is Veg? false. Eats: Can eat you. Number of legs: 4

To override functions of the superclass, we use the keyword override in the subclass as shown below.

要覆盖超类的功能,我们在子类中使用关键字override ,如下所示。

class Tiger: Animal {

    override func printProperties() {
        super.printProperties()
        print("This is Subclass Tiger")
    }
    
}

var tiger = Tiger(isVeg: false, eats: "Can eat you", numberOfLegs: 4)
tiger.printProperties() //prints Is Veg? false. Eats: Can eat you. Number of legs: 4
This is Subclass Tiger

To access properties/functions of the superclass inside the class we use the super keyword followed by the dot operator.

要访问类内部超类的属性/函数,我们使用super关键字,后跟点运算符。

If the function printProperties in the Animal class is set to final, it can’t be overridden and will lead to compilation error.

如果Animal类中的函数printProperties设置为final,则不能覆盖它,这将导致编译错误。

class Animal{


    var isVeg : Bool
    var eats : String
    var numberOfLegs : Int
    
    init(isVeg: Bool, eats: String, numberOfLegs: Int) {
        self.isVeg = isVeg
        self.eats = eats
        self.numberOfLegs = numberOfLegs
    }
    
    final func printProperties() {
    print("Is Veg? \(isVeg). Eats: \(eats). Number of legs: \(numberOfLegs)")
    }

}


class Tiger: Animal {

    //The Below function can't be overridden. Would give compilation error.
    override func printProperties() {
        super.printProperties()
        print("This is Subclass Tiger")
    }
    
}

In the following code, we place properties inside a subclass and create it’s own init

在下面的代码中,我们将属性放在子类中并创建它自己的init

class Tiger: Animal {

    var color : String
    
    init(isVeg: Bool, eats: String, numberOfLegs: Int, color: String) {
        self.color = color
        super.init(isVeg: isVeg, eats: eats, numberOfLegs: numberOfLegs)
    }
    
    override func printProperties() {
        print("Is Veg? \(isVeg). Eats: \(eats). Number of legs: \(numberOfLegs). Color is \(color).")
    }
    
}

var tiger = Tiger(isVeg: false, eats: "Can eat you", numberOfLegs: 4, color: "White")
tiger.printProperties() //prints Is Veg? false. Eats: Can eat you. Number of legs: 4. Color is White.

In the above code, we add another property color to the subclass.
super.init is called to initalise the properties of the superclass once the subclass properties are initialised.

在上面的代码中,我们向子类添加了另一个属性color
初始化子类属性后,将调用super.init初始化超类的属性。

Swift类是引用类型 (Swift Classes are Reference Types)

Unlike Structs, Classes are passed by reference and not value. Let’s set the above tiger instance to another variable and change its properties.

与结构不同,类是通过引用而不是值传递的。 让我们将上述tiger实例设置为另一个变量并更改其属性。

var tiger = Tiger(isVeg: false, eats: "Can eat you", numberOfLegs: 4, color: "White")

var tiger1 = tiger
tiger1.color = "Yellow"

Now the output on printing the property color on both the tiger and tiger1 instances is given below.

现在,下面给出了在tigertiger1实例上打印属性颜色的输出。

print(tiger1.color) //Yellow
print(tiger.color) //Yellow

tiger1 is not a copy of tiger. It is the same as tiger.

tiger1 不是 tiger 的副本 。 它 tiger

便利初始化器 (Convenience Initializers)

Convenience Initializers act as supporting initializers for the main initializers of the class. These are primarily used to set default values in the main initializers. Convenience Initializers require the keyword convenience before init as shown below.

便利的初始化程序充当该类的主要初始化程序的支持初始化程序。 这些主要用于在主初始化程序中设置默认值。 方便初始化器所需要的关键字convenience之前init如下所示。

class A {
    var eyes: Int
    var legs: Int
    init(eyes: Int, legs: Int) {
        self.eyes = eyes
        self.legs = legs
    }
    
    
    convenience init() {
        self.init(eyes: 2, legs: 4)
    }

}

var a = A()
print(a.eyes) //prints 2
print(a.legs) //prints 4

Note : Convenience initializers are only valid when a normal initializers was already declared in the class
A convenience initializer must call another initializer from the same class.

注意 :便捷初始化器仅在类中已声明常规初始化器时才有效
便捷初始化程序必须从同一类调用另一个初始化程序。

This brings an end to this swift class tutorial.

这样就结束了本快速的课堂教程。

翻译自: https://www.journaldev.com/15980/swift-class

迅捷cad

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值