Python Learning path-day6-object oriented

Source: Internet
Author: User

First, object-oriented learning characteristics
    • Class
A class is an abstraction, a blueprint for a class of objects that have the same properties. Prototype. The properties and common methods of these objects are defined in the class.

Process-oriented programming and object-oriented programming:

Process-oriented Programming: Use a series of instructions to tell the computer how to execute the basic design idea is that the program starts with solving a big problem and then breaks down a big problem into many small problems or sub-processes object-oriented programming: OOP programming is using "classes" and "objects" To create a variety of models to achieve the real world description of the world is the object of everything. As long as it is an object, it must belong to a certain species.

Object-Oriented Programming:

OOP programming is the use of "classes" and "objects" to create a variety of models to achieve the real world of the description of the world is the object of everything. As long as it is an object, it must belong to a certain species.

Objiect object:

An object is an instantiated instance of a class, a class can instantiate multiple objects, and each object can have different properties

Encapsulation Package :

Inheritance Inheritance :

A class can derive a subclass, a property defined within the parent class, a method that automatically inherits the quilt class

    • Object

An object is an instantiated instance of a class, a class must be instantiated before it can be called in a program, a class can instantiate multiple objects, and each object can have different properties

Ii. Examples

1. Create a class

Class Role (object):  #定义一个类    def __init__ (self): #初始化        Pass

The above __init__ () is called the initialization method (or constructor method), when the class is called, this method (although it is a function form, but in the class is not called the function, called the method) will automatically execute, do some initialization action.

2. Example

__author__ = ' LW ' class Role (object): Def __init__ (self,name,role,weapon,life_value=100,money=15000): ##定义一个类, class是定义类的语法,Role是类名,(object)是新式类的写法Self.name = name ##初始化函数,在生成一个角色时要初始化的一些属性就填写在这里Self.role = Role## # #self. Name = R1.name Self.weapon = Weapon Self.life_value = Life_value Self.money = Money def shot (self): print ("Shoot ing ...) def got_shot (self): print ("Ah...,i got shot ...") def buy_gun (self,gun_name): Print ("Just Bo Ught%s "%gun_name) r1 = role (' Alex ', ' police ', ' AK47 ') #生成一个角色, that is, generate an instance (object) r2 = role (' Jack ', ' terrorist ', ' B22 ') #生成一个角色, Generate an instance (object)
We see that when we created the role above, we did not pass the value to __init__, and the program did not give an error because the class was calling its own __init__ (...). When you assign a value to the self parameter,
r1 = Role( ‘Alex‘ , ‘police‘ , ‘AK47’) #此时self 相当于 r1 ,  Role(r1,‘ Alex ‘,‘ police ‘,‘ AK47’) r2 = Role( ‘Jack‘ , ‘terrorist‘ , ‘B22’)#此时self 相当于 r2, Role(r2,‘ Jack ‘,‘ terrorist ‘,‘ B22’)

Python Learning path-day6-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.