Python Programming - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Python Programming

Description:

Testing the Class. print 'Testing Animal methods: ... print animal1 #test the __str() method! print 'Finished testing Animal methods: ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 8
Provided by: csu15
Category:

less

Transcript and Presenter's Notes

Title: Python Programming


1
Python Programming
  • Building a Class

2
Building a Base Class
class Animal define the base class "Animal
the constructor def __init__(self,type,weight)
self.__type type
self.__weight weight
3
Adding the get Methods
get method for __type variable def
get__type(self) return self.__type
get method for __weight variable def
get__weight(self) return self.__weight
4
Adding the set Methods
set method for __type variable def
set__type(self,type) self.__type
type set method for __weight variable def
set__weight(self,weight) self.__weight
weight
5
Adding a String Representation
the string representation of an Animal object
def __str__(self) return "Type"
self.__type " " "Weight" self.__weight
6
Adding a Speak Method
animal objects can say "Hello" def
speak(self) print "I can only say
'Hello'"
7
Testing the Class
print "Testing Animal methods" animal1 Animal(
"frog", "2 ounces") constructor returns new
animal object print animal1.get__type()
call Animal method get__type() print
animal1.get__weight() call Animal
method get__weight() animal1.set__type("horse")
test the set__type() method animal1.set_
_weight("400 pounds") test the set__weight()
method print animal1.get__type() print
animal1.get__weight() animal1.speak()
test the speak() method print animal1
test the __str()
method! print "Finished testing Animal methods"
Write a Comment
User Comments (0)
About PowerShow.com