Oo=object oriented object-oriented
Python is an object-oriented programming language
Features of OO:
1, Package: Throw a bunch of things together, into a class
2, Inheritance: If a class inside
3, polymorphic: Different classes have the same name of the function, if you call separately, you can ... It looks like there's no egg.
1, the self of Python
You just use it like this, in the Def parameter of class:
Class B:
def setname (self,name):
Self.name=name
def kick (self):
Print (' My name is%s, damn it, who kicked me .... '%self.name ')
You just use it, and when you write Def, the first argument is self.
Call it this way: b=b (' potatoes ')
B=kick () #会有输出
2. Python's __init__ (self)
Class B:
def __init__ (self,name):
Self.name=name
def kick (self):
Print (' My name is%s, damn it, who kicked me .... '%self.name ')
Call it this way: b=b (' potatoes ')
B=kick () #会有输出
__init__ replaced the SetName function, which will be discussed in detail __init__
3. Python's private variables
Class Person:
__name= ' Zai zhe li '
You are now output: P=person ()
Print (P.__name) #是会报错的
Want to output like this: P=person ()
Print (P._person__name) #_类名__变量名称
Python is object-oriented programming, so he has a class, there are objects, not like a single dog, even the object is not
Define a class with class, and the first letter of the class name must be capitalized:
Class CC:
def setxy (self,x,y):
Self.x=x
Self.y=y
def printxy (self):
Print (SELF.X,SELF.Y)
Then: DD=CC () defines an object of the CC class, also the class is a template, DD is the finished product, a template can produce a lot of finished products.
Use del cc to delete this class, after the egg is deleted, the object can still be used
can use
Dd.setxy (4,5), the X and y assignments in the DD object,
dd.__dict__ Viewing function assignment conditions
Getting started with Python--20--classes, objects