- 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.
推薦閱讀
- Getting Started with Citrix XenApp? 7.6
- CockroachDB權威指南
- MySQL 8 DBA基礎教程
- C語言程序設計案例式教程
- Python機器學習算法與實戰
- Advanced Oracle PL/SQL Developer's Guide(Second Edition)
- 組態軟件技術與應用
- C程序設計實踐教程
- Mastering Android Development with Kotlin
- Android程序設計基礎
- jQuery Mobile移動應用開發實戰(第3版)
- Terraform:多云、混合云環境下實現基礎設施即代碼(第2版)
- Android項目實戰:手機安全衛士開發案例解析
- 零基礎學C++(升級版)
- Python一行流:像專家一樣寫代碼