Swift Learning Day 11th: definition of class

Source: Internet
Author: User
Tags uikit

One:

Main content
    • Introduction and definition of the class
    • Properties of the class
    • Constructors for classes
I. Introduction and definition of classes
    • Swift is also a language for object-oriented development
    • The Object-oriented Foundation is the class, and the class produces the object
    • How do you define classes in swift?
      • Class is a keyword in swift that defines a class
class 类名 : SuperClass {    // 定义属性和方法}
  • Attention:
    • Defines a class that can have no parent class. then the class is Rootclass
    • typically, when you define a class. inherit from NSObject (non-oc Nsobject)
    • Import UIKit/*1. Definition of Class 2. Create the object that corresponds to the class 3. Assign a value to the property of a class 1> direct assignment 2> through KVC Assignment 4. You can override SetValue (value:anyobject?, Forundefinedkey key:, then the word A field that is not in the code can have no corresponding attribute in the class 5.override: override, if one of the methods written is an override of the method of the parent class, you must precede the method with the override*/classperson:nsobject {var age:int=0        //Override: Override if one of the methods written is an override to the method of the parent class, you must precede the method with the override    OverrideFunc SetValue (_ value:any?, Forundefinedkey Key:string) {}}let P=person ()//p.age =P.setvaluesforkeys ([" age": -,"name":" why"]) Print (p.age)

    • Import UIKitclassPerson:nsobject {//class : Declare the keyword of a class, declare a class, you can inherit, or you can not inherit//define the properties of the Class: optional type definition, which can be nil or nilvar name:string?var age:int=0var height:double=0.0}let P= Person ()//Creating Objects//Note: the assignment is not to call the set method, directly to the property to assign a value: inheritance NSObject can also use KVC assignment, if the class attribute cannot find the corresponding key value, will cause a crash, You can override Setvalueforunderdefinekey in a class to implementP.name =" why"P.age= -P.height=1.88


Swift Learning Day 11th: definition of class

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.