The face of the object

Defined functions:

def 函数名(参数列表):

    函数体

 May be used in Python classkeyword define classes, and then studied by the previously defined class method function, so that the object can describe the dynamic characteristics, the code shown below.

# __Init__ is a special method for initialization when you create an object

# This way we can bind the two properties name and age for students Objects

 DEF the __init __ ( Self, name, Age): the self.name = name self.age = Age DEF Study ( Self, COURSE_NAME): Print ( ' % S is learning % S. ' % ( the self.name, COURSE_NAME)) # the PEP 8 connection identifier name is underlined plurality of words in all lowercase # but some companies prefer to use the programmer and nomenclature hump (hump identification) DEF watch_movie ( Self): IF self.age < 18 is: Print ( ' % s can only watch "of bears." ' % self.name) the else: Print ( ' .% s are watching the big island of love movie ' % Self.name)

python accessed only two types one is a private is public 

PS: If you want the property is private property in the name to be used as two underscores beginning 

 However, Python does not guarantee strict privacy private property or method from the grammar, it just gives private properties and methods of a change of name to hinder access to them, in fact, change the name if you know the rules still have access to but they are in the actual development does not recommend the property to private because it makes the subclass can not access, so most of the named attribute names will begin with a single underscore to represent the property is protected

A simple watch:

from time import sleep
class Clock(object):

def __init__(self,hour=0,minute=0,second=0):
'''初始化方法
:param hour:时
:param minute:分
:param second:秒
'''
self._hour = hour
self._minute = minute
self._second = second
def run(self):
self._second += 1
if self._second == 60:
self._secound = 0
self._minute += 1
if self._minute == 60:
self._minute = 0
self._hour += 1
if self._hour == 24:
self._hour = 0
def show(self):
return '%02d:%02d:%02d' % \
(self._hour,self._minute,self._second)

def main():
clock = Clock(23,59,58)
while True:
print(clock.show())
sleep(1)
clock.run()
if __name__ == '__main__':
main()

PS: the above code directly copied to knock them out, and want to see if their attention typesetting wailing 

Describes a class defined point on the plane and provide motion and point calculation method to another point distance.

Math sqrt Import from 


class Point (Object):

DEF the __init __ (Self, X = 0, Y = 0):
"" "as initialization
: param x: abscissa
: param y: vertical coordinate
" ""
self.x = X
Y = self.y

DEF move_to (Self, X, Y):
"" "move to the specified target
: param x: new abscissa
: param y: new ordinate
" ""
self.x = X
self.y = Y

DEF move_by (Self, DX, Dy):
"" "increasing the amount of movement specified
: param dx: abscissa
: param dy: ordinate
" ""
self.x + = DX
Self.y += dy

def distance_to(self, other):
"" "Calculating the distance to another point
: param other: another point
" ""
DX-other.x self.x =
Dy = self.y - other.y
return sqrt (DX Dy + 2 ** 2 ** )

DEF __str __ (Self):
return '(% S,% S)'% (STR (self.x), STR (self.y)) the PS: Lv Python is very strict. I have this line of code because missed one underscore gave me the wrong result, is a strict, keep that in mind, and Gan


DEF main ():
p1 = Point (3, 5)
P2 = Point ()
Print (p1 )
Print (P2)
p2.move_by (-1, 2)
Print (P2)
Print (p1.distance_to (P2))


IF the __name__ == '__main__':
main ()

Guess you like

Origin www.cnblogs.com/linQingxuan/p/11893978.html