- Object–Oriented Programming with Swift 2
- Gastón C. Hillar
- 227字
- 2021-07-23 14:29:42
Exercises
Now that you understand an instance's life cycle, it is time to spend some time in the Playground creating new classes and instances:
- Exercise 1: Create a new
Employee
class with a custom initializer that requires two string arguments:firstName
andlastName
. Use the arguments to initialize properties with the same names as the arguments. Display a message with the values forfirstName
andlastName
when an instance of the class is created. Display a message with the values forfirstName
andlastName
when an instance of the class is destroyed.Create an instance of the
Employee
class and assign it to a variable. Check the messages printed in the Playground's Debug area. Assign a new instance of theEmployee
class to the previously defined variable. Check the messages printed in the Playground's Debug area. - Exercise 2: Create a function that receives two
string
arguments:firstName
andlastName
. Create an instance of the previously definedEmployee
class with the received arguments as parameters for the creation of the instance. Use the instance properties to print a message with the first name followed by a space and the last name. You will be able to create a method and add it to theEmployee
class later to perform the same task. However, first, you must understand how you can work with the properties defined in a class.
推薦閱讀
- Python編程自學(xué)手冊
- Python自然語言處理實戰(zhàn):核心技術(shù)與算法
- 自制編譯器
- PostgreSQL 11從入門到精通(視頻教學(xué)版)
- Hands-On C++ Game Animation Programming
- 算法訓(xùn)練營:提高篇(全彩版)
- 程序設(shè)計基礎(chǔ)教程:C語言
- Mastering Android Game Development
- Python算法指南:程序員經(jīng)典算法分析與實現(xiàn)
- Mastering Git
- Statistical Application Development with R and Python(Second Edition)
- Principles of Strategic Data Science
- 天天學(xué)敏捷:Scrum團隊轉(zhuǎn)型記
- Learning Embedded Android N Programming
- 深入淺出Python機器學(xué)習(xí)