Created
December 16, 2018 18:53
-
-
Save hitendradeveloper/1767821faf5000ef77b9d792bd042af1 to your computer and use it in GitHub Desktop.
Protocol Example - 6 : Protocol as a super-type : Medium blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
//MARK:- InformationDisplayble Protocol | |
protocol InformationDisplayble { | |
func displayInformation() | |
} | |
//MARK:- Employee Class | |
class Employee { | |
var employeeID: String = "EMP007" | |
var organizationName: String = "ISRO" | |
}//End: Employee | |
//MARK:- Employee extension | |
extension Employee: InformationDisplayble { | |
func displayInformation() { | |
print("Employee works in \(self.organizationName), employee ID is \(self.employeeID)") | |
} | |
} | |
//MARK:- Student Class | |
class Student { | |
var enrollmentNumber: String = "1153506930" | |
var universityName: String = "GTU" | |
}//End: Student | |
//MARK:- Student extension | |
extension Student: InformationDisplayble { | |
func displayInformation() { | |
print("Student studied in \(self.universityName), entrollment number is \(self.enrollmentNumber)") | |
} | |
} | |
//Global Function that can print information of InformationDisplayble | |
func displayInformation(of object: InformationDisplayble){ | |
object.displayInformation() | |
} | |
//creating instances | objects | |
let employee = Employee() | |
let student = Student() | |
//displaying created instances | |
displayInformation(of: employee) | |
displayInformation(of: student) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment