# 类属性

`实例属性`属于一个特定类型的实例；`类属性`属于整个类型，在多个该类型的实例间共享。

`类属性`既可以定义为`存储属性`也可以定义为`计算属性`

`存储的类属性`必须在定义时提供初始化器。但是`类型属性`在首次访问时才初始化，同时`类型属性`的初始化是线程安全的。

## 定义类属性

使用关键字 `static` 来定义类型属性。在为类定义计算型类型属性时，可以改用关键字 `class` 来支持子类对父类的实现进行重写.

> 类，结构体和枚举都可以定义存储型类属性

```swift

class SomeStructure {
    static var storedTypeProperty = "Some value."
    class var computedTypeProperty: Int {
        return 1
    }
}
enum SomeEnumeration {
    static var storedTypeProperty = "Some value."
    static var computedTypeProperty: Int {
        return 6
    }
}
class SomeClass : SomeStructure{
    class override var computedTypeProperty: Int {
        return 107
    }
}

```

`类型属性`可以被子类继承，子类不可以定义与父类中名字相同的`类型属性`。\
当使用`class`定义的类型计算属性，可以被子类重写。

## 2. 类型属性可以定义属性观察器


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.existorlive.cn/kai-fa-yu-yan-xue-xi/swift/9.-lei-he-jie-gou-ti/shu-xing/2.5-lei-shu-xing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
