Python learns to be object oriented

Source: Internet
Author: User

Object oriented
Object Oriented thinking:
An object is a tangible entity that can be accurately described.
Object-oriented programming is a switch from God's perspective
The process-oriented programming used before is the result-oriented programming, a pipelining
Come down with a fixed result.
Process-oriented programming good thinking, the disadvantage is poor scalability, easy to cause code security problems
Object oriented
Class: A category of things that have the same properties and methods
Objects: Things that have specific properties
Class name: Do not write parentheses define a class that writes a property
Capitalize the first letter of the class name
class Person:
role= ' person '
print (person.role)
class Fetch value ↑
class Person:
role= ' person '
def attack (self)
print (' attact ')
print (person.role)
In a class, a method is essentially a function, with a parameter that must be passed, and the self parameter is called something else.
Instantiate an Object
Alex=person () created an object
Alex.attack () When you use an object to invoke his method, it automatically passes a parameter, which is the object itself.
Egon=person ()
Egon.attack () ibid.
Can take a value print self which object value
def __init__ (Self,name,sex,aggr,blood) must double-down the route init
Self.name=name
Self.sex=sex
Self.aggr=aggr
Self.blood=blood
Similar to the dictionary definition, but the grammar is different self must use
Self is now equivalent to a dictionary
Alex=person (' Alex ', ' Male ', 250,20000) to the above __init__ the parameters to be instantiated when passing
Alex.name can get the data for Alex name in the class.
Alex.attack ()
The following method can also use the data in Def __INIT__, because it has already been created
Self is the object itself, and the class invokes the data in the
class Dog:
def __init__ (self,a,b,c,d): Object Properties
self.name=a
Self.sex = b
Self.aggr = C
Self.blood = d
def attack (self):
print ('%s beat up '%self.name)
Egon=dog (' Egon ', ' Man ', 500,90000)
Egon.attack ()

Class Person:
role= ' person ' static property
Print (Person.role)
Static attribute class properties, all objects common to the property, can be called directly by the class
object property, which belongs to only one object, must be called with an object after instantiation

When object-oriented is used: When you need to pass the same arguments over and over again to some functions

Class names can call class properties to view class methods but cannot call class methods
Object names can call object properties and can call class properties
The name of the object. Method ()

Python learns to be 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.