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