Learning python fifth day notes

30,036
target = attribute + method
# property is the object itself comes with features such as: height, weight, age and so on;
# target is to do things, such as: running, playing and so on;
create an object, such as: class name of the object:
object-oriented features: encapsulation, inheritance, polymorphism.
Inheritance is # subclass inherits the parent class, for example: class Mylist (List):
Pass # skip mean
List1 = Mylist () # List1
list1.append (0)
list1.append (. 9)
List1
[0,9]
# plurality Polymorphism is the object can have a variety of forms, for example: class A:
DEF Fun (Self):
Print ( "okay you!")
class B:
DEF Fun (Self):
Print ( "Hello")
A = A ()
B = b ()
a.fun () # A later turned into a, as can call this function
you okay!

31,037
Self equivalent function pointer, for example: class Ball:
DEF the setName (Self, name):
the self.name = name
DEF Kick (Self):
Print ( "My name S%" the self.name%)
A = Ball ()
a.setName ( 'white')
B = Ball ()
b.setName ( 'packet')
a.kick a value of () # self recorded on the pointer, the name of the corresponding look self.name
my name is white
b.kick () # value b is recorded on a self pointer, the corresponding name from the self.name go
I called packet
_init_ usage, for example: class Ball:
DEF _init_ (self, name):
name = self.name
DEF Kick (Self):
Print ( "my name is% s"% self.name)
total in private division, only need to define the name Cadogan two front line fell on the line, such as the Person class:
_ _name = "Xiaopeng"
the p-the Person = ()
p._ # _name such access is not accessible.
p._Person_ _name # In fact, it is double underlined that the variable name becomes a class name _ _ _ variable name

32,038
Inheritance: subclasses inherit all the attributes of the parent class, for example:
Import Random R & lt AS

class Fish: Fish are the property #
DEF __init __ (Self):
self.x = r.randint (0,10)
self.y = r.randint (0,10)

def move (self): # let the fish are moving to the left, the other side of the border appear when the value reaches the boundary.
self.x IF <1:
self.x = 10
self.x - = 1
Print ( "My current position:", self.x, self.y)
the else:
self.x - = 1
Print ( "I now location: ", self.x, self.y)

class Goldfish (Fish): # Goldfish Fish inherited properties of
pass

class Carp (Fish): # inherited property Carp Fish's
pass

class Salmon(Fish):
pass

Shark class (Fish):
DEF __init __ (Self):
Super () .__ the init __ () # Super function can help you automatically before the contents of the parent class early.
self.hungry = True

EAT DEF (Self):
IF self.hungry:
Print ( "I will kill you")
self.hungry = False
the else:
Print ( "I'm full!")

33,039
class, the class object and instance objects, for example:
class C: When the definition C # class is, at the end of the definition of an object class is C
DEF setXY (Self, X, Y):
self.x = X
self.y Y =
DEF printXY (self):
Print (self.x, self.y)
d = C () # where d is the object instance corresponds to the function value becomes self d to be input to store, into e.g. dx = x and the dy = y

33,040
#issubclass (subclass, superclass) is used to check whether the parent-child class relationship, if returns True, otherwise return False
#isinstance (instance of an object, the parent class) is used to check whether an object is instantiated from a parent class, if returns True, otherwise return False
#hasattr (objects 'attributes') has the property to check whether the object.
#setattr (objects 'attributes', 'value of the property') if the object does not find access to the properties of matter, will create the property and give the property value

#property (method of obtaining property, modify the properties of the method, remove the attribute method) used to modify the properties of an object.

Guess you like

Origin www.cnblogs.com/dcpb/p/11570336.html