Swift Learning-Definition of class, use, inheritance, construction, etc. (v)

Source: Internet
Author: User
Tags class definition

Swift Learning -- the definition of class, used. inheritance, construction, etc. (v.)

Instructions for using the class

1 using class and class names to create a class name, such as: class student

The only difference between the declaration of a property in class 2 and a constant and a variable is that their context is a class

3 methods and function declarations are the same

// 1 ---- simple class introduction
class Student {
    // The variable student number is initialized to 0
    var num = 0;
    // member function
    func GetNum ()-> Int {
        return num

// Create an instance of the class

var student = Student ()

// Access the member variables of the class

student.num = 1

// Access member functions of the class
var Num = student.GetNum ()

// Print out class variables and member functions
println ("student1 num is \ (student.num)")
println ("student2 num is \ (Num)")

// 2 ---- Constructors and destructors in the class

/ *
    Constructor init
    Destructor deinit
* /

class StudentLow {
    var numberlow: Int = 0
    var namelow: String
    //Constructor. Constructor to assign
    init (namelow: String) {
        / *
            self is used to distinguish instance variables. When you create an instance, you give the class the same as a function parameter
            The parameters passed to the constructor.
Every property needs to be assigned a value-whether through numberlow
            Still through the constructor (like namelow)
        * /
        self.namelow = namelow
    func simpleDescription ()-> String {
        return "I am is low studnet"

// Note that my constructor has parameters, here I create the instance object. The parameters are correct

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 ---- inherit class. Create a subclass of StudentLow

/ *
    Inheritance note:
        Subclasses are defined by appending the name of the parent class to their class name and cutting them with a colon.
        Creating a class does not require a standard root 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
        super.init (namelow: name)
        numberlow = 3
    func Grade ()-> Double {
        return grade * 0.9
    // If the subclass assumes that you want to override the method of the parent class, you need to use the override tag-assuming that the override of the parent method without adding override, the compiler will report an error.
    // The compiler will also check if the method marked by override is indeed in the parent class.
    override func simpleDescription ()-> String {
        return "I am a hight student"

// Create an instance object

let studenthight1 = StudentHight (grade: 98, name: "lucy")

println ("studenthight1 num is \ (studenthight1.numberlow)")

println ("studenthight1 name is \ (studenthight1.namelow)")

//call function
println ("Greades is \ (studenthight1.Grade ())")

println ("studenthight1 grade is \ (studenthight1.grade)")

//call function
println ("studenthight1 information is \ (studenthight1.simpleDescription ())");

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

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.