1 / 40

Introduction to Classes in Computer Science

Learn about object-oriented programming, class definitions, encapsulation, modularity, inheritance, and polymorphism. Understand the difference between a class and an instance, and how to use constructors, accessors, and mutators methods. Explore the relationship between methods and functions, and the importance of the 'self' parameter.

hshirly
Download Presentation

Introduction to Classes in Computer Science

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. Chapter 11Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

  2. Lab 12 • Let’s review a solution

  3. What is a Class? • The short answer is that object oriented programming is a way to think about “objects” in a program (such as variables, functions, etc). • A program becomes less a list of instructions and more a set of objects and how they interact.

  4. OOP Principles • Encapsulation: hiding design details to make the program clearer and more easily modified later. • Modularity: the ability to make objects “stand alone” so they can be reused (our modules). I.e. the math module. • Inheritance: create a new object by inheriting (like father to son) many object characteristics while creating or over-riding for this object. • Polymorphism: (hard) Allow one message to be sent to any object and have it respond appropriately based on the type of object it is.

  5. Class versus Instance • A class is used as a definition to create new instances of those classes (which are objects) • Describes what the object is going to look like

  6. Why a Class? • We make classes because we need more complicated, user-defined data types to construct instances we can use. • Each class has potentially two aspects: • the data (types, number, names) that each instance might contain • the messages that each instance can respond to

  7. A First Class

  8. class Student(object): """Simple Student class.""" def __init__(self,first='', last='', id=0): # init instance self.firstNameStr = first self.lastNameStr = last self.idInt = id def __str__(self): # string representation for printing return "{} {}, ID:{}".format\ (self.firstNameStr, self.lastNameStr, self.idInt)

  9. Constructor • When a class is defined, a function is made with the same name as the class • This function is called the constructor. By calling it, you can create an instance of the class. • We can affect this creation (more later), but by default Python can make an instance.

  10. A Class is a New Type >>>myLst = [1, 2, 3] type(myLst) => type<‘list’> >>>myStr = ‘abc’ type(myStr) => type<‘str’> >>>s1 = Student(“Sarah",“Diesburg",12345) type(s1) <class '__main__.Student'>

  11. Instance Knows its Class • Because each instance has as its type the class that it was made from, an instance “remembers” its class. • This is often called the “instance-of” relationship.

  12. “Dot” Reference • We can refer to the attributes of an object by doing a “dot” reference, of the form: object.attribute • The attribute can be a variable or a function. • It is part of the object, either directly or by that object being part of a class.

  13. Accessor MethodsA method that returns information about the STATE of the object. (The value of one or more instance variables).

  14. Two ways to access data • print myInst.myVal • print a variable associated with the object myInst • myInst.myMethod() • call a method associated with the object myInst • Variable versus method, you can tell by the parenthesis at the end of the reference

  15. class Student(object): """Simple Student class.""" def __init__(self,first='', last='', id=0): # init instance self.firstNameStr = first self.lastNameStr = last self.idInt = id def __str__(self): # string rep, e.g. for printing return "%s %s, ID:%s" % \ (self.firstNameStr, self.lastNameStr, self.idInt) def getID(self): return self.id

  16. Why I prefer accessor methods over direct access • print s1.idInt • print s1.getID() • Both return the same exact value right now, but I think that the accessor method is a better way to do this. • Any idea why?

  17. Mutator MethodsMethods that have the potential to change the state of an object.

  18. class Student(object): def __init__(self,first='', last='', id=0): self.firstNameStr = first self.lastNameStr = last self.idInt = id def update(self,first='',last='',id=0): if first: self.firsNameStr = first if last: self.lastNameStr = last if id: self.idInt = id def __str__(self): # string rep, e.g. for printing return "%s %s, ID:%s" % \ (self.firstNameStr, self.lastNameStr, self.idInt)

  19. Method versus Function • Discussed before, a method and a function are closely related. They are both “small programs” that have parameters, perform some operation and (potentially) return a value. • The main difference is that methods are functions tied to a particular object.

  20. Difference in Calling Functions are called, methods are called in the context of an object: • function: dosomething(param1) • method: anObject.doSomething(param1) This means that the object that the method is called on is always implicitly a parameter!

  21. More on Self • selfis an important variable. In any method it is bound to the object that called the method. • Through self, we can access the internal structure of the instance.

  22. self is Bound for Us • When a dot method call is made, the object that called the method is automatically assigned to self. • We can use self to remember, and therefore refer, to the calling object. • To reference any part of the calling object, we must always precede it with self. • The method can be written generically, dealing with all calling objects through self.

  23. Writing a class

  24. Constructor • There are some special methods that have certain pre-defined roles for all classes. • One of the first we will learn is a constructor. • Constructor is called when an instance is made and provides the class designer the opportunity to set up the instance with variables, by assignment.

  25. Calling a Constructor • As mentioned, a constructor is called by using the name of the class as a function call (by adding () after the class name): myInst = myClass() • Creates a new instance using myClass.

  26. Special Python Keywords • Again, Python has special uses for keywords that begin and end with __. • so far we have seen the __doc__ attributed of a function with a doc string. • In classes, we will see more of these special values.

  27. The __init__ Method • One of the special method names in a class is the constructor name, __init__. • By assigning values in the constructor, every instance will start out with the same variables. • You can also pass arguments to a constructor through its init method.

  28. def __init__(self,first='', last='', id=0): self.firstNameStr = first self.lastNameStr = last self.idInt = id • self is bound to the default instance as it is being made. • If we want to add an attribute to that instance, we modify the attribute associated with self.

  29. Default Constructor • If you don’t provide a constructor then only the default constructor is provided. • The default constructor does “system stuff” to create the instance, nothing more. • You cannot pass arguments to the default constructor.

  30. Every Class Should Have __init__ • By providing the constructor, we ensure that every instance, at least at the point of construction, is created with the same contents. • This gives us some control over each instance.

  31. Printing def __str__(self): return "%s %s, ID:%s" % \ (self.firstNameStr, self.lastNameStr, self.idInt) • What happens when we call print myInst? • This is assumed, by Python, to be a call to “convert the instance to a string”, which is the __str__ method. • In the method, myInst is bound to self, and printing then occurs using that instance.

  32. Now There are Three • There are now three groups in our coding scheme: • user • programmer, class user • programmer, class designer

  33. Class Designer • The class designer is creating code to be used by other programmers. • In so doing, the class designer is making a kind of library that other programmers can take advantage of.

  34. Remember this:OOP Principles • Encapsulation: hiding design details to make the program clearer and more easily modified later. • Modularity: the ability to make objects “stand alone” so they can be reused (our modules). I.e. the math module. • Inheritance: create a new object by inheriting (like father to son) many object characteristics while creating or over-riding for this object. • Polymorphism: (hard) Allow one message to be sent to any object and have it respond appropriately based on the type of object it is.

  35. We are Still at Encapsulation • Features of encapsulation: - hid details of the implementation so that the program was easier to read and write - modularity: make an object so that it can be reused in other contexts - providing an interface (the methods) that are the approved way to deal with the class

  36. Private Values

  37. Private Variables in an Instance • Many OOP approaches allow you to make a variable or function in an instance private. • Private means not accessible by the class user, only the class developer. • There are advantages to controlling who can access the instance values.

  38. ‘Privacy’ in Python • Python takes the approach “We are all adults here.” No hard restrictions. • Provides naming to avoid accidents. Use __ in front of any variable • This ‘mangles’ the name to include the class, namely __var becomes _class__var. • Still fully accessible, and the __dict__ makes it obvious.

  39. Privacy Example class myClass(object): def __init__(self,p1='firstParam',p2='secondParam'): self.var1=p1 self.__var2=p2 myInst = myClass() myInst.__dict__.items() [('_myClass__var2', 'secondParam'), ('var1', 'firstParam')]

More Related