python-Object-oriented

Source: Internet
Author: User

1. Process oriented

-----What is called process-oriented

Process: The steps and processes for dealing with things. Core: Process

Advantages: Reduced complexity of the program disadvantages: Poor scalability

2. Object-oriented

------Object-oriented: Everything is Object core: Object

Advantages: Solve the problem of poor program extensibility

Note: In Python a variable represents a feature, a function represents a skill, and a class is a combination of a variable and a function, and an object is a combination of a variable and a method (a function that points to a class).

3. Class

Class concept: Classification, common characteristics or not a class or not, the class is to extract the same characteristics

In real life: first there are objects and classes, in the program is the first class, then there are objects

Define class: Define variable and function first letter capitalization

By an abstract thing parentheses followed by a colon produces a class, the return value is called instantiating an object, the instantiation triggers __init__ execution, the __init__ method cannot have a return value, and the instantiation tries not to invoke the Class property

classPerson : #定义一个人类 role='Chinese'# Class Property static property Def __init__ (SELF,NAME,LIFE_VALUE,AGGR): #__init__方法 dynamic Property Self.name=name #姓名 dynamic Property Self.life_value=life_value #生命值 Dynamic Properties Self.aggr=aggr #攻击力 Dynamic Properties def attack (Self,enemy): #定义了一个攻击的类属性方法 enemy.life_value=enemy.life_value-self.aggr #对象的生命值 Print ('Attack') Egg=person ('Egon', +, -#实例化这个对象 equals the execution of person.__init__ () print (egg.name) #查看属性/Call method print (Egg.life_value) print (EGG.AGGR) 

The process of instantiating is the process of class------object
The self-----automatically passes the instantiated object/instance itself to the first parameter of __init__

Interaction between programs

classPerson:role='Chinese'# Class Property static property Def __init__ (SELF,NAME,LIFE_VALUE,AGGR): #__init__方法 dynamic Property
#动态属性 Self.name=name #姓名
Self.life_value=life_value #生命值 Self.aggr=AGGR #攻击力 def attack (Self,alex): #定义一个攻击的动态属性 alex.life_value-=self.aggr #alex的生命值 #egon. Life_value-=Self.aggr #egon的生命值
# instantiating objects viewing Properties/calling methods Egon=person ('Geon', +, the) Alex=person ('Alex', -, -) print (Egon.life_value) #egon未战斗之前的生命值alex. Attack (Egon) #alex攻击egonprint (Egon.life_value) #egon战 Life after bucket print (Alex.life_value) #alex未战斗之前的生命值egon. Attack (Alex) #egon攻击alexprint (Alex.life_value) #alex战之后的生命值 classDog: #定义狗类 def __init__ (SELF,NAME,LIFE_VALUE,AGGR): #__init__方法 Dynamic Properties
#动态属性 Self.name=name #名字 self.life_value=life_value #生命值 Self.aggr=aggr #攻击力 def Bite (Self,person): #定义狗咬的技能 egon.life_value-=Self.aggr #egon的被攻击后的生命值旺财=dog ('Xiao Qiang', -, $) #实例化dog对象 # #egon与旺财大战print (egon.life_value) #狗未攻击egon之前egon的生命值旺财. Bite (Egon) #狗攻击egonprint (Egon.life _value) #egon被攻击之后的生命值print (Wang Choi Life_value) #egon未打狗之前狗的生命值egon. Attack (Wong Choy) # #egon打狗print (Mong Choi. Life_value) #狗被打之后的生命值 #alex and Wong Choy War print (alex.life_value) #alex没被狗攻击之前的生命值旺财. Bite (Alex) #狗攻击alexprint (Alex.life_value) #alex被咬之后的生命值print (Mong Choi Life_value) #狗没挨打之前的生命值alex. Attack (Wong Choy) #alex打狗print (Mong Choi Life_value) #狗受伤之后的生命值


4. Class namespace, instance namespace

Creating a class creates a namespace that is used to store all the names defined in the class, which are called Class name properties

The class has two properties: static property and dynamic property

    • A static property is a variable that is defined directly in the class
    • A dynamic property is a method defined in a class
# #面向对象名称空间问题class Person:
Role= ' Chinese '
passegon.role=' Indians 'print (Egon.role,id (egon.role)) Print (Alex.role,id ( Alex.role)) Print (Person.role,id (person.role)) person.role=' Indians 'print ( Egon.role,id (Egon.role)) print (Alex.role,id (alex.role)) print (Person.role,id (person.role)) # #总结: In object-oriented, if you want to invoke a property, You will not find it in the class, if you have it, call itself

5. Object-oriented combination and usage

Combination usage: In one class, an object of another class as a data property, this class is called a class combination

Example

classGrandpa: Def __init__ (self, name, age, work): Self. Name=name self. Age=age self. Work=workclassdad: Def __init__ (self, name, age, work): Self. Name=name self. Age=age self. Work=workclassson: Def __init__ (self, name, age, work, Grandpa _ name, Grandpa _ Age, Grandpa _ Work): Self. Name=name self. Age=age self. Work=work self. Grandpa=Grandpa (Grandpa _ Name, Grandpa _ Age, Grandpa _ Work) Xiao Qiang= Son ('Xiao Qiang', the,'Student','pa DAO', the,'Farmers') Print (Xiao Qiang's grandfather. Name, Xiao Qiang. Grandfather, age, Xiao Qiang. Work) Xiao Qiang= Daddy ('Big Strong', -,'Farmers') print (Xiao Qiang. Name, Xiao Qiang. Age, Xiao Qiang. Work)

Ring instance

 fromMath Import Piclasscircle:def __init__ (self,r): #动态参数 SELF.R=R #名字 Object Properties def area (self): #定义面积方法 Dynamic PropertiesreturnPI * self.r**2#计算面积并返回 def perimeter (self): #定义周长方法 Dynamic PropertiesreturnPI * self.r*2#计算周长并返回a=circle (Ten) #实例化一个圆对象print (A.area ()) print (A.perimeter ())classRing: # def __init__ (Self,r1,r2,p=3.14): Self.r1_circle=Circle (R1) self.r2_circle=Circle (R2) SELF.P=P def area (self):returnSelf.r1_circle.area ()-Self.r2_circle.area () def perimeter (self):returnSelf.r1_circle.perimeter () +Self.r2_circle.perimeter () a=ring (Ten,5) Print (A.area ()) print (A.perimeter ())

6. Object-Oriented inheritance
What do you mean, inheritance

Inheritance is a way of creating new classes in Python, where a new class can inherit one or more parent classes, which can be called a base class or a superclass, and a new class is called a derived class or subclass.

python-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.