1 / 0

Section 6.1

Section 6.1 . CS 106, Fall 2013. The Big Q. What do we get by being able to define a class?! Or Do we really need this?!. What do you think?. How to represent a playing card?. A card is a tuple with 2 parts, a suit (one of “s”, “d”, “c”, “h”) and a denom (2 – 14).

breck
Download Presentation

Section 6.1

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

    CS 106, Fall 2013
  2. The Big Q What do we get by being able to define a class?! Or Do we really need this?!
  3. What do you think?
  4. How to represent a playing card? A card is a tuple with 2 parts, a suit (one of “s”, “d”, “c”, “h”) and a denom (2 – 14). We create a card by making a tuple. We access the suit via card[0] and denom via card[1]. What is good and what is bad about this implementation?
  5. What types of variables can we make? Is this good enough?
  6. Second big Q What defines a type? Data + operations what you can store. what you can do to or with it.
  7. Lecture... a class is like a recipe (or template). you don't eat the recipe, right? an object is an instantiation of that class that's what you eat. Or, a class is a new type.
  8. Class (or Type) Marriage of data and methods. Boundary between caller and implementer perspective on the world. self is a pointer to the object, from within the code. Each object has its own namespace, accessible via self. (In fact, isn't an object just a namespace? Could be...)
  9. Creating class instances Q: One creates a new instance of a class by calling the class ______________. A: constructor
  10. What does constructor code do? Q: When code instantiates a class, the __init__ method is called. This method's "primary purpose is to establish initial values for the ____________ of the newly created object." A: attributes
  11. Code for Car constructor Q: I have a class Car that I instantiate this way: car = Car() Write the method definition for Car’s constructor. A: def __init__(self): # code here to initialize attributes to # default values … self._color = “black”
  12. Signature Q: After creating a Car instance (i.e., object), my main code wants to set the Car's color to a new color. Write the signature of the Car member function, setColor(), to set the car's color. A: def setColor(self, color): ‘’’set the color of this car to the given color’’’ self._color = color
  13. Signature Q: Then, my main code wants to get the color from my car instance. Write the signature for the member function getColor(). A: def getColor(self): ‘’’return the color of this Car to the caller.’’’ return self._color # my color
  14. Designing a Class When you design a class, you decide: what are the important properties (or attributes, characteristics, or state) of an instance. what information a caller should be able to get from an object what state a caller should be able to change in an object what a caller should be able to ask an object to do to itself.
  15. Naming Conventions A class name always starts with a capital letter. Each word in the class starts with a capital letter. class MyFathersCar: class Cs106Lab: A class attribute always starts with an underscore: _ self._x_loc, self._y_loc, self._color
  16. Naming Conventions local variables must start with a lowercase letter. global variables must also start with a lowercase letter, unless they are CONSTANTS, which must be all uppercase. function names start with a lowercase letter. Either use camelCase for function names and variables or use _'s between words in a name. myXLocation, or, my_x_location getLocation(), or, get_location()
  17. Order of methods in a class Q: Does the order the methods are defined in a class matter? A: No. By convention the constructor method is always first, however.
  18. Attribute names Q: Why would the coder choose _x and _y instead of x and y? A: It is a convention to name your attributes with names starting with _. The reader of the code can then know what something is when it sees the _ in front of the name.
More Related