python0.14-----Object-oriented ideas/classes/objects

Source: Internet
Author: User
Tags access properties define function

Object Oriented thinking:
1. Put together the data and the methods of operation of the data as an interdependent whole-object.
2. Abstract similar objects into their generality and form a class. For example: Bosses and employees have a lot in common and can form a person class.
3. Most of the data in a class can only be processed using the methods of this class. For example: I have 100 dollars, it is impossible for you to spend.
4. The class is connected to the outside world through a simple external interface. Objects communicate with each other through a message.
5. The procedure flow has the user to decide in the use.

Process oriented:
Emphasis is on functional behavior and what steps are required to solve the problem.
For example: Build a house to live cement, then brick ...
The focus is on which objects are needed to solve the problem.
For example, to build a house, you just need to find someone who can build a house.

Different ideas for putting elephants in refrigerators:

Process-oriented thinking:
1 Open the refrigerator door.
2. Put the elephant in the refrigerator.
3. Close the refrigerator door.

Object Oriented thinking:
1. Find a person who can open the refrigerator.
2. Find the person who can put the elephant in the refrigerator.
3. Find a person who can turn off the refrigerator.

Because there are many things in life that we don't want to do. So I want to find someone who can do these things. If this object does not exist, the object is created. Let it do these things, and then we finish the task.

For example:

A drawing is equivalent to a class, a car equals an object
A lot of different colors can be made against the drawings.
Similarly, different objects can be created from a class.

Design of the class:
Only care about 3 things:
Class Name: See the name of the meaning, the first letter capitalized, the other follow the hump principle.
Properties: See name only, others follow the hump principle.
Behavior (method or function): see the name, others follow the hump principle.
For example: The wife's class named Wife,wife property has: name, age and so on, its methods are: eat, drink and so on.

To create a class:

Class: A data type that does not itself account for memory space, similar to the Number,string,booleann you have learned. An instantiated object (variable) created with a class that occupies memory space.


Class type (parent class list):

class Person (object): #object: #object为所有类的父类, the parent class is usually written without a suitable parent class.
name= ""
#定义方法 (define function):
#注意: The parameter of the method must be self when the first argument, self represents an instance of the class (an object).
def run (self):
print (' Run ')
def Eat (Self,food)
print (' Eat ' +food)

Instantiating an object:
Format: Object name = Class name (argument list) #若没有参数列表, and cannot omit parentheses
Example: Pr1=person ()  

Pr2=person ()

Note: The object is in the heap area, and the variable is in the stack. pr1,pr2 in the stack area, they store the object address of the heap. Different objects have different addresses in the heap area.

Accessing the properties and methods of the object:
per=person () #实例化对象per
Access properties:
format: Object name. property name
Assignment: Object name. property name = new value. ' Bob '
" Span style= "FONT-SIZE:16PX; font-family: "microsoft yahei" ">per.age=18

Access method:
Format: Object name. Method Name (parameter list) () (' Apple ')

#题目: Design class implementation people shot bullets

Class Person:
def __init__ (Self,gun):
def fire (self):
Self.gun.shoot ()
def fillbullet (self):
Print (' Fill a pitch ')

class Gun:
def __init__ (Self,bullet):
def shoot (self):
If self.bullet.bulletcount<=0:
print (' no bullets ')
print (' also%d rounds '%self.bullet.bulletcount)

Class Bullet:
def __init__ (Self,bulletcount):
Self. Bulletcount=bulletcount


Bullet=bullet (5)

Gun=gun (Bullet)

Per=person (gun) ()

Python0.14-----Object-oriented ideas/classes/objects

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