13-Swiftのクラス
7144 ワード
一、クラスの紹介と定義
class :SuperClass{
//
}
二、クラスを定義する際の注意事項:
class Person{
var add:String?
var age:Int = 0
var name:String
init() {
name = "yijiang"
}
}
let p = Person()
三、クラスの構造関数
class Student:NSObject{
var name:String
var age:Int
//
// , “ ”
init(name:String,age:Int) {
self.name = name
self.age = age
}
//
override init() {
name = "jiang"
age = 0
}
}
let s1 = Student(name: "yijiang", age: 18)
s1.name //yijiang
let s2 = Student()
s2.name //jiang
四、クラスの構造関数
class YJWView:UIViewController{
//
var name:String?
var age:Int?
//
deinit {
print(" ")
}
}
//
extension YJWView{
override func viewDidLoad() {
}
}
//
extension YJWView{
func data() {
}
}
var p:YJWView? = YJWView()
p = nil // “ ”
五、クラスの属性
class Person:NSObject{
// :
var name:String = ""
var age:Int = 0
var score1:Double = 50
var score2:Double = 70
// , setter/getter , getter , setter ,
// :
var score3:Double{
get{
return (score1 + score2) / 2
}
set{
newValue
//
}
}
// :
static var personCount:Int = 0
override init() {
Person.personCount += 1
}
deinit {
Person.personCount -= 1
}
}
let p = Person()
p.score3 = 20 // newValue 20
p.score3 //60
let p2 = Person()
var p3:Person? = Person()
Person.personCount //3
p3 = nil
Person.personCount //2
六、傍受属性の変更
class Person{
// willSet/didSet
var name:String? = "yijiangwnag"{
willSet{
newValue
print(name ?? "")
print("---------")
}
didSet{
oldValue
print(name ?? "")
}
}
}
let p = Person()
p.name = "jiang"
p.name = "yijiang"
/*
:
yijiangwnag
---------
jiang
jiang
---------
yijiang
*/
七、類の方法
class Person{
//
func introduce(name:String,age:Int) {
print(" ",name,age)
}
//
static func intro(name:String){
print(" ",name)
}
}
let p = Person()
//
p.introduce(name: "yijiang", age: 18)
//
Person.intro(name: "wang")
/*
:
yijiang 18
wang
*/
八、三大特性
class Person{
func eat() {
print(" ")
}
}
class Man:Person{
// eat
override func eat() {
print(" ")
}
}
let p = Man()
p.eat()
//
class Num{
func read(num:Int) {
print(" ")
}
}
class NumE:Num{
func read(num: Float) {
print(" ")
}
func read(num: Int,num2:Int) {
print(" ")
}
}
let num = NumE()
num.read(num: 12) //
num.read(num: 12.0) //
num.read(num: 1, num2: 2) //
class Printer{
func printer(){
print(" ")
}
}
class ColorPrinter:Printer{
override func printer() {
print(" ")
}
}
class BlackPrinter:Printer{
override func printer() {
print(" ")
}
}
func work(printer:Printer){
// printer Printer
printer.printer()
}
let p = Printer()
let colorP = ColorPrinter()
let blackP = BlackPrinter()
work(printer: blackP) //
九、類と構造体の違い
struct PersonS{
var name:String
func run() {
print(" ")
}
}
class PersonC{
var name:String
func run() {
print(" ")
}
init() {
name = "zhangsan"
}
}
//
struct PersonS{
var name:String
func run() {
print(" ")
}
}
//
class PersonC{
var name:String
func run() {
print(" ")
}
init() {
name = "zhangsan"
}
}
//
let ps = PersonS(name: "zhangsan")
ps.name //zhangsan
ps.run() //
var pss = ps
pss.name = "yijiang"
ps.name //zhangsan
//
let pc = PersonC()
pc.name //zhangsan
pc.run() //
let pcc = pc
pcc.name = "yijiang"
pc.name //yijiang