110 likes | 250 Vues
Classes and Instance Variables. Classes. Only classes we know are arrays Much of the terminology for classes/objects comes from everyday life class = type of unit (Student) object = member of a class (Zack). Let’s think about a car. Manufacturer Color # of doors Transmission (A/M)
E N D
Classes • Only classes we know are arrays • Much of the terminology for classes/objects comes from everyday life • class = type of unit (Student) • object = member of a class (Zack)
Let’s think about a car • Manufacturer • Color • # of doors • Transmission (A/M) • # of gears • Could go into more detail, but this serves our purpose quite well
How can we represent this using variables in Java? • String for Manufacturer • String for color • int for # of doors • boolean for transmission (T for auto, F for manual) • int for # of gears
Creating a (Dream) Car String f430Manu = “Ferrari”; String f430Color = “red”; int f430Doors = 2; boolean f430Trans = false; int f430Gears = 6; • Pass to method requires each var individually
Car Class public class Car { public String manufacturer; public String color; public int numDoors; public boolean trans; public int numGears; } // • Now we have Cardata type! (We can allocate objects of type Car)
Creating a Dream Car (better way) Car f430 = new Car(); f430.manufacturer = “Ferrari”; f430.color = “red”; f430.numDoors = 2; f430.trans = false; f430.numGears = 6; • Pass to method only requires me to send f430.
Important Vocabulary • Class – a blueprint • Object – creation based on a class • Instance Variables – variables declared as part of a class, but are not part of a method
Blueprints and Classes • Blueprints used to build many houses • Classes used to “build” many objects • Each house has own stuff • Change one, but not other • Each class has own stuff • Change one, but not other