9.36. Swift inheritance

发布时间 :2023-12-08 01:10:06 UTC      

Inheritance can be understood as one class getting the methods and properties of another class.

When a class inherits other classes, the inherited class is called a subclass, and the inherited class is called a superclass (or parent class).

In Swift, classes can call and access superclass methods, properties, and subscript scripts, and can override them.

We can also add a property watcher to the inherited properties in the class.

9.36.1. Base class #

A class that does not inherit another class is called a Base Class.

In the following example, we define the base class StudDetails describing the student (stname) and his scores in each subject (mark1, mark2, mark3):

class StudDetails {
    var stname: String!
    var mark1: Int!
    var mark2: Int!
    var mark3: Int!
    init(stname: String, mark1: Int, mark2: Int, mark3: Int) {
        self.stname = stname
        self.mark1 = mark1
        self.mark2 = mark2
        self.mark3 = mark3
    }
}
let stname = "swift"
let mark1 = 98
let mark2 = 89
let mark3 = 76

let sds = StudDetails(stname:stname, mark1:mark1, mark2:mark2, mark3:mark3);

print(sds.stname)
print(sds.mark1)
print(sds.mark2)
print(sds.mark3)

The output of the above program execution is as follows:

swift
98
89
76

9.36.2. Subclass #

A subclass refers to creating a new class based on an existing class.

To indicate the superclass of a class, write the superclass name after the subclass name, separated by a colon (:). The syntax format is as follows

class SomeClass: SomeSuperclass {
    // Class definition
}

Example

In the following example, we define a superclass StudDetails and then use the subclass Tom inherit it:

class StudDetails
{
    var mark1: Int;
    var mark2: Int;

    init(stm1:Int, results stm2:Int)
    {
        mark1 = stm1;
        mark2 = stm2;
    }

    func show()
    {
        print("Mark1:\(self.mark1), Mark2:\(self.mark2)")
    }
}

class Tom : StudDetails
{
    init()
    {
        super.init(stm1: 93, results: 89)
    }
}

let tom = Tom()
tom.show()

The output of the above program execution is as follows:

Mark1:93, Mark2:89

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.