Junior — Middle
Kas saaksite selgitada, mida tähendab staatiline omadus objektorienteeritud programmeerimises?
sobes.tech AI
Vastus AI-lt
Statically in object-oriented programming, a property is a class property, not an instance property. It belongs to the class itself and is shared among all objects of that class.
For example, if you have a class Car, and it has a static property numberOfCars, this property will be common to all Car objects and can be used to count the total number of cars created.
In Swift, it looks like this:
class Car {
static var numberOfCars = 0
init() {
Car.numberOfCars += 1
}
}
let car1 = Car()
let car2 = Car()
print(Car.numberOfCars) // Will print 2
Therefore, static properties are useful for storing data that should be common to all instances of the class.