Created
December 13, 2011 14:35
-
-
Save dketov/1472323 to your computer and use it in GitHub Desktop.
Определение классов
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Простейший класс | |
""" | |
class MyClass: | |
"A simple example class" | |
i = 12345 | |
def f(self): | |
return 'hello world' | |
#__doc__ is also a valid attribute, returning the docstring belonging to the class: | |
#"A simple example class" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Класс с конструктором | |
""" | |
class Worker: | |
def __init__(self, name, pay): | |
self.name = name | |
self.pay = pay | |
def lastName(self): | |
return self.name.split( )[-1] | |
def giveRaise(self, percent): | |
self.pay *= (1.0 + percent ) | |
bob = Worker('A', 50000) | |
sue = Worker('B', 60000) | |
print bob.lastName( ) | |
print sue.lastName( ) | |
sue.giveRaise(.10) | |
print sue.pay |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Вывод класса | |
""" | |
class Point: | |
def __init__( self, xValue = 0, yValue = 0 ): | |
self.x = xValue | |
self.y = yValue | |
def __str__( self ): | |
return "( %d, %d )" % ( self.x, self.y ) | |
point = Point( 72, 115 ) | |
print "X coordinate is:", point.x | |
print "Y coordinate is:", point.y | |
point.x = 10 | |
point.y = 10 | |
print "The new location of point is:", point |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Экземпляры | |
""" | |
#Just pretend that the class object is a | |
#parameterless function that returns a new instance of the class. | |
class MyClass: | |
"A simple example class" | |
i = 12345 | |
def f(self): | |
return 'hello world' | |
x = MyClass() | |
# creates a new instance of the class and assigns this object to the local variable x. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment