1 / 10

Classes and Instance Variables

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)

Télécharger la présentation

Classes and Instance Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes and Instance Variables

  2. 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)

  3. 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

  4. 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

  5. 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

  6. 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)

  7. 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.

  8. 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

  9. 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

More Related