Swift learning-class definition, use, inheritance, and construction (5)

Source: Internet
Author: User

Swift learning-class definition, use, inheritance, and construction (5)


Class instructions

1. Use the class and class Name to create a class name, for example, class student.

The attribute Declaration of the two classes is the same as that of the constant and the variable. The only difference is that their context is the class.

3. The method is the same as the function declaration.


// 1 ---- simple class introduction class Student {// variable Student ID initialized to 0 var num = 0; // member function func GetNum () -> Int {return num }}// create the class's instance object var student = Student () // The member variable student of the student class. num = 1 // The member function var Num = student Of the handler class. getNum () // print out the class variables and member functions println ("student1 num is \ (student. num) ") println (" student2 num is \ (Num )") // 2 ---- constructor and destructor in the class/* constructor init destructor deinit */class StudentLow {var numberlow: Int = 0 var namelow: string // constructor to assign values to in It (namelow: String) {/* self is used to differentiate instance variables. When you create an instance, you can pass in the constructor parameters to the class just like passing in function parameters. Each attribute must be assigned a value-whether through the declaration (numberlow) or through the constructor (like namelow) */self. namelow = namelow} func simpleDescription ()-> String {return "I am is low studnet"} // note that my constructor has parameters. When I create an instance object, var studentlow1 = StudentLow (namelow: "Jhon") studentlow1.numberlow = 2 println ("studentlow1 num is \ (studentlow1.numberlow )") println ("studentlow1 name is \ (studentlow1.namelow)") println ("studentlow1 information is \ (studentlow1. SimpleDescription () ") // 3 ---- inheritance class, create StudentLow's subclass StudentHight/* inheritance Description: The subclass definition method is to add the parent class name after their class names, use colons. You do not need a standard root class when creating a class, so you can ignore the parent class. */Class StudentHight: StudentLow {var grade: Double init (grade: Double, name: String) {self. grade = grade // note that the namelow: name parameter is passed to super. init (namelow: name) numberlow = 3} func Grade ()-> Double {return grade * 0.9} // subclass if you want to override the parent class method, override should be used to mark -- If override is not added, the compiler will report an error if the parent method is overwritten. // The Compiler also checks whether the methods marked by override are indeed in the parent class. Override func simpleDescription ()-> String {return "I am a hight student" }}// create the instance object let studenthight1 = StudentHight (grade: 98, name: "lucy ") println ("studenthight1 num is \ (bytes)") println ("studenthight1 name is \ (bytes)") // call the function println ("Greades is \ (studenthight1.Grade ()) ") println (" studenthight1 grade is \ (studenthight1.grade) ") // call the println function (" studenthight1 information is \ (studenthight1.simpleDescription ())");


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.