The object and class of the Swift programming language entry

Source: Internet
Author: User
Tags constant constructor count expression

Use class to create a class. The declaration of a property is declared in a class as a constant or variable, except in the context of the class. Methods and functions are written in this way.

Class Shape {var numberofsides = 0 Func simpledescription ()-> String {return "A-shape with (numberofsides) sides."} }

Practice

Add a constant property through "let" and add another method to accept the argument.

Create an instance of the class by adding parentheses after the class name. Use the point syntax to access the properties and methods of the instance.

var shape = shape ()

Shape.numberofsides = 7

var shapedescription = shape.simpledescription ()

Something important about this version of the Shape class is not: A constructor to set the class when the instance is created. Use init to create one.

Class Namedshape {

var numberofsides:int = 0

var name:string init (name:string) {

Self.name = Name

}//by Gashero

Func simpledescription ()-> String {

Return "A Shape with (numberofsides) sides."

}

}

Note Self is used to differentiate between the Name property and the name parameter. The constructor's life is like a function, except that it creates an instance of the class. Each attribute needs to be assigned, whether in a declaration or in a constructor.

Use Deinit to create a destructor to perform cleanup work on Object destruction.

Subclasses include the names of their superclass, separated by colons. You do not need to declare when inheriting a standard root class, so you can ignore the superclass.

The methods of subclasses can be override by overloading the implementations in the superclass, and No Override is considered an error by the compiler. The compiler also checks for methods that are not overloaded.

Class Square:namedshape {var sidelength:double init (sidelength:double, name:string) {self.sidelength = sideLength sup Er.init (name:name) numberofsides = 4} func area ()-> Double {return sidelength * sidelength} override func Simplede Scription ()-> String {return "A square with sides of length (sidelength)."} Let test = Square (sidelength:5.2, name: ' My Test Square ') Test.area () test.simpledescription ()

Practice

The subclass of another namedshape is called Circle, which accepts the radius and the name to its constructor. Implement area and describe methods.

Properties can have getter and setter.

Class Equilateraltriangle:namedshape

{

var sidelength:double = 0.0

Init (sidelength:double, name:string) {

Self.sidelength = Sidelength

Super.init (Name:name)

Numberofsides = 3

}

var perimeter:double{

get {

Return 3.0 * Sidelength

}

set {

Sidelength = newvalue/3.0

}

}

Override Func simpledescription ()-> String {

Return "a equilateral triangle with sides of length (sidelength)."

}

}

var triangle = Equilateraltriangle (sidelength:3.1, Name: "A triangle")

Triangle.perimeter

Triangle.perimeter = 9.9

Triangle.sidelength

In the setter of perimeter, the name of the new value is newvalue. You can provide a name that does not conflict after set.

Note that the Equilateraltriangle constructor has 3 different steps:

Set the value of the property to call the constructor of the superclass to change the value of the attribute defined by the superclass, add additional work to use the method, getter, setter can also be

If you do not need to compute the attribute, but still provide to perform the work after setting the value, use Willset and Didset. For example, the following class ensures that the side lengths of its triangles are equal to the length of the rectangle.

Class Triangleandsquare {

var Triangle:equilatertriangle {

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 = Equilatertriangle (sidelength:size, Name:name)

}

}

var triangleandsquare = Triangleandsquare (size:10, Name: "Another Test shape")

TriangleAndSquare.square.sideLength

TriangleAndSquare.triangle.sideLength

Triangleandsquare.square = Square (sidelength:50, name: "Larger square")

TriangleAndSquare.triangle.sideLength

There is an important difference between a class's method and a function. The parameter name of the function is used only with the function, but the parameter name of the method can also be used to invoke the method (except the first argument). By default, a method has an argument with the same name, which is the parameter itself. You can specify a second name to use within 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 working with an optional value, you can write "?" to the operator similar to the method attribute. If the value is in the "?" It's been nil before, all after "?" is automatically ignored, and the entire expression is nil. In addition, the optional values are not packaged, and all the "?" after is taken as an unpackaged value. In both cases, the value of the entire expression is an optional value.

Let Optionalsquare:square? = Square (sidelength:2.5, name: "Optional Square")

Let Sidelength = Optionalsquare? Sidelength

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.