1 / 25

Introduction to Classes

Learn about object-oriented programming and how to define and use classes in Python. Understand the principles of encapsulation, modularity, inheritance, and polymorphism.

anaw
Download Presentation

Introduction to Classes

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

  2. PA09 • New homework - word clouds!

  3. Programming Styles • Up until today, we have been programming in a procedural programming style • Series of computational steps to be carried out • We do this by writing functions that operate on data structures • This style of programming is suitable for smaller programming tasks

  4. Programming Styles • Classes are part of Object Oriented Programming (OOP) • OOP 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. • Models the “real world” where objects (entities) work together to accomplish tasks

  5. Responding to “Messages” • As a set of interacting objects, each object responds to “messages” sent to it. • The interaction of objects via messages makes a high level description of what the program is doing. Start!

  6. Everything in Python is an Object • In case you hadn’t noticed, everything in Python is an object. • Thus Python embraces OOP at a fundamental level. • This is in contrast to other programming languages • C is completely procedural • Java is OOP but has some partial non-objects

  7. OOP Helps in Software Engineering • Software engineering is the discipline of managing code to ensure its long-term use. • We’ve already seen some code reuse • Counters, accumulators • Can write general functions • We will find the same thing with objects • Some basic objects are even easier to reuse • Strings, list, dictionaries, …

  8. More Refactoring • Hiding the details of what the message entails means that changes can be made to the object and the flow of messages (and their results) can stay the same. • Thus the implementation of the message can change but its intended effect stays the same. • This is encapsulation.

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

  10. OOP Principles • Inheritance: create a new object by inheriting (like parent to child) 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. • Where have we seen this already??

  11. Class versus Instance • One of the harder things to understand is what a class is and what an instance of a class is. • Consider the analogy of the cookie cutter and a cookie.

  12. Class versus Instance • The cutter is a template for “stamping out” cookies, the “cookie” is what is made each time the cutter is used. • One template can be used to make an infinite number of cookies, each one just like the other. • No one confuses a cookie for a cookie cutter, do they?

  13. Same in OOP • You define a class as a way to generate new instances of that class. • Both the instances and the classes are themselves objects. • The structure of an instance starts out the same, as dictated by the class. • The instances respond to the messages defined as part of the class.

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

  15. A First Class • Say we need to make a new university information system • What is this?

  16. A First Class • In a university information system, we need information about • Students • Faculty • Courses • Transcripts • Python does not have these • We need to create our own definitions of what a “student” is!

  17. A First Class • What should a bundle of information called a “student” should know about in our code? • Name • Student ID • What else…?

  18. A First Class • What should a bundle of information called a “student” should know about in our code? • Name • Student ID • What else…? • Could put this information in a tuple, but what’s missing?

  19. A First Class • What should a bundle of information called a “student” should know about in our code? • Name • Student ID • What else…? • Could put this information in a tuple, but what’s missing? • It would be nice to pass message to a student object, and have that student object operate on its own data

  20. A sample class 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)

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

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

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

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

More Related