First, object-oriented
Python itself is an object-oriented language, the core of object-oriented program design is the object, first of all to understand what is the object.
Object-oriented Advantages: It solves the extensibility of the program.
Object-oriented disadvantage: poor controllability.
1. Introduction to Object-oriented nouns
Class: Used to describe a collection of objects that have the same properties and methods, and he defines the properties and methods that are common to each object in the collection, which is an instance of the class;
Class variables: Class variables are common throughout the instantiated object.
Instantiation: Creates an instance of a class, the concrete object of the class.
Method: A function defined in a class
Object: an instance of a data structure defined by a class; An object consists of two data members (class variables and instance variables) and methods
2, the syntax of the class:
class class Name: = aprint(class name. Attribute)# The role of the class name is to manipulate the properties to view the properties
After a class is instantiated, its properties can be used, and after a class is actually created, its properties can be accessed through the class name.
3. Class objects
The class object supports two operations: Property Reference and instantiation.
The property reference uses the same standard syntax as all property references in Python:obj.name.
After a class object is created, all the names in the class namespace are valid property names. So if the class definition is this:
# !/usr/bin/python # -*-encodeing:utf-8-*- class MyClass: """ a simple instance of a class """ = 12345 def f (self): return ' Hello World ' Print (MYCLASS.I)
class definition
Many classes tend to create objects that have an initialized state. Thus the class may define a special method named __init__ (), the instantiation of the class automatically calls the __init__ () method, the __init__ () method can have parameters, and the arguments are passed through __init__ () to the instantiation of the class, with the following example:
# !/usr/bin/python # -*-encodeing:utf-8-*- class Person (): def __init__ (self,*args): = Args[0] = args[1] = args[2= person (' Xiao Ming ' , ten,' male ')
__init__
4. Self represents an instance of a class, not a class
There is only one special difference between a method of a class and a normal function--they must have an extra first parameter name , according to the Convention its name is self.
# !/usr/bin/python # -*-encodeing:utf-8-*- class Test: def prt (self): Print (self) Print (self.) __class__ = Test () t.prt ()
View Code
5. Methods of Class
Inside the class, use the def keyword to define a method that, unlike a generic function definition, must contain the parameter self, which is the first argument, and self represents an instance of the class.
#!/usr/bin/python#-*-encodeing:utf-8-*-classDog:def __init__(self,name,blood,aggr,kind): Self.name=name SELF.HP=Blood Self.aggr=Aggr Self.kind=KinddefBite (Self,person):#dog bites, people drop blood . ifPerson.blood <=0:Print('%s bit the%s,%s and was bitten to death.'%(self.name,person.name,person.name))Else: Print('%s bit%s,%s, lost%s blood.'%(SELF.NAME,PERSON.NAME,PERSON.NAME,SELF.AGGR))classPerson :def __init__(self,name,blood,aggr,sex): Self.name=name Self.blood=Blood Self.aggr=Aggr Self.sex=SexdefAttack (Self,dog):ifDOG.HP <=0:Print('%s%s,%s was beaten to death.'%(self.name,dog.name,dog.name))Else: Print('%s hit%s,%s lost the blood of%s'%(Self.name, Dog.name, Dog.name,self.aggr)) Dog1= Dog ('Wong Choy', 990, 80,'Keddy') Alex= Person ('Zhang San', 999,998,'Unknown') Dog1.bite (Alex) Alex.attack (DOG1)example of man and dog
Python Object-oriented