Swift programming language entry objects and Classes

Source: Internet
Author: User

Swift programming language entry objects and Classes

You can use class to create a class. An Attribute declaration is declared as a constant or variable in the class, except in the context of the class. The same is true for methods and functions.

Class Shape {var numberOfSides = 0 func simpleDescription ()-> String {return "A shape with (numberOfSides) sides ."}}

Exercise

Use "let" to add a constant attribute and add another method to accept parameters.

Create a class instance by adding parentheses after the class name. Use the dot syntax to access instance attributes and methods.

Var shape = Shape ()

Shape. numberOfSides = 7

Var shapeDescription = shape. simpleDescription ()

Some important things of the Shape class in this version are not: A constructor sets the class when creating an instance. 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 that self is used to distinguish the name attribute from the name parameter. The life of the constructor is the same as that of a function, except that the class instance is created. Each attribute must be assigned a value, either in the Declaration or in the constructor.

Use deinit to create a destructor to clear objects during destruction.

Child classes include the names of their superclasses, separated by colons. You do not need to declare the standard root class, so you can ignore the superclass.

The subclass method can be used to mark override to reload the implementation in the superclass, and without override, the compiler will regard it as an error. The compiler also checks methods that are not overloaded.

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 length (sideLength ). "}} let test = Square (sideLength: 5.2, name:" my test square ") test. area () test. simpleDescription ()

Exercise

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

Attributes can include 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 "An equilateral triangle with sides of length (sideLength )."

}

}

Var triangle = EquilateralTriangle (sideLength: 3.1, name: "a triangle ")

Triangle. perimeter

Tritriangle. perimeter = 9.9

Triangle. sideLength

In the setter of perimeter, the new value is named newValue. You can provide a non-conflicting name after set.

Note that the constructor of EquilateralTriangle has three different steps:

Call the hyperclass constructor to change the attribute value of the hyperclass definition. Add additional work to use methods, getter, and setter.

If you do not need to calculate attributes, but you still need to perform the work after setting values, use willSet and didSet. For example, the following class must ensure that the side length of the triangle is 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 class methods and functions. The Parameter Name of the function is only used with the function, but the parameter name of the method can also be used to call the method (except the first parameter ). There is a parameter with the same name in a method, and the parameter itself is called. You can specify the second name and use it inside 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 optional values, you can write "? "Similar to the method property before the operator. If the value is "? "Previously nil, all in "? "Will be automatically ignored later, and the entire expression is nil. In addition, the optional values are unpackaged, all "? "All subsequent values are unpackaged values. In either case, the value of the entire expression is optional.

Let optionalSquare: Square? = Square (sideLength: 2.5, name: "optional square ")

Let sideLength = optionalSquare ?. SideLength

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.