Object-C ---) Swift (Class 8) and struct

Source: Internet
Author: User

Object-C ---) Swift (Class 8) and struct

In Swift, classes and struct have high similarity. The main differences between the two are:
1. struct does not support inheritance
2. struct does not support definition of the destructor
3. struct is a value type, while class is a reference type.

Syntax format of the definition class:
[Modifier] class name {zero to multiple constructors zero to multiple attributes zero to multiple methods zero to multiple subscript}

For a class definition, there are three most common members: constructor, attribute, and method. If the programmer does not provide constructor for a class, the system provides a default, non-parameter constructor for this class. Once the programmer provides the constructor for the class, the system will not provide the default constructor for the class.

# Define the structure syntax format:
[Modifier] struct name {zero to multiple constructors zero to multiple attributes zero to multiple methods zero to multiple subscript}

Struct cannot define final, Because struct does not support inheritance. If the programmer does not provide a constructor for the struct, the system provides two constructor for the struct: A non-parameter constructor and a constructor that initializes all storage properties.

Syntax:
[Modifier] func method name (parameter list)-[-> return value type] {// specific function code}

If you want to define a class method, you can use the class modifier.

class Person{var name:String=""var age:Int=0func say(content:String){print(content)}   }

No constructor is defined in the Code above. The system will provide a default non-parameter constructor for it, but this default constructor will not assign an initial value to the storage attribute, therefore, programs must specify initial values for these storage attributes.
Note: The class is a reference type.

Var p: Person p = Person () p. name = "" var p1 = p p1.name = "Sun Wukong" print (p. name) // output Sun Wukong
Reference Type comparison

The reference type has two operators: = and! =, These two operators can only be used to compare the number of referenced types. When two referenced variables point to the same instance, and two referenced variables point to the same instance, = true for comparison. When two referenced variables do not point to the same instance ,! = Compare to get true

Class Person {var name: String = "" var age: Int = 0 init (name: String, age: Int) {self. name = nameself. age = age} func say (content: String) {print (content)} var p1 = Person (name: "Sun Wukong", age: 2100) var p2 = Person (name: "maid", age: 48) print (p1 = p2) // outputs falseprint (p1! = P2) Output truevar p3 = p1print (p1 = p3) // Output true
Let's talk about struct.
Struct Monkey {var name: Stringvar age: Intfunc say () {print ("\ (name) age \ (age)") }}var monkey = Monkey (name: "Sun Wukong", age: 1500) print (monkey. name)

The code above calls one of the constructors with two parameters of Monkey to create an instance. The struct creates two constructors by default: A constructor without parameters and a constructor that initializes all storage properties.
When no parameter constructor is called

Var monkey = Monkey () print (monkey. age) // output 0

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.