Difference between revisions of "W1412 Constructors"
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[File: | [[File:Louis-Emile Durandelle, The Eiffel Tower - State of the Construction, 1888.jpg|thumb|right|Louis-Emile Durandelle, The Eiffel Tower - State of the Construction, 1888]] | ||
== Prerequisites == | |||
* [[W1215 Function Signatures]] | |||
* [[W1251 Parameter Passing]] | |||
== Research == | == Research == | ||
Line 12: | Line 12: | ||
== Experiment == | == Experiment == | ||
Create a directory within your " | Create a directory within your "Experiences" directory: | ||
{{ConsoleLine|john-williams@codermerlin:~$|cd ~/Experiences}} | |||
cd ~/ | {{ConsoleLine|john-williams@codermerlin:~/Experiences$|mkdir W1412}} | ||
mkdir | {{ConsoleLine|john-williams@codermerlin:~/Experiences$|cd W1412}} | ||
Create a new swift project: | |||
{{ConsoleLine|john-williams@codermerlin:~/Experiences/W1412$|swift-init}} | |||
Edit a new file named | Edit a new file named {{Pathname|main.swift}}: | ||
{{ConsoleLine|john-williams@codermerlin:~/Experiences/W1412$|emacs main.swift}} | |||
emacs main.swift | |||
For this project you'll repeatedly edit the same file. It's probably easiest to test this file ''within emacs'' by typing: | For this project you'll repeatedly edit the same file. It's probably easiest to test this file ''within emacs'' by typing: | ||
{{Key|F5}} {{Key|r}} and observing the results. For each question, be sure to '''understand''' the behavior before proceeding to the next question. It will be helpful to '''record''' your answers and reasoning for later review. | |||
<quiz shuffleanswers=true display=simple> | <quiz shuffleanswers=true display=simple> | ||
Line 319: | Line 316: | ||
</quiz> | </quiz> | ||
{{KeyConcepts| | |||
* '''Initialization''' is the process of preparing an instance of a class, structure, or enumeration type for use. | * '''Initialization''' is the process of preparing an instance of a class, structure, or enumeration type for use. | ||
** Initialization '''requires''' that we '''provide an initial value for each stored property'''. | ** Initialization '''requires''' that we '''provide an initial value for each stored property'''. | ||
Line 331: | Line 328: | ||
* Because the initializer must be called ''init'', '''argument labels''' play a crucial role for specifying the correct initializer. | * Because the initializer must be called ''init'', '''argument labels''' play a crucial role for specifying the correct initializer. | ||
* Constant properties may be assigned at any point during initialization, but once assigned, may not be further modified. | * Constant properties may be assigned at any point during initialization, but once assigned, may not be further modified. | ||
}} |
Latest revision as of 21:24, 22 March 2021
Prerequisites[edit]
Research[edit]
Initialization is the process of preparing an instance of a class, structure, or enumeration type for use. It requires that we provide an initial value for each stored property. We may also perform any other additional initialization that we require.
- Read Initialization (through "Initializer Delegation for Value Types") (Swift Documentation)
- Read Initialization (First paragraph only) (Wikipedia)
- Read Computer Science I Textbook (by Bourke) Chapter 10.1.2. Creating
Experiment[edit]
Create a directory within your "Experiences" directory:
john-williams@codermerlin:~$ cd ~/Experiences
john-williams@codermerlin:~/Experiences$ mkdir W1412
john-williams@codermerlin:~/Experiences$ cd W1412
Create a new swift project:
john-williams@codermerlin:~/Experiences/W1412$ swift-init
Edit a new file named main.swift:
john-williams@codermerlin:~/Experiences/W1412$ emacs main.swift
For this project you'll repeatedly edit the same file. It's probably easiest to test this file within emacs by typing: F5 r and observing the results. For each question, be sure to understand the behavior before proceeding to the next question. It will be helpful to record your answers and reasoning for later review.
- Initialization is the process of preparing an instance of a class, structure, or enumeration type for use.
- Initialization requires that we provide an initial value for each stored property.
- Initialization is handled by defining initializers.
- Unlike functions and methods, initializer do not return a value.
- Initial values for stored properties may be:
- Set within an initializer, or
- by setting a default value value when the property is defined
- The simplest initializer is simply defined as init(), with no parameters. Note that is must be called init.
- As with other functions and methods, a parameter list may be provided.
- Because the initializer must be called init, argument labels play a crucial role for specifying the correct initializer.
- Constant properties may be assigned at any point during initialization, but once assigned, may not be further modified.