Day25--python First Class and Object

Source: Internet
Author: User

I. Object-oriented definition

 When it comes to object-oriented, let's take a look at the process-oriented definition: The core of process-oriented program design is process (pipeline thinking), the process is to solve the problem, the process-oriented design is like a well-designed pipeline, consider when to deal with what things. Advantages : It reduces the complexity of the program and makes the process of the program become intuitive and straightforward. disadvantage : A set of pipeline or process is used to solve a problem, the production line of soda can not produce a car, even if it can, also must be big change, change a component, reaching.

Object-oriented definition: The core of object-oriented program design is object, everything is object. Advantages : It solves the extensibility of the program. A single modification of an object is immediately reflected in the entire system, such as the character of a character parameter in the game and the ability to modify it easily. cons : Poor controllability, inability to process-oriented programming pipelining can accurately predict the problem of the processing process and results, the object-oriented program once started by the interaction between the object to solve the problem, even God can not predict the end result. So we often see a game of people changes in a certain parameter is likely to lead to the ability of the bully to appear, a knife to kill 3 people, the game is out of balance.

ii. Classes and Objects

 A class is an abstract concept that integrates the features and actions of the thought of a class of things together. An object is a concrete ' object ' that is created based on a class and is also a combination of features and actions. It is important to keep in mind that classes are abstract templates, and instances are specific "objects" that are created from classes, each with the same method, but the data may be different.

iii. definition of Classes

 In Python, the definition of a class is by class keyword:

class Student (object):  # The first letter of the class name is capitalized, and object is the inheriting object    Pass

>>> Bart = instance of Student () #创建创建出 Student
>>> Bart
<__main__. Student object at 0x0000000002f12eb8> #变量 bart Point to an Student instance, followed by the 0x10a67a590 memory address of the instance
>>> Student
<class ' __main__. Student ' >

  Let's look at a specific example:

classStudent (object): Ability = ' excellent 'def __init__(self, Name, score): Self.name=name Self.score=scoredefask_questions (self):Print('{0} is asking a math question'. Format (self.name))defExamination (self):Print('{ 0} exam results are {1}'. Format (self.name,self.score)) Student1= Student ('xiaoming', 90,)#instantiation of a classPrint(Student1.__dict__)#View Instance PropertiesPrint(Student.__dict__)#View class PropertiesStudent1.ask_questions ()#instance calls the function property of the class, the instance does not have a function property, only the function property of the class can be calledPrint (student1.ability) #访问类的数据属性

1. The self that is passed in the function attribute of the class is actually the instance itself (STUDENT1)

2. The instance calls the function property of the class, the instance does not have a function property, only the function property of the class can be called

3. If the instance is not found in the __init__ when the data property is called, then it will be looked up, and no error will be found.

 Additions and deletions of class attributes

classStudent (object): Ability='Excellent'    def __init__(self, Name, score): Self.name=name Self.score=scoredefask_questions (self):Print('{0} is asking a math question'. Format (self.name))defExamination (self):Print('{ 0} exam results are {1}'. Format (self.name,self.score)) Student1= Student ('xiaoming', 90,)#instantiation of a class instancePrint(student.ability)#Viewstudent.ability='Good' #ModifyPrint(student.ability) Student.sport='Basketball' #IncreasePrint(Student.sport)Print(Student.__dict__)#Check to see if the sport is added to the property dictionary.delStudent.sport#DeletePrint(Student.__dict__)#See if the sport is removed from the property dictionaryPrint(Student.sport)#Attributeerror:type object ' Student ' has no attribute ' sport '

 Adding and deleting instance attributes

classStudent (object): Ability='Excellent'
L = [' A ', ' B ']
def __init__(self, Name, score): Self.name=name Self.score=scoredefask_questions (self):Print('{0} is asking a math question'. Format (self.name))defExamination (self):Print('{ 0} exam results are {1}'. Format (self.name,self.score)) Student1= Student ('xiaoming', 90,)#instantiation of a class instancePrint(student1.ability)#ViewStudent1.name='Xiaohua' #ModifyPrint(student1.name) Student1.sport='Basketball' #IncreasePrint(Student1.sport)Print(Student1.__dict__)#Check to see if the sport is added to the property dictionary.delStudent1.sport#DeletePrint(Student1.__dict__)#See if the sport is removed from the property dictionary#Output:Excellent xiaohuabasketball{'name':'Xiaohua','score': 90,'Sport':'Basketball'}{'name':'Xiaohua','score': 90}

  Add:

Call Student1 on the above. L.append (' C ') is changed to the L of the class, because there is no such attribute at all in the instance student1.

But if it's student. L = [' Q ', ' W ', ' e '] this is equivalent to adding a new instance of the L attribute list, which does not have any effect on L in the data properties of the class

Note: The new property is done by assigning a value operation .

Day25--python First Class and Object

Related Article

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.