Python cultivation------Object-oriented

Source: Internet
Author: User

Object-Oriented-----classes

Class: A class is a data structure that is like a model that is used to represent a class of things (things are the combination of data and actions) that are used to produce real objects (instances).

Object: What is the object: Open your eyes, everything you see is a single object, you can understand the object as a specific thing (things are the combination of data and action)

Class-To-object relationships: objects are created by the class, God makes people, God first has a template for human beings, this template is the human class, then God according to the definition of the class to produce a person

#creation of Class"""Class Name: "Data Attribute" "def function Property""""#Note: Classes and objects use points to access their properties#class Chinese:#dang= " Egg and Egg"#def wugong (self):#print ("Drop Dragon 18 Palm")#print (chinese.__dict__) #把Chinese class all properties get encapsulated into dictionary#print (chinese.__dict__["Dang"]) #通过key执行方法#chinese.__dict__["Wugong" (1)#{' __weakref__ ': <attribute ' __weakref__ ' of ' Chinese ' objects>, ' dang ': ' egg-balls ', ' wugong ': <function Chinese. Wugong at 0x0000007dd1850268>, ' __module__ ': ' __main__ ', ' __dict__ ': <attribute ' __dict__ ' of ' Chinese ' objects , ' __doc__ ': None}##18 Palms of the Dragon#Print (dir (Chinese))##class Chinese:#"I'm a love of the motherland."#country= "China"#def __init__ (self,name,sex,age):#Self.mingzi=name#Self.sex=sex#Self.age=age#def Gongfu (self):#print ("%s with Dragon 18 Palm"%self.mingzi)#def Zhaoji (self):#print ("%s snapped a sister"%self.mingzi)###D1=chinese ("Chenglonog", "Men",) #相当于执行类chinese下的__init__#print (D1) #<__main__.chinese object at 0x0000008758e03240>#print (d1.__dict__) # d1 {' Age ': $, ' sex ': ' Men ', ' mingzi ': ' Chenglonog '}#D1.gongfu () #先d1找gongfu can't find the Chinese .#Chenglonog the Dragon 18 Palm#2. Properties of the class#View class Properties#print (chinese.country) #China#modifying the properties of a class#chinese.country= "Japan"#print (chinese.country) #Japan#Delete the properties of a class#del chinese.country#print (chinese.country) #AttributeError: Type object ' Chinese ' has no attribute ' countr#Increase#chinese.yangjing= "BLACK"#print (chinese.yangjing) #黑色#Function Properties | | | | | function additions and deletions and data types are communicated#View function Properties#print (chinese.gongfu.__dict__)#Add function Properties#def Say_word (Self,word):#print ("%s speaks%s"% (Self.mingzi,word))#Chinese.say_word=say_word #创建属性函数sya_word | | | The function must be defined at the head first no self can not be created#D1.say_word ("The Birds") #chenglonog讲鸟语#Modifying function Properties#chinese.zhaoji=zhaole Error #NameError: Name ' Zhaole ' is not defined#def Zhao (self):#print ("%s snapped a sister"%self.mingzi)#Chinese.zhaoji=zhao#print (chinese.__dict__)#Modify Instance Properties | | | | Only adding data properties can also increase the function properties, but the call does not give the incoming value self, which does not recommend adding function properties#class Chinese:#"I'm a love of the motherland."#country= "China"#def __init__ (self,name,sex,age):#Self.mingzi=name#Self.sex=sex#Self.age=age#def Gongfu (self):#print ("%s with Dragon 18 Palm"%self.mingzi)#def Zhaoji (self):#print ("%s snapped a sister"%self.mingzi)###D1=chinese ("Chenglonog", "Men",) #相当于执行类chinese下的__init__#View#print (d1.__dict__) #{' Mingzi ': ' chenglonog ', ' sex ': ' Men ', ' age ':##Increase#d1.yanjing= "Brown"#print (d1.yanjing) #褐色#Modify#d1.yanjing= "Blue"#print (d1.yanjing) #蓝色#Delete#del d1.age#print (d1.__dict__) #{' yanjing ': ' Blue ', ' mingzi ': ' chenglonog ', ' sex ': ' Men '}#Scope of the class#1. #定义一个类, only one scope to use#class Mydata:#Pass#x=10#y=12#mydata.x=18#mydata.y=22#print (x, y) #10#print (MYDATA.X,MYDATA.Y) #18#country= "China"#class Chinese:#country= "China"#Globals () #声明全局变量#country= "Japan" #如果输出变量的话为 Japan#def __init__ (self,name):    #Self.name=name    #Print (country) #-------is a normal variable.#D1=chinese ("Cheng Long") #不加globals----"China | It is not through the class to find directly to find variables outside the class#print (d1.country) # China#print (d1.__dict__) # print (d1.__dict__)classMydata:country="Zhongguo"L=[]    def __init__(self,name): self.xingming=namedefprint1 (self):Print("Laura") D1=mydata ("Lao") D1.L=[1]#{' L ': [1], ' xingming ': ' Lao '} in D1 property dictionaryD1.l.append (4)#The D1 Dictionary has l[] in L will append all MyData data Properties l[]Print(D1.__dict__)

--------------------------------------------------------------------------------------------------------------- ---------
Add:
Class A:
def b (self):
Print ("xxx")
@staticmethod #静态方法 The following function can be called by A.C ()
Def c ():
Print ("Oooo")
@classmethod #类方法 can also be called by the class
Def d (SB):
Print (SB)
Print ("Xoxoxo")
    @property              #属性
def q (self):
Print ("Ooooxxxx")
Return 1

# A.C () #oooo
# A.D () #<class ' __main__.a ' > xoxoxo

F=a ()
After #加上函数装饰器 @property
# Print (F.Q ()) #会报错 TypeError: ' Nonetype ' object is not callable
Print (F.Q)
>>>>>ooooxxxx
>>>>>1


Python cultivation------Object-oriented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.