一、 实验目的:
1.掌握Swift扩展型
2.掌握Swift协议
3.掌握Swift类
二、实验原理:
1.Swift扩展的定义
2.Swift协议的定义
3.Swift类的定义
三、实验步骤及内容:
1.方法
//实例方法
class Website {
var visitCount = 0
func visiting(){
visitCount += 1
}
}
let sina = Website()
sina.visitCount
sina.visiting()
sina.visitCount
sina.visiting()
sina.visitCount
//带参数的方法
class Website {
var visitCount = 0
var visitor = String
var visitDate = “”
func visiting(visitor: String, visitDate : String){
visitCount += 1
self.visitor.append(visitor)
self.visitDate = visitDate
}
}
let sina = Website()
sina.visiting(visitor: “Tommy”, visitDate: “2016-6-1”)
sina.visitCount
sina.visitor
sina.visitDate
//类型方法
class Website {
static var visitCount = 0
static var visitor = String
static var visitDate = “”
static func visiting(visitor:String, visitDate : String){
visitCount += 1
self.visitor.append(visitor)
self.visitDate = visitDate
}
}
Website.visiting(visitor:“Tommy”, visitDate: “2016-6-1”)
Website.visitCount
Website.visitor
Website.visitDate
//结构体可变方法
struct Website {
var visitCount = 0
mutating func visiting() {
visitCount += 1
}
}
var sohu = Website()
sohu.visiting()
print(“(sohu.visitCount)”)
//下标方法
class Website {
var visitCount = 0
var visitor = String
var visitDate = “”
func visiting(visitor:String, visitDate : String){
visitCount += 1
self.visitor.append(visitor)
self.visitDate = visitDate
}
subscript(index : Int) -> String {
get {
return visitor[index]
}
set {
visitor[index] = newValue
}
}
}
var sina = Website()
sina.visiting(visitor: “Tom”, visitDate: “2016-6-3”)
sina.visiting(visitor: “Sam”, visitDate: “2016-6-9”)
print(“(sina[0])”)
sina[1] = “Pennie”
print(“(sina[1])”)
2.类
//基类
class Student {
var name = “”
var age = 0
var id = “”
var basicInfo : String {
return “(name) is (age) years old, the id is (id”
}
func chooseClass(){
print(“(name) choose a class.”)
}
func haveClass(){
print(“(name) have a class.”)
}
}
let theStudent = Student()
theStudent.name = “Tommy”
theStudent.age = 19
theStudent.id = “37060115”
print(theStudent.basicInfo)
//子类
class Graduate : Student {
var supervisor = “”
var researchTopic = “”
func chooseSuperVisor(superVisor:String){
self.supervisor = superVisor
}
}
let theGraduate = Graduate()
theGraduate.name = “Sam”
theGraduate.age = 23
theGraduate.id = “SY0602115”
theGraduate.haveClass()
theGraduate.researchTopic = “Graphics”
theGraduate.chooseSuperVisor(superVisor: “Ian”)
print(“Graduate (theGraduate.name) is (theGraduate.age) and the id is (theGraduate.id), The research topic is (theGraduate.researchTopic) and supervisor is (theGraduate.supervisor)”)
//子类的子类
class Doctor: Graduate {
var articles = String
func publishArticle(article : String){
articles.append(article)
}
}
let theDoctor = Doctor()
theDoctor.name = “Pennie”
theDoctor.age = 26
theDoctor.id = “BY0607120”
theDoctor.basicInfo
theDoctor.chooseSuperVisor(superVisor: “Ellis”)
theDoctor.supervisor
theDoctor.publishArticle(article: “Petri nets theory”)
theDoctor.publishArticle(article: “Process management”)
theDoctor.articles
//多态性
class Student {
var name = “”
var age = 0
var id = “”
var basicInfo : String {
return “(name) is (age) years old, the id is (id”
}
func chooseClass(){
print(“(name) choose a class.”)
}
func haveClass(){
print(“(name) have a class.”)
}
}
class Graduate : Student {
var supervisor = “”
var researchTopic = “”
override var age : Int {
didSet {
print(“age is set from (oldValue) to (age”)
}
willSet {
print(“original age will be set to (newValue)”)
}
}
override var basicInfo: String{
return super.basicInfo + “, supervisor is (supervisor), research topic is (researchTopic)”
}
func chooseSuperVisor(superVisor:String){
self.supervisor = superVisor
}
override func chooseClass() {
print(“graduate (name) choose a class”)
}
}
func sportGameRoster(stu:Student)->String{
return “Athlete name:(stu.name),age:(stu.age),id:(stu.id)”
}
let studenteTom=Student()
studenteTom.name=“Tom”
studenteTom.age=19
studenteTom.id=“37060116”
let graduateJim=Graduate()
graduateJim.name=“Jim”
graduateJim.age=24
graduateJim.id=“SY060218”
let rosterTom=sportGameRoster(stu: studenteTom)
let rosterJim=sportGameRoster(stu: graduateJim)
3.扩展
//扩展计算属性
extension Int {
var kg : Int {return self * 1000 }
var ton : Int {return self * 1000000}
}
var aChick = 2.kg
var aElephant = 8.ton
print(“aClick is (aChick)g”)
print(“aElephant is (aElephant)g”) //扩展计算属性
extension Int {
var kg : Int {return self * 1000 }
var ton : Int {return self * 1000000}
}
var aChick = 2.kg
var aElephant = 8.ton
print(“aClick is (aChick)g”)
print(“aElephant is (aElephant)g”)
//扩展构造器
struct Student {
var name : String
var id : String
var age : Int
init(){
name = “”
id = “”
age = 0
}
}
extension Student {
init(name : String, id : String, age : Int){
self.name = name
self.id = id
self.age = age
}
}
let theStudent = Student(name: “Jack”, id: “37060115”, age: 18)
//扩展方法
struct Student {
var name : String
var id : String
var age : Int
init(){
name = “”
id = “”
age = 0
}
}
extension Student {
init(name : String, id : String, age : Int){
self.name = name
self.id = id
self.age = age
}
func description(){
print(“(name)'s age is (age) and id is (id)”)
}
}
let theStudent = Student(name: “Jack”, id: “37060115”, age: 18)
theStudent.description()
//扩展下标
struct Student {
var name : String
var id : String
var age : Int
init(){
name = “”
id = “”
age = 0
}
}
extension Student {
init(name : String, id : String, age : Int){
self.name = name
self.id = id
self.age = age
}
}
struct Class {
var students = Student
}
extension Class {
subscript(Index : Int) -> String{
return students[Index].name
}
}
var theClass = Class()
theClass.students.append(Student(name: “Sam”, id: “0”, age: 16))
theClass.students.append(Student(name: “Tom”, id: “1”, age: 18))
theClass.students.append(Student(name: “Jeff”, id: “2”, age: 19))
let oneStudentName = theClass[0]
print(oneStudentName)
4.协议
//协议的声明
protocol Person {
var name: String { get set}
var age: Int { get }
}
//类遵守协议
class Student: Person {
var name : String
var age : Int
init(){
name = “”
age = 0
}
}
//协议中的关联类型
protocol Person {
associatedtype UnknownType
var name: String { get set}
var age: Int { get }
var weight: UnknownType { get }
}
class Student: Person {
var name : String
var age : Int
var weight: Double
init(){
name = “”
age = 0
weight = 0.0
}
}
//协议中的关联类型
protocol Person {
associatedtype UnknownType
var name: String { get set}
var age: Int { get }
var weight: UnknownType { get }
}
class Student: Person {
var name : String
var age : Int
var weight: Double
init(){
name = “”
age = 0
weight = 0.0
}
}
//协议中的关联类型
protocol Person {
associatedtype UnknownType
var name: String { get set}
var age: Int { get }
var weight: UnknownType { get }
}
class Student: Person {
var name : String
var age : Int
var weight: Double
init(){
name = “”
age = 0
weight = 0.0
}
}
//协议的继承性
protocol Person {
associatedtype UnknownType
var name : String {get set}
var age : Int {get set}
var weight: UnknownType { get }
func personDescription()
}
protocol Student {
var school : String {get set}
func studentDescription()
}
protocol Graduate : Person, Student {
var supervisor : String {get set}
func graduateDescription()
}
class ComputerAssociationMember : Graduate {
var name : String = “”
var age : Int
var weight: Double
var school : String
var supervisor : String
func personDescription() {
print(“It’s person description”)
}
func studentDescription() {
print(“It’s student description”)
}
func graduateDescription() {
print(“It’s graduate description”)
}
init(name : String, age : Int, weight: Double, school : String, supervisor :String){
self.name = name
self.age = age
self.weight = weight
self.school = school
self.supervisor = supervisor
}
}
let theMember = ComputerAssociationMember(name: “Tom”, age: 23, weight: 69.2, school: “BUAA”, supervisor: “Ian”)
theMember.personDescription()
theMember.studentDescription()
theMember.graduateDescription()
四、实验结果与分析:
本次实验,我主要学习了Swift扩展&协议&类的使用。在本次的实验学习中,有时候会因为对基本概念不熟悉,导致编写语法使用不恰当,导致运行结果出错,但最后经过不但的改进与尝试,最后的结果都是可以正常运行出来的。
五、实验总结:
通过本次课程的学习,主要的了解如下:Swift中有三种type(类型):class(类),struct(结构体),enum(枚举):
这三个类型的区别在于class是引用类型,而另外两个是值类型。区别在于,引用类型的对象是可以动态分配的(可以变化),而值类型的对象在初始化时一般就分配好了(不准改动)。而mutating关键字则允许在它修饰的方法中去修改对象本身的值或对象属性的值(self或self.xxx的值)。
1.如果协议仅针对class,不需要mutating关键字。
2.如果协议会被enum或struct使用,协议中的方法需要mutating修饰。