Object-Oriented Programming: encapsulate the functions you wrote previously into a class
Class: When defining a class, the first letter of the class name is capitalized, and the class must be instantiated first.
Constructor: When the class is instantiated, it automatically executes the constructor.
Properties: Variables inside a class
Method: A function inside a class.
Self represents this class of objects.
Instances are objects, instance variables, and class variables
Private functions
Private variables
Can only be used in the class, out of the class can not be used.
Inherited
The parent class has all the functions and the variable quantum classes.
If you define a class that has a lot of repetitive functions, you can define these repeating classes as parent classes.
Packaging
Polymorphic Python does not support polymorphism
A method of multiple implementations.
instance method, which must be instantiated before it can be called
Property method You look like a property, actually a function, and you have to instantiate it to invoke
Class method
It is straightforward to use without instantiation, and it can be used with class variables and class methods.
#类方法, it doesn't need to be instantiated, it can be used directly. It's a static method advanced point
#它可以使用类变量和类方法.
Static methods
No need to instantiate the direct use, in fact, and the class has nothing to do with, is a common function
It's written in the class, and it doesn't use self, and it calls out the other functions inside the class.
Modifying the constructor of a parent class
class Car (): #模型, Template
def __del__ (self):
#析构函数, this instance is destroyed by the execution.
print (' over.. ')
def my_self (self):
Print (
' I am a car my color is '%s ' and I have '%s ' Windows '% (Self.color,self.window)
)
Self.price = 10002
def run (self):
print (Self.color)
print (Self.window)
print (Self.price)
print (' The car is running ... ')
def __init__ (Self,color,window):
#
#构造函数, the class executes it at initialization time.
#如果你的类在实例化的时候要传入一些参数, then you're going to write the arguments in the __init__ function.
Self.color = Color #绑定属性
Self.window = window
print (' Execute me now. ')
#把模型做成实际的一个汽车, this process is called instantiation.
bus = Car (' Yellow ', ' 3 Open Door ') #实例化
bus2 = Car (' Black ', ' 4 Open ') #实例化
Bus3 = Car (' Pink ', ' 2 open ') #实例化
bus.my_self () #
bus2.my_self ()
bus3.my_self ()
#实例就是指具体造出来的东西, what is handled by class instantiation is an instance
#对象, it's an example.
Manipulating the database (encapsulating a MySQL Class)
Import Pymysql
class OPMYSQL1: #经典类
Pass
class Opmysql (object): #新式类
def __init__ (self,host,user,password,db,port=3306,charset= ' UTF8 '):
schema = {
' user ': User,
' host ': Host,
' password ':p assword,
' db ':d B,
' Port ':p ort,
' charset ': CharSet
}
Try:
Self.coon = Pymysql.connect (**schema)
except Exception as E:
print (' Database connection exception! %s '%e)
quit (' Database connection exception! %s '%e)
Else: #没有出异常的情况下, creating Cursors
self.cur = self.coon.cursor (cursor=pymysql.cursors.dictcursor)
def execute (self,sql):
Try:
self.cur.execute (SQL)
except Exception as E:
print (' SQL has error%s '%e)
return e
if sql[:6].upper () = = ' SELECT ':
return Self.cur.fetchall ()
Else: #其他sql语句的话
Self.coon.commit ()
return ' OK '
def __del__ (self):
self.cur.close ()
self.coon.close ()
YBQ = opmysql (' 211.149.218.16 ', ' jxz ', ' 123456 ', db= ' jxz ') #实例化
Print (Ybq.execute (' select * from Stu; '))
Print (Ybq.execute (' select * from Stu; '))
Print (Ybq.execute (' select * from Stu; '))
Self summary
class My ():
def __init__ (self,name):
self.name = name
self.cry ()
def Cry (self): #实例方法, it must be instantiated before it can be called
print ('%s ' is crying ... '%self.name)
def Learn (self):
Self.skill = [' Drive ']
def my_self (self):
print (' My name '%s ' I will%s '% (Self.name,self.skill))
WSL = My (' Wang Silei ') # self = WSL
Wsl.skill = ' wave '
Wsl.learn ()
wsl.skill.append (' wave ')
wsl.skill.append (' Sao ')
wsl.skill.append (' cheap ')
wsl.my_self ()
Wsl.learn ()
Wsl.my_self ()
Instance variable
class Person (object):
country = ' China ' #类变量
def __init__ (self,name,age,sex):
self.name = name #实例变量, must be instantiated before it can be used, member variables
self.age = Age
self.sex = Sex
def say_my_country (self):
print (self.country)
#print (person.country)
# DSX = person (' Big bro ', 23, ' Male ')
# Print (dsx.name)
# Print (dsx.age)
# Print (dsx.sex)
# Print (dsx.country)
# YBQ = Person (' original Po Qing ', 28, ' Male ')
Day8-python Learning Notes (18) object-oriented, self, private, attribute methods