您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 电子设计/PCB > 《IOS高级应用开发技术》实验报告二详细答案
实验编号:2四川师大《IOS应用开发技术》实验报告2017年9月23日计算机科学学院2015级班实验名称:Swift类和子类姓名学号:指导老师:________实验成绩:_____实验_二__Swift类和子类一.实验目的及要求用Swift语言编写类和子类,掌握类和子类的定义及使用二.实验内容(1)实现Person类:a)要求具有firstName,lastName,age,gender等存储属性,fullName计算属性;其中gender是枚举类型(male,female);b)具有指定构造函数和便利构造函数;c)两个Person实例对象可以用==和!=进行比较;d)Person实例可以直接用print输出;e)Person增加run方法(方法里面直接print输出PersonXXXisrunning;(2)从Person分别派生Teacher类和Student类:a)Teacher类增加属性title,实例可以直接用print输出;b)Student类增加属性stuNo,实例可以直接用print输出;c)Teacher和Student重载run方法(方法里面直接print输出TeacherXXXisrunning和StudentXXXisrunning)(3)分别构造多个Person、Teacher和Student对象,并将这些对象存入同一个数组中;(4)新建一个协议SchoolProtocol,协议包括一个department属性(Enum,自己实现enum的定义)和lendBook方法(随便写点内容,能区隔即可);(5)修改Teacher和Student,让这两个类实现该协议;(6)对数组执行以下要求:a)分别统计Person、Teacher和Student对象的个数并放入一字典中,统计完后输出字典内容;b)对数组按以下要求排序并输出:age、fullName、gender+age;c)对数组进行穷举,调用每个对象的run方法,同时调用满足协议SchoolProtocol对象的lendBook方法;3、主要仪器设备及药品:iMac(MacOS、XCode)三.实验结果的分析与评价(该部分如不够填写,请另加附页)(1)找出从1-10000的所有质数,将找出的质数存入可变数组,对数组进行正反排序并输出结果(采用5种以上的方法实现);代码:funcsearchPrimNum()-[Int]{varchangeArray=[2,3]//定义可变数组varrecordIsPrim=0//用于记录判断质数forxin4...10000{foryin2..x{ifx%y==0{recordIsPrim+=1break}}ifrecordIsPrim==0{changeArray.append(x)}recordIsPrim=0}returnchangeArray}varprimNum=searchPrimNum()primNum.sort()//升序排序print(数组升序排列为:)print(primNum)/***************使其降序排列*******************///第一种实现数组降序排列方法funccompare(m:Int,n:Int)-Bool{returnmn}primNum.sort(by:compare)print(第一种数组降序排列为:)print(primNum)/////////////////第二种//////////////primNum.sort(by:{(m:Int,n:Int)-Boolinreturnmn})print(第二种数组降序排列为:)print(primNum)//////////////////第三种//////////////primNum.sort{(m,n)-Boolinreturnmn}print(第三种数组降序排列为:)print(primNum)//////////////第四种///////////////////primNum.sort{(m,n)inreturnmn}print(第四种数组降序排列为:)print(primNum)//////////////////第五种///////////////primNum.sort(by:)print(第五种数组降序排列为:)print(primNum)///////////////第六种/////////////////primNum.sort{$0$1}print(第六种数组降序排列为:)print(primNum)运行结果:(2)代码://性别枚举enumGender:Int{casemalecasefemalestaticfunc(lhs:Gender,rhs:Gender)-Bool{returnlhs.rawValuerhs.rawValue}}//department枚举enumDepartment:String{caseteachercasestudent}//协议protocolSchoolProtocol{funclendbook()vardepartment:Department{getset}}//人类classPerson:CustomStringConvertible{varfisrtName:StringvarlastName:Stringletage:Intvargender:Gendervarfullname:String{get{returnfisrtName+lastName}}//构造函数init(fisrtName:String,lastName:String,age:Int,gender:Gender){self.fisrtName=fisrtNameself.lastName=lastNameself.age=ageself.gender=gender}//便利构造函数convenienceinit(fisrtName:String,lastName:String){self.init(fisrtName:fisrtName,lastName:lastName,age:0,gender:Gender.male)}//此两个函数用于使两个Person实例对象可以用==和!=进行比较;staticfunc==(lhs:Person,rhs:Person)-Bool{returnlhs.fullname==rhs.fullname}staticfunc!=(lhs:Person,rhs:Person)-Bool{return!(lhs==rhs)}funcrun(){print(Person\(self.fullname)isrunning)}//使其直接用print输出vardescription:String{return全名:\(fullname)年龄:\(age)性别:\(gender)}}//老师类classTeacher:Person,SchoolProtocol{vartitle:Stringvardepartment:Departmentinit(title:String,fisrtName:String,lastName:String,age:Int,gender:Gender){self.department=Department.teacherself.title=titlesuper.init(fisrtName:fisrtName,lastName:lastName,age:age,gender:gender)}convenienceinit(fisrtName:String,lastName:String){self.init(title:hello,everyone,fisrtName:fisrtName,lastName:lastName,age:18,gender:.male)}overridevardescription:String{return标题:\(title)\(super.description)}overridefuncrun(){print(Teacher\(self.fullname)isrunning)}funclendbook(){print(Teacher\(self.fullname)lendonebookfromlibrary)}}//学生类classStudent:Person,SchoolProtocol{varstuNo:Intvardepartment:Departmentinit(stuNo:Int,fisrtName:String,lastName:String,age:Int,gender:Gender){self.stuNo=stuNoself.department=Department.studentsuper.init(fisrtName:fisrtName,lastName:lastName,age:age,gender:gender)}convenienceinit(fisrtName:String,lastName:String){self.init(stuNo:100110,fisrtName:fisrtName,lastName:lastName,age:18,gender:Gender.female)}overridevardescription:String{return\(super.description)学号:\(stuNo)}//重写overridefuncrun(){print(Student\(self.fullname)isrunning)}funclendbook(){print(Student\(self.fullname)borrowonebookfromlibrary)}}vart=Teacher(fisrtName:希,lastName:有)vars=Student(fisrtName:珍,lastName:琪琪)print(t)t.run()t.lendbook()print(.............................)print(s)s.run()s.lendbook()varp1=Person(fisrtName:贾,lastName:丽,age:19,gender:Gender.female)varp2=Person(fisrtName:李,lastName:好,age:21,gender:Gender.male)//varp2=Person(fisrtName:wa)print(...........................)//print(p2)//print(p1)//p2.run()//p1.run()//print(p1!=p2)//print(p1==p2)varrecordArray=[Person]()//构造多个Person、Teacher和Student对象foriin1...2{lettemporaryArrayPerson=Person(fisrtName:\(i),lastName:连,age:i+30,gender:Gender.male)recordArray.append(temporaryArrayPerson)}//分别统计Person、Teacher和Student对象的个数并放入一字典中foriin1...3{lettemporaryArrayTeacher=Teacher(title:你好,fisrtName:\(i),lastName:团,age:i+18,gender:.male)recordArray.append(temporaryArrayTeacher)}foriin1...4{lettemporaryArrayStudent=Student(stuNo:10000+i,fisrtName:\(i),last
本文标题:《IOS高级应用开发技术》实验报告二详细答案
链接地址:https://www.777doc.com/doc-3180040 .html