60 likes | 226 Views
CPSC 233 Tutorial. Xin Feb 2, 2011. Scoping rules. Class Foo { int x ; // attribute, valid in the class ✓ Func1 (){ int x ; // shadowing ✗ int y = 0; // local variable. Valid here after till the end of Func1 ✓
E N D
CPSC 233 Tutorial Xin Feb 2, 2011
Scoping rules Class Foo { intx; // attribute, valid in the class ✓ Func1 (){ intx; // shadowing ✗ inty = 0; // local variable. Valid here after till the end of Func1 ✓ for (inti = 0; i < 3; i ++){ // i is local variable, valid in the for ✓ double k = y + 3; // local variable, valid in the for ✓ y += 3; // ✓ } i = 5; // ✗ Func2 (); // ✓ } Func2 () { // u = 3.0; // ✓ t = 5.f; //✗ float t; // ✓ } double u; // attribute, valid in the class ✓ }
Associations • aggregation relationship (has-a relation) • public class Engine • { • public boolean ignite () { .. } • } • public class Car • { • private Engine anEngine; • private Light headLight; • public start () • { • anEngine.ignite (); • headLights.turnOn (); • } • } public class Light { private booleanisOn; public void turnOn () { isOn = true;} }
Car Light Car Student Lecture Light Direction Bidirectional associations
Car Light Car Owner Car Engine Multiplicity 10 1 1 * 1 1
Assignment 2 Q&A • Due on Friday at 4 pm