Friday, 17 November 2017

Object Oriented Programming in Swift

The majority of Apple's structures have a protest situated engineering. Before you begin diving into iOS/MacOS advancement you should first comprehend protest situated programming and configuration designs. In this article we will experience the essential thoughts and configuration designs keeping in mind the end goal to kick you off with application improvement.

Review

Protest situated programming (OOP) is a programming worldview that speaks to the idea of "objects" that have information fields (traits that portray the question) and related systems known as strategies. Items, which are generally occurrences of classes, are utilized to associate with each other to plan applications and PC programs.

There are 3 key parts of question arranged programming:

Exemplification implies that items keep their state data private. Instead of straightforwardly controlling a protest's information, different articles send solicitations to the question, as messages, some of which the protest may react to by modifying its inside state.

Polymorphism implies that objects of various classes can be utilized reciprocally. This is particularly critical, as it enables you to attach classes at a later date in ways you didn't really anticipate when those classes were first outlined.

Legacy implies that objects of one class can infer some portion of their conduct from another (base or parent) class. Some protest situated dialects (C++, for instance, yet not Swift) permit different legacy, where objects of one class can determine part or the greater part of their conduct from numerous free base classes.

Classes and Objects

In question situated programming, a class is an extensible program-code-format for making objects, giving beginning esteems to state (part factors) and executions of conduct (part works, techniques). At the end of the day, a class resembles an outline, it characterizes the information and conduct of a sort.

class Button {

}

The definition above makes a vacant class named Button. The main thing it can do is make new Button objects:

var catch = Button()

In the case above catch is an occurrence of the Button class.

Properties

Classes and occurrences can have related esteems named properties.

class Square {

var length: Int = 1

}

The Square class has a length property that has a default estimation of 1.

Keeping in mind the end goal to make squares with an unexpected length in comparison to 1 we have to compose a custom initializer.

class Square {

var length: Int = 1

init(length: Int) {

self.length = length

}

}

var firstSquare = Square(length: 3)

println(firstSquare.length)

var secondSquare = Square(length: 10)

println(secondSquare.length)

in the event that firstSquare.length < secondSquare.length {

println("the little square has the length \(firstSquare.length)")

} else {

println("the little square has the length \(secondSquare.length)")

}

Strategies

Strategies add conduct to classes and cases.

class Square {

var length: Int = 1

func zone() - > Int {

return length * length

}

}

The zone() technique registers the territory of a Square occasion when called. To call a strategy utilize the . documentation.

var square = Square(5)

println(square.area())/prints 25

sqare.length = 10

println(square.area())/prints 100

Class Properties

The typical utilization of properties are occurrence properties, similar to the length of a Square. You can likewise have a properties. The following is an illustration utilization of class properties. The Tank class has a figured propertynamed bonusDamage that is utilized to expand the harm for all tanks while redesigning. At the point when an overhaul is finished all we have to do is callTank.upgrade() and all Tank cases will have more harm.

class Tank {

class var bonusDamage: Double {

return Double(Upgrade.level) * 2.5

}

let baseDamage = 10.0

var harm: Double {

return self.baseDamage + Tank.bonusDamage

}

class func redesign() {

Upgrade.level += 1

}

struct Upgrade {

static var level = 0

}

}

var tank = Tank()

println(tank.damage)

/10.0

Tank.upgrade()

println(tank.damage)

/12.5

Tank.upgrade()

println(tank.damage)

/15.0

Legacy

A class can acquire strategies, properties, and different attributes from another class. When one class acquires from another, the acquiring class is known as a subclass, and the class it acquires from is known as its superclass. Legacy is an essential conduct that separates classes from different sorts in Swift.

A straightforward case of legacy:

class AClass {

func doSomething() {

println("Hello from AClass")

}

}

class Subclass: AClass {

}

let base_object = AClass()

base_object.doSomething()

/> Hello from AClass

let enhanced_object = Subclass()

enhanced_object.doSomething()

/> Hello from AClass

Superseding

You can supersede strategies with a specific end goal to give custom conduct. To supersede a strategy compose the abrogate watchword before the technique affirmation:

class AClass {

func doSomething() {

println("Hello from AClass")

}

}

class Subclass: AClass {

abrogate func doSomething() {

println("Hello from Subclass")

}

}

let base_object = AClass()

base_object.doSomething()

/> Hello from AClass

let enhanced_object = Subclass()

enhanced_object.doSomething()

/> Hello from Subclass

You can utilize the super watchword to call any technique from the superclass.

...

class Subclass: AClass {

abrogate func doSomething() {

super.doSomething()

println("Hello from Subclass")

}

}

let enhanced_object = Subclass()

enhanced_object.doSomething()

/> Hello from AClass

/> Hello from Subclass



No comments:

Post a Comment