forked from studoverse/Kotlift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
19_interfaces.swift
90 lines (75 loc) · 1.36 KB
/
19_interfaces.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Simple Interface
protocol MyInterface {
func bar() -> String
/*fun foo(): Int {
return 1
}*/
}
public class Implementation: MyInterface {
func bar() -> String {
return "2-Implementation"
}
init() {
}
}
// Interface + Inheritance
public class Parent {
func three() -> Int32 {
return 3
}
var four = 4
init() {
}
}
public class Child: Parent, MyInterface {
let five = 5
func six() -> String {
return "6"
}
func bar() -> String {
return "2-Child"
}
override init() {
}
}
// Abstract Interface + Inheritance
public class AbstractParent {
func three() -> Int32 {
fatalError("Method is abstract")
}
var four = 4
init() {
}
}
public class AbstractChild: Parent {
override init() {
}
}
public class NonAbstractChild: AbstractChild, MyInterface {
func bar() -> String {
return "2-NonAbstractChild"
}
override init() {
}
}
func main(args: [String]) {
// Simple Interface
let obj = Implementation()
print(obj.foo())
print(obj.bar())
// Interface + Inheritance
let child = Child()
print(child.foo())
print(child.bar())
print(child.three())
print(child.four)
print(child.five)
print(child.six())
// Abstract Interface + Inheritance
let naChild = NonAbstractChild()
print(naChild.foo())
print(naChild.bar())
print(naChild.three())
print(naChild.four)
}
main([])