1. Use class to create a class in Swift. The declaration of a class is declared in a class as a constant or variable, except in the context of the class. This is also the case in methods and functions.
2. Use init (...) in Swift As a constructor of initialization
3. In Swift, when initializing a member variable with a constructor, the format is: Self.name = name. The declaration of a constructor is the same as a function, except that an instance of the class is created. Each property needs to be assigned, both in the Declaration and in the constructor.
4. Use Deinit in Swift to create a destructor that is automatically called by the system to undo the object and to clean up the memory
5. When the swift neutron class inherits from the parent class, a colon: separates. There is no need to declare when inheriting the standard root class, so you can ignore the superclass
6, the Swift Neutron class overrides the parent class method, must use the keyword override overload the superclass the implementation, if does not have this keyword, the compiler will error
7. The Get and set methods can be set in Swift, as shown below:
var perimeter:double {
Getter method
get{
Return slidelength
}
Setter method
set{
Slidelength = NEWVALUE/2
}
}
8, Swift can give the second parameter of the parameter name and then set a name, the function uses the original name of the parameter, the name of the set parameter name is used when calling
Specific examples are as follows:
define a class with no constructors shape
// declares a class without constructors class shape{ // member variable, number of sides var numberofsides = 0 // Span style= "color: #008000;" > member Method func Simpledescripton ()->string{ return a shape with \ (numberofsides) sides. " }} // Create object var shape = shape () // {numberofsides 0}
// Set member variables
// Call member Method Shape.simpledescripton () //"A shape with 4 sides"
define a subclass Nameshape inherit the above parent class shape, subclass with an Init constructor and a deinit destructor
//declares a class with constructors, inherits the parent class shapeclassnameshape:shape{//the name of the shapevar name:string//Initialize MethodInit (name:string) {self.name=name}//fictitious method, cannot display the call, the system will automatically call after the program execution completes, cleans up the memorydeinit{println ("Deinit") } //member method, overriding the method of the parent class must be added with the keyword override OverrideFunc Simpledescripton ()string{return "A shape with name:\ (name) has \ (numberofsides) sides." }}//create object, parameter name cannot be omittedvar nameshape:nameshape = nameshape (name:"rect") //{{numberofsides 0} name "Rect"} //Set member variablesNameshape.numberofsides =4 //{{numberofsides 4} name "Rect"} //Calling member MethodsNameshape.simpledescripton ()//"A shape with Name:rect has 4 sides"
Defines a square class square to inherit the parent class Nameshape, in fact, it also inherits the root parent class shape,square inherit all the public properties and methods of its parent class, at initialization, subclasses can use the initialization method of the parent class to assign the initial value to the properties they share.
//inherit parent class NameshapeclassSquare:nameshape {//Edge Lengthvar slidelength:double =0.0
//Subclass own initialization method init (slidelength:double,name:string) {self.slidelength=Slidelength//calling the initialization method of the parent classsuper.init (name:name)//directly with the number of edges inherited from the parent class PropertyNumberofsides =4}//Setting the object's set and get method var perimeter:double {//Getter Method Get{ returnSlidelength } //Setter Method Set{slidelength= newvalue/2 } } //define the method for calculating areaFunc area ()double{returnslidelength*Slidelength}OverrideFunc Simpledescripton ()String {return "A shape with name:\ (name) has \ (numberofsides) sides.its area are \ (area ())" }}//parameter names cannot be omitted when creating an objectvar square:square = Square (slidelength:2, Name:"Square")
Square.perimeter = 6square.simpledescripton () //"A shape with Name: Square has 4 sides. Its area is 9.0"square.slidelength //3
The parameter name of the second parameter can be set again by a name, the function internally uses the original name of the argument, and the name of the set parameter name is used when calling
class counter{ 0
//give the second parameter times a name of Numberoftimes, but the function internally uses thetimes func incrementby (Amount:int, Numberoftimes Times:int) { + = Amount * times = //Create an object
//When calling a function, the name of the second parameter used is Numberoftimescounter.incrementby (27) //14
Swift: Getting Started knowledge and objects