Quick Index
Ivar Concept Sketches
Ivar Access Example Using 'this'
Index
Ivar Concept Sketches
One Class
Ivars On Object Instances
Ivar Access Example Using 'this'
An Example Class
Accessing Ivars
Target-Dot-Ivar
Coding Access to Ivar
Ivar Concept Sketches
Instance variables (ivars) are variables on a given object instance.
Recall that from one class we
construct many object instances
.
Ivars are typically considered
private
to the object.
One Class
Our simple Circle class (defintion). We define one ivar "r" (the radius of the circle).
Ivars On Object Instances
Now we construct many Circle object instances.
One circle has r=1, another r=2, etc.
Ivar Access Example Using 'this'
An Example Class
This class has two ivars (width and height).
We want to access the ivars for use.
Ivars are generally private so access would only be by the object that owns them
.
Accessing Ivars
Accessing ivars is done similarily to sending messages.
Target-Dot-Ivar
We have a
general
pattern we can always use to access an ivar:
target-dot-ivar
Syntax may vary slightly between programming languages.
Here is a pattern for accessing ivars:
<target><dot><ivar>
It is very similar to our previous pattern for sending messages:
<target><dot><msg>()
target = the target (receiver) object owning the ivar
dot = .
msg = the ivar name
Coding Access to Ivar
Using
"target-dot-ivar"
we write code to access an ivar:
this.width
Where:
target = "this" dot = . ivar = "width"
📋
Coding Fundamentals
Chapter 101 - Concepts
Ivars
Search Site
Home
Back To Top