Difference between revisions of "W1412 Constructors"
m (Merlin moved page Project-1412 to W1412 Constructors: Improved navigation) |
|||
Line 11: | Line 11: | ||
== 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}} | ||
Edit a new file named | Create a new swift project: | ||
{{ConsoleLine|john-williams@codermerlin:~/Experiences/W1412$|swift-init}} | |||
emacs main.swift | |||
Edit a new file named {{Pathname|main.swift}}: | |||
{{ConsoleLine|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: | 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> |
Revision as of 21:22, 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.
Key Concepts[edit]
- 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.