in the class followed by the class name to create a class. In addition to the context is a class, declaring a property and a constant, the variable declaration is the same, and the methods and functions are the same.
Class Shape { var numberofsides = 0 func simpledescription ()-String { return ' A Shape with \ (numberofsid ES) sides. " }}
Give it a try:
Add a constant property, and then add a function with one parameter.
Create an instance of the class by enclosing the parentheses after the class name. Use dot syntax to access the properties and methods of this instance.
var shape = shape () shape.numberofsides = 7var shapedescription = shape.simpledescription ()
last version of Shape class is missing an important thing: Create an instance from the initializer. Use init to create a.
Class Namedshape { var numberofsides:int = 0 var name:string init (name:string) { self.name = name } func simpledescription (), String { return "A shape with \ (numberofsides) sides." }}
It is important to note that it is used here Selfto differentiatenameis a property or a parameter. When you create an instance of a class, the arguments passed here are the same as the arguments passed when the function is called. Each property needs to be assigned, at the time of declaration (e.g.Numberofsides) or in the initializer (e.g.name).
When you need to do some cleanup work before the object is released, you can use deinit to create a destructor.
The subclass class name is followed by the parent class name, separated by a colon:. There is no forcing subclass to have a standard root class, so you can omit the parent class as needed.
When a subclass's method overrides the implementation of the parent class method, it will use the override tag, and if not added, the compiler would error. The compiler also detects whether the subclass tag has an override method that actually overrides the implementation of the parent class method.
Class Square:namedshape { var sidelength:double init (sidelength:double, name:string) { self.sidelength = Sidelength super.init (name:name) numberofsides = 4 } func area (), Double { return Sidelength * Sidelength } override func simpledescription () String { return ' A square with sides of l Ength \ (sidelength). " }} Let test = Square (sidelength:5.2, Name: "My Test Square") Test.area () test.simpledescription ()
Give it a try:
Write a subclass Circlethat inherits from Nameshape and receives the radius and name parameters in the initializer. and implement area and describe methods.
In addition to simple storage properties, attributes also have getter and setter methods.
class Equilateraltriangle:namedshape {var sideLength: Double = 0.0 init (sidelength:double, name:string) {self.sidelength = Sidelength super.init (name:n AME) Numberofsides = 3} var perimeter:double {get {return 3.0 * Sidelength} set { Sidelength = newvalue/3.0}} override Func Simpledescription (), String {return "an Equil Ateral Triagle with sides of length \ (sidelength). "}} var triangle = Equilateraltriangle (sidelength:3.1, Name: "a triangle") Triangle.perimetertriangle.perimeter = 9.9triangle.sidelength
perimeter setter method, the new value has an implicit name NewValue . You can set
Three things are done for the initializer in equilateraltriangle :
1. Setting the value of a property declared by a subclass
2. Calling the initializer of the parent class
3. Change the property value defined by the parent class. Of course, through other methods,getter,setter, can do this.
Span style= "color: #006600;" >willset and didset . For example, the following class ensures that the side length of triangles and squares are the same.
Class Triangleandsquare { var triangle:equilateraltriangle { Willset { square.sidelength = Newvalue.sidelength } } var square:square { Willset { triangle.sidelength = Newvalue.sidelength } } Init (size:double, name:string) { square = Square (sidelength:size, name:name) triangle = Equilateraltriangle (Sidelength:size, Name:name) }} var triangleandsquare = Triangleandsquare (size:10, Name: "Another Test shape") TriangleAndSquare.square.sideLengthtriangleAndSquare.triangle.sideLengthtriangleAndSquare.square = Square ( SIDELENGTH:50, Name: "Larger square") triangleAndSquare.triangle.sideLength
There is an important difference between the methods and functions in the class, the parameter names of the functions can only be used in the function, but the parameter names in the methods need to be used when you invoke the method (except for the parameter name of the first argument). By default, the parameter name of a method is the same when invoked or used inside a method, and you can identify a second name to use in the method.
Class Counter { var count:int = 0 func incrementby (amount:int, Numberoftimes times:int) { count + = Amount * Times }}var counter = Counter () Counter.incrementby (2, Numberoftimes:7)
when using theOptionalvalue, you can add a question mark before the Operation? . If the? the previous value isNil, then question mark? The subsequent code is ignored and is consideredNil. Otherwise, thisOptionalvalue is unpacked, question mark? after that, it is used as the unpacking value. In the following two cases, the value of the entire expression is aOptionalvalues.
Let Optionalsquare:square? = Square (sidelength:2.5, name: "Optional square") Let Sidelength = Optionalsquare?. Sidelength
Swift Learning-Objects and classes