Uploaded by Such Doge

Blackboard - EECS2030 - F21 - Lecture 4 - annotated

advertisement
Lecture 4
Part A
Call by Value Primitive vs. Reference Arguments
heador
o
f
m
em
ody
.
c
at
/
b
Y
vaHepar
a
w=ar
g
ument
&
r
e
f
/
f
Method Call: Callee vs. Caller
class A {
...
void m(T param) {
/* use of param */
}
}
%aa-i onldef-n-t-H.to
method
( called
avg
→
both cases
of pain
.
types
class B {
...
void n(...){
A co = new A();
co.m(arg);
} tcontext object
}
↳ application
(
of
context)
usage method
a
( caller)
.
Call by Value: Primitive Argument
eatT÷"
class Circle {
int radius;
void setRadius(int r) {
this.radius = r;
paan
}
}
¥
class CircleUser {
...
Circle c = new Circle();
int arg = 10;
c.setRadius(arg);
}
↳
}
argument
( ~
10
✓
1°
Eg
Circle
radius ☒ 10
Call by Value: Reference Argument
:¥!•÷÷
class Circle {
int radius;
Circle() {}
Circle(int r) {
this.radius = r;
}
void setRadius(Circle c) {
→ this.radius = c.radius;
}
}
class CircleUser {
...
Circle c = new Circle();
Circle arg = new Circle(10);
c.setRadius(arg);
.
}
}
¥
ÉiÉÉ%£f
""
c
→
t.de
radius to ÉdeUser radius ☒
main
.
10
Lecture 4
Part B
Call by Value Asserting Call by Value in JUnit
Call by Value: Re-Assigning Primitive Parameter
parameter
0
→
-
I
-
①
→
→
I
Given
g.
that
copy
a
"
→
argument
by
¥
"J
store
j=[
µµwee✗
É5" nochtngewdlbedonetoavg
f.
"
'
call
value
II.
to
"
.
↳ rather than
=
I
"
'
argument
I
.
Call by Value: Re-Assigning Reference Parameter
this line redirect avg.pt
> Will
executing
.
call by value
.
g. =p
.
I
-
↳
re-assigned
phasnotojhfbe.mg
re-assigned
np→¥[yrefofPBe¥
s"¥ ;;
✗
)£
param
(
3
been
by
the method
.
Call by Value: Calling Mutator on Reference Parameter
t=É%
"" :@ =p
.
←
¥.am?rg
.
side
caller
→
;;¥É!;¥:ÉÉ¥*hx
*ftp.je#ref0fPBetEy
!
:L
.es#I :*
:&
1--4
-
.
✗=3
able to
observe the
change.
the
*
Param
alias
.
.
£
;
=
-
(f)
Lecture 4
Part C
Aggregation and Composition Terminology, Modelling, and Implementation
Terminology: Container vs. Containee
f1
f2
container
✓container
Course
“Advanced OOP”
Faculty
name
te
“Jackie”
Faculty
name
te
“Jonathan”
title
eecs2030 prof
÷:¥#
Course
title
eecs3311
prof
s
“Software Design”
Student “Jim”
id
cs
!
fontaines
"ebYÉ.IE?tawYaifxa-.neemif
)
containers
"É¥fwdat
the
I
multiple
gesha.pl/aonta-nedbf-sdestwYedsdthgfod
when
or
✗
Container
other
exist
☒
.
.
Aggregation: Design
↳
lang;£padat class Course {
,
Java Implementation
container
language
"sÉ?ame
Design 1: Single Containee
"""
Abuse
•¥;µfas¥P¥ifa
↳
Faculty prof;
...
}
.
specific
ofanta-neetontaaeraggfegat.MG
Course
Of
prof Faculty
1
class Faculty {
...
}
namt¥
:;¥
contained
Design 2: Multiple Containees
of
list
student
"
A
:*
:&
:*:* I
class Student {
Course[] courses;
...
}
☐-i¥¥É→
a
Student
*
courses
Course
go.q.gg?.g....,,g......
name
of contained
class Course {
...
}
Lecture 4
Part D
Aggregation and Composition Building Aggregated Object Structure
±
¥÷÷÷÷:: *÷:¥É
Aggregation (1)
Course
title
prof
☒
Faculty
name
±:: ⇐¥ÉÉ÷*i*-☒*
i: :i
Faculty
name ,
Course
title
prof
eeiE☒→"%;¥
&
-
-0
OT
Course
title
prof
:*
)pwH→→"ñc+ha¥
""
YEA
"
carg
Faculty
name
Aggregation (2)
Student
id
cs
Faculty
name
te
Course
title
prof
☒☒☒
→
✓É¥¥i
.
③
.
p.ge/-TeCt-oJ
③ s.ge#Eo]
a
F-
¥
Course
title
prof
Faculty
name
te
"
%
"
⇐?⇐ÉÉ"eei¥s☒
€s ☒÷÷
"
"
8
'
③
Course
title
prof
Student
id
cs
Lecture 4
Part E
Aggregation and Composition Navigating Objects via Aggregation Links
Runtime Object Structure: Student, Course, Faculty
É
.
nah
e¥És-⑧
we
need class/method
using
contexts using
knowing
☒
the
f1
f2
Faculty
name
te
“Jackie”
Faculty
name
te
“Jonathan”
②
def
the
the
.
,
£
p
example
type info of attributes /emanation
.
links
in an
Course
title
eecs2030 prof
“Advanced OOP”
Course
title
eecs3311
prof
“Software Design”
s
Student “Jim”
id
cs
Dot Notation for Navigating Classes (1)
É
c.0
g.
}
⇐
O
/* Get thepstudent’s id.
*/ e. said
String getID() {
return
☐
C. 0
/* Title of ith course
*/
String getTitle(int i) {
.
-
.
e.gs#EgetTitle0e.g.sO.cs-i.getPnfc).getNY
¥¥÷ɥɥ¥g÷ÉÉ
ip.ge#f:;ge-Y;this :D ,
.
}
Faculty
name
te
“Jackie”
Faculty
name
te
“Jonathan”
☒
f2
-
C. 0
/* Name of
→
* ith course’s instructor
*/
String getName(int i) {
}
☒÷÷T¥É_☒←-¥
f1
→
Course
title
eecs2030 prof
“Advanced OOP”
Course
title
eecs3311
prof
“Software Design”
☐→
s
É%
Student “Jim”
id
cs
Dot Notation for Navigating Classes (2)
ÉÉ÷g÷
✓
?ÉHnÉ¥µ¥
this
/* Get course’s title.
*/
String getTitle() {
/* Name of instructor
*/
String getName() {
}
}
/* Title of instructor’s
* ith teaching course
*/
String getTitle(int i) {
'
ve-wm-t-s-i-e-e-wmt.is#-ge--e--.-h-i.E-.g--&-.E&.g¥¥
f1
f2
Faculty “Jackie”
name
te
Faculty
name
te
“Jonathan”
}
Course
title
eecs2030 prof
“Advanced OOP”
Course
title
eecs3311
prof
“Software Design”
,
s
Student “Jim”
id
cs
.
Dot Notation for Navigating Classes (3)
I
/* Name of instructor
*/
String getName() {
return this
name 5
/* Title of instructor’s
* ith teaching course
*/
String getTitle(int i) {
retfh-s.tt?ge-T--.
}
f1
⇐
}
Faculty
name
te
“Jackie”
Faculty
name
te
“Jonathan”
Course
title
eecs2030 prof
“Advanced OOP”
Course
title
eecs3311
prof
“Software Design”
¥É÷÷:É÷☐ T?-
f2
s
Student “Jim”
id
cs
Lecture 4
Part F
Aggregation and Composition Implementing
Composition via Copy Constructors
Composition: No Sharing
¥1:*:*
→
¥
o
,
€=
=
*tramp
Sitton
.
000
% ↳
*
L
-
D
-
→
are there
multiple
each
for
aliases
?
file object
¥
sharing
files
.
of
.
Composition: Copy Constructor (Shallow Copy)
atast.Fdlofna.mg
call by Nate :dha=di
I
3
D-
É→a¥\
* ÷ ÷÷÷:!¥
-
✗
laser :
;É¥¥÷;¥%aii%"¥
pain)
great
nolongerthe
:D
(
.
→ this
↳
↳
¥
-
④
✗
""
dz
.
.
-
.
Composition: Copy Constructor (Deep Copy)
⑦
•
←
=
others
-
>
calling overloaded as
÷÷÷:::÷¥÷
✓
call to a afforestation
another
constrictor
ahdptrmethod
.
cousin
;E
Yi
,aaGI→→→
nt¥¥ÉET÷µµ
.
.
.
00
.
☐
I
.fi/es-ojE*uE#-E-&-nt-iEY~:p..
*
z-E
3
→ 0
other
'
•
O
]
-
Exercise: Copy Constructor (Composition?)
↳ True -1
d¥
-
1,2 ,
-
*
✗
1¥
shaving
.
.
:}
.
ya
→
I
other
.
src
①
* ties
;¥⇐
Lecture 4
Part G
Aggregation and Composition Example and Exercise
Modelling: Aggregation vs. Composition
→
Composition
r
-
\G§gga;;¥g
Exercise
>
↳ declare Java
classes and attributes
.
for
implementing
design
.
this
✓
✓
Implementation: Aggregation or Composition
What if the
author field
e.
¥7
F
↳
✗
1865
↳
to
is visible
changecontainers
>
( books
o
✗ 465
modified ?
gets
✓
-
unchanged
Ichange only applied
↳
>
sharing
owning
this author page
.
to F1S
container
.
Download