#!/usr/bin/env python# encoding:utf-8 "" "@version: Python3.6@file:object_oriented.py@time:2017/11/3 21:18" "" " Define the class and execute the methods in the class: Class Name: Def method Name (Self,arg): print (ARG) Intermediate object = Class name () #类的实例化中间人. Method Name (1) #此处的 1 refers to the above arg ' # ' class Bar:d EF paper (self,a): #a = ' had been accepted ' # print (a) return Aobject=bar () ret = object.paper (' paper ') print (re T) ' Class bar:def foo (self,arg): print (self,self.name,self.age,self.sex,arg) z = Bar () print (z) z.name = ' Alex ' Z.age = ' z.sex ' = ' Male ' Z.foo (999) # <__main__.bar object at 0x000001c89e2dbd68># <__main__.bar object at 0x000001c89 E2dbd68> Alex Male 999 shows that self represents an instance of the class "" #self代指调用方法的对象, and is also an instantiation of the class (middleman) class Person:def __init__ (self,name,age): # The constructor method is called by Python itself to initialize the Operation function 1. Create a Class object, 2. The constructor method is automatically executed by a special method in the object execution class #构造方法的特性是 class name () Self.name = name Self.age = Age def Show (self): #需要人为调用 Print ('%s-%s '% (self.name,self.age)) Lihuan = person (' Liuhuan ', ") Zhaojin = person (' Zhaojin ', ') lihuan.show () ' ' # ' #继承classF:def F1 (self): print (' f.f1 ') def f2 (self): print (' F.f2 ') class S (F): def-S1 (self): print (' s.s1 ') def f2 (self): print (' s.f2 ') #由于子类中由f2这一方法 so the f2 of the parent class is not inherited; there are two ways to output the F2 of the parent class: #super (S, self). F2 () #方法一: Execute the F2 method in the parent class #F. F2 (self) #方法二: Executes the F2 method in the parent class obj = S () obj.s1 () #s1中的self是形参, this time refers to the OBJOBJ.F1 () #self永远指调用方法的调用者obj. F2 ()
' Class province:country = ' China ' #静态字段, belongs to Class Def __init__ (Self,name): #普通字段, belongs to object self.name =namehenan = Province (' Henan ') Hebei = Province (' Hebei ') print (province.country) print (Hebei.name) "#" Class Foo:def bar (self):p rint (' Bar ') @staticmethod # static method keyword def sta ():p rint (' sta ') @staticmethoddef sta1 (A1,A2):p rint (A1,A2) @classmethod # class Method Def rew (CLS):p rint (CLS Print (' SDFSADF ') @property #属性关键字 defined in the normal way, but executes the def per (self) in class,:p rint (213) Foo.sta () foo.sta1 obj = foo () Obj.bar () foo.rew () obj = foo () obj.per "" "Class Pergination:def __init__ (self,current_page): Try:p =int ( Current_page) except Exception as e:p =1self.page = p@propertydef start (self): val = (self.page-1) *10return Val@propertydef End: Val=self.page*10return valli=[]for i in range (+): Li.append (i) while true:p = input (' Please enter the page number to view: ') #每页显示10条obj =pergination (p) print (Li[obj.start:obj.end]) ""
1. How to create a class
Class Name:
Pass
2. How to create
Construction method, __init__ (Self,arg)
obj = Class (' A1 ')
Common methods
obj = Class (' xxx ')
Obj. Common method Name ()
3. One of the three main features of object-oriented: encapsulation
Class Bar:
def __init__ (self, n,a):
Self.name = n
Self.age = A
Self.xue = ' O '
B1 = Bar (' Alex ', 123)
b2 = Bar (' Eric ', 456)
4. Application Scenario:
Convert to object-oriented if there are some of the same parameters in multiple functions
Class Databasehelper:
def __init__ (self, IP, port, username, pwd):
Self.ip = IP
Self.port = Port
Self.username = Username
Self.pwd = pwd
def add (self,content):
# using linked data, such as user name, password, etc. encapsulated in self
Print (' content ')
# Close Data Link
def delete (self,content):
# using linked data, such as user name, password, etc. encapsulated in self
Print (' content ')
# Close Data Link
def update (self,content):
# using linked data, such as user name, password, etc. encapsulated in self
Print (' content ')
# Close Data Link
def get (self,content):
# using linked data, such as user name, password, etc. encapsulated in self
Print (' content ')
# Close Data Link
S1 = databasehelper (' 1.1.1.1 ', 3306, ' Alex ', ' SB ')
5, object-oriented three major characteristics of the second: inheritance
1. Inheritance
Class Parent class:
Pass
Class Subclass (Parent Class):
Pass
2. Rewrite
Prevent methods in the parent class from being executed
3. Self is always the caller of the method that executes the change
4.
Super (subclass, Self). Methods in the parent class (...)
Parent class name. Method in parent class (self,...)
5. Support multiple inheritance in Python
A. Left priority
B. A road goes to the black
C. At the same root, the root is finally executed
6, object-oriented three major characteristics of three: polymorphism
====> Native polymorphism
# Java
String v = ' Alex '
def func (string arg):
Print (ARG)
Func (' Alex ')
Func (123)
# Python
v = ' Alex '
def func (ARG):
Print (ARG)
Func (1)
Func (' Alex ')
==================================================================
Practice:
Class Person:
def __init__ (self,n,a,g,f):
Self.name = n
Self.age =a
Self.gender =g
Self.fight = f
Role_list = []
Y_n = input (' Do you want to create a role? ‘)
if y_n = = ' Y ':
Name = input (' Please enter name: ')
Age = input (' Please enter name: ')
...
Role_list.append (Person (...))
# role_list,1,2
======================================================= advanced ================================================= in Object-oriented =======
Class Foo:
def __init__ (self, name):
# normal Fields
Self.name = Name
# Common method
Def show (self):
Print (Self.name)
obj = Foo (' Alex ')
Obj.name
Obj.show ()
Class Members:
# field
-Normal field, save in object, execute only through object access
-static fields, which are saved in the class, can be accessed through the object or accessed through the class
# method
-Normal method, saved in class, called by object, self= "Object
-static method, saved in class, called directly by class
-Class method, saved in class, called directly by class, cls= "Current Class"
######## Application Scenario:
If you need to save some values in an object and perform a function, you need to use the values in the object-> the normal method
No value in any object is required, static method
# Properties, Attributes
-Nondescript
All provinces of China, expressed in object-oriented knowledge?
Class Province:
# static fields, belonging to class
Country = ' China '
def __init__ (self, name):
# Normal field, belongs to object
Self.name = Name
Henan = Province (' Henan ')
Henan.name
Henan.name = "Henan South"
#hebei = Province (' Hebei ')
# Province.country
Python module-Class