官术网_书友最值得收藏!

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 and lastName. Use the arguments to initialize properties with the same names as the arguments. Display a message with the values for firstName and lastName when an instance of the class is created. Display a message with the values for firstName and lastName 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 the Employee 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 and lastName. Create an instance of the previously defined Employee 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 the Employee class later to perform the same task. However, first, you must understand how you can work with the properties defined in a class.
主站蜘蛛池模板: 同仁县| 元谋县| 台江县| 西充县| 邢台市| 金乡县| 长阳| 西乌珠穆沁旗| 开原市| 莫力| 淮北市| 夏津县| 高要市| 汉中市| 康马县| 防城港市| 黎平县| 古丈县| 周口市| 仪征市| 高安市| 图们市| 大港区| 沙雅县| 商都县| 平顺县| 阿巴嘎旗| 手游| 天全县| 汝城县| 禄劝| 怀化市| 长乐市| 昭通市| 福建省| 黎平县| 卓资县| 紫金县| 扎赉特旗| 安吉县| 双牌县|