类型方法和类型实例方法等之间的区别? [英] Difference between type method and type instance method, etc?

查看:47
本文介绍了类型方法和类型实例方法等之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我已经阅读了苹果文档并研究了一本 swift 书.

  1. 我对类型实例方法"(如果存在,如果我错了,请纠正我)和类型方法之间的区别感到困惑?

  2. 类方法和实例方法的区别?

  3. 类型属性和实例属性之间的区别(如果存在,抱歉我对类型属性主题很困惑)?

  4. 最后,Swift 中是否存在类属性?

抱歉打扰了:'(

解决方案

在 Swift 中,类型命名类型复合类型.命名类型包括结构枚举协议.除了用户定义的命名类型之外,Swift 还定义了许多命名类型,例如数组、字典和可选值.(让我们暂时忽略复合类型,因为它与您的问题没有直接关系.)

为了回答您的问题,假设我创建了一个名为 Circle 的用户定义类(这只是一个示例):

class Circle {静态让 PI = 3.14var 半径:双倍初始化(半径:双){self.radius = 半径}//返回这个圆的面积功能区(){返回 PI * 半径}//用于演示目的的荒谬的类方法静态功能打印类型名称(){println("圆")}}

<块引用>

  1. 我对类型实例方法"之间的区别感到困惑(如果存在,如果我错了,请纠正我)和类型方法?

如前所述,类型是指类、结构、枚举、协议和复合类型.在我上面的例子中,我使用一个名为 Circleclass 来定义一个 type.

如果我想构造 Circle 类的单个对象,那么我将创建一个实例.例如:

让 myCircleInstance = Circle(radius: 4.5)让 anotherCircleInstance = Circle(半径:23.1)

以上是Circle的对象或实例.现在我可以直接调用它们的实例方法.我的类中定义的实例方法area.

let areaOfMyCircleInstance = myCircleInstance.area()

现在,类型方法是一种可以直接在类型上调用的方法,无需创建该类型的实例.>

例如:

Circle.printTypeName()

请注意,在 func 之前有一个 static 限定符.这表明它直接属于类型,而不属于类型的实例.

<块引用>

  1. 类方法和实例方法的区别?

见上面的解释.

<块引用>

  1. 类型属性和实例属性的区别(如果这样存在,抱歉,我对类型属性主题感到很困惑)?

这与问题一中的解释类似,除了它不是应用于方法,而是应用于类型的属性(即属性、变量).

在我的 Circle 示例中,属性定义为:

static 让 PI = 3.14var 半径:双倍

属性PI是一个类型属性;它可以通过类型

直接访问

Circle.PI

属性radius是类型的实例属性;它可以被类型的实例访问.使用我们之前创建的变量:

//我可以这样做;它将是 4.5myCircleInstance.radius//和这个;它将是 23.1anotherCircleInstance.radius//但我不能这样做,因为半径是一个实例属性!圆.半径

<块引用>

  1. 最后,Swift 中是否存在类属性?

绝对的!阅读我对上面问题 3 的解释.我示例中的 PI 属性是类属性的示例.

参考文献:

Note: I've read the apple documentation and studied a swift book.

  1. I'm confused on the difference between a "type instance method" (if such exists, correct me if I'm wrong) and a type method?

  2. Difference between class method and instance method?

  3. Difference between type property and instance property(if such exists, sorry I'm very confused on Type Properties subject)?

  4. Lastly, Do class properties exist in swift?

Sorry for the confusion :'(

解决方案

In Swift, types are either named types or compound types. Named types include classes, structures, enumerations, and protocols. In addition to user-defined named types, Swift defines many named types such as arrays, dictionaries, and optional values. (Let's ignore compound types for now since it doesn't directly pertain to your question.)

To answer your questions, suppose that I create a user defined class called Circle (this is just an example):

class Circle {

    static let PI = 3.14

    var radius: Double

    init(radius: Double) {
        self.radius = radius
    }

    // Returns the area of this circle
    func area() {
        return PI * radius
    }

    // Ridiculous class method for demonstration purposes
    static func printTypeName() {
        println("Circle")
    }
} 

  1. I'm confused on the difference between a "type instance method" (if such exists, correct me if I'm wrong) and a type method?

As mentioned earlier, a type refers to a class, structure, enumeration, protocol, and compound types. In my example above, I use a class called Circle to define a type.

If I want to construct an individual object of the Circle class then I would be creating an instance. For example:

let myCircleInstance = Circle(radius: 4.5)
let anotherCircleInstance = Circle(radius: 23.1)

The above are objects or instances of Circle. Now I can call instance methods on them directly. The instance method defined in my class is area.

let areaOfMyCircleInstance = myCircleInstance.area()

Now, a type method is a method that can be called directly on the type without creating an instance of that type.

For example:

Circle.printTypeName()

Notice that there is a static qualifier before the func. This indicates that it pertains to the type directly and not to an instance of the type.

  1. Difference between class method and instance method?

See the explanation above.

  1. Difference between type property and instance property(if such exists, sorry I'm very confused on Type Properties subject)?

This is a similar explanation to the one in your question one except that instead of applying to methods, it applies to the properties (i.e., attributes, variables) of the type.

In my Circle example, the properties are defined as:

static let PI = 3.14
var radius: Double

The property PI is a type property; it may be accessed directly by the type

Circle.PI

The property radius is an instance property of the type; it may be accessed by an instance of the type. Using the variables we created earlier:

// I can do this; it will be 4.5
myCircleInstance.radius

// And this; it will be 23.1
anotherCircleInstance.radius

// But I CANNOT do this because radius is an instance property!
Circle.radius

  1. Lastly, Do class properties exist in swift?

Absolutely! Read my explanation to your question 3 above. The PI property in my example is an example of a class property.

References:

这篇关于类型方法和类型实例方法等之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆