Day 10: Python classes and objects

Keywords: Python Programming Java Attribute

by programmer

When we first came into contact with programming, we learned that there are two major ideas in the field of programming: process oriented and object-oriented. Python is an object-oriented language. If you have known the idea of object-oriented, naturally you know that the idea of object-oriented contains two basic concepts: class and object. Let's learn the class and object of Python in detail!

1 Basic Concepts

1.1 object oriented

Object oriented is a kind of abstraction and a method of looking at problems in a classified way. In terms of Java programming idea, everything is an object. Object oriented has three major characteristics: encapsulation, inheritance and polymorphism.

Category 1.2

As mentioned above, object-oriented is a way of looking at problems in a classified way. A classification is a class, which can be regarded as an abstract template, such as Car class.

1.3 object

Objects are instances created from classes.

2 basic use

2.1 definition of class

# Definition of class
class Car:
    pass

2.2 creation of objects

# Create instance object c of Car
class Car:
    pass
	
c = Car()

2.3 defining attributes in a class

# Define the attribute name of Car class
class Car:
    name = 'BMW'

Methods in 3 categories

3.1 built in method

When Python creates any class, it will include some built-in methods, mainly including the following:

method explain
__init__ Constructor, called when the object is generated
__del__ Destructor, used when releasing objects
__repr__ Printing, converting
__setitem__ Assign by index
__getitem__ Get values by index
__len__ Get length
__cmp__ Comparison operation
__call__ function call
__add__ Additive operation
__sub__ Subtraction
__mul__ Multiplication
__div__ Division operation
__mod__ Remainder operation
__pow__ Power

3.2 custom method

Python has three common methods: instance method, class method, and static method, all of which are defined in the class.

3.2.1 methods

Class methods are methods that operate on the class itself as an object.

Definition and use

'''
//Class method (adjustable class variable, callable by instance, callable by class)
1,Class method passed@classmethod The decorator can only access class variables, not instance variables;
2,adopt cls The parameter passes the current class object and does not need to be instantiated.
'''
class Car(object):
    name = 'BMW'
    def __init__(self, name):
        self.name = name
    @classmethod
    def run(cls,speed):
        print(cls.name,speed,'travel')
# Access 1
c = Car("bmw")
c.run("100 Mai")
# Access mode 2
Car.run("100 Mai")
3.2.2 static method

A static method is a function in a class and does not require an instance.

Definition and use

'''
//Static method (adjustable class variable, callable by instance, callable by class)
1,use @staticmethod Decorated without self Parameter method;
2,Static methods are nominally classified and managed. In fact, they cannot access any properties in classes and instances;
3,The call does not need to pass a class or instance.
'''
class Car(object):
    name = 'BMW'
    def __init__(self, name):
        self.name = name
    @staticmethod
    def run(speed):
        print(Car.name,speed,'travel')
		
# Access 1
c = Car("bmw")
c.run("100 Mai")
# Access mode 2
Car.run("100 Mai")
3.2.3 example method

An instance method is a method that an instance of a class can use.

Definition and use

# Instance method (adjustable class variable, adjustable instance variable, callable by instance)
# The first parameter is forced to the instance object self.
class Car(object):
    name = 'BMW'
    def __init__(self, name):
        self.name = name
    def run(self,speed):
        print(self.name,speed,'travel')

# visit
c = Car("bmw")
c.run("100 Mai")

Inheritance of class 4

Definition and use

# Basic syntax: class ClassName(BaseClassName)
# Superclass
class Car(object):
    name = 'BMW'
    def __init__(self, name):
        self.name = name
    def run(self,speed):
        print(self.name,speed,'travel')
        
# Subclass
class BMWCar(Car):
    conf = "Economical and applicable"
    pass

# Call the run method in the parent Car
bc = BMWCar("BMW Economical car")
bc.run("100 Mai")

Five kinds of polymorphism

Definition and use

# Superclass
class Car(object):
    name = 'BMW'
    def __init__(self, name):
        self.name = name
    def run(self,speed):
        print('Car-->',self.name,speed,'travel')

# Subclass 1
class BMWCar(Car):
    def run(self,speed):
        print('BMWCar-->',self.name,speed,'travel')

# Subclass 2
class SVWCar(Car):
    def run(self,speed):
        print('SVWCar-->',self.name,speed,'travel')

# Call the run method
c = Car("Car")
c.run("120 Mai")

bc = BMWCar("bmw")
bc.run("100 Mai")

sc = SVWCar("public")
sc.run("80 Mai")

# Output results
'''
Car--> Car 120 Walking
BMWCar--> BMW 100 miles
SVWCar--> 80 miles for Volkswagen
'''

In the above example, we can see that c, bc and sc are different types of objects. When they call run methods, they call methods in their own classes, which is polymorphism.

summary

This section introduces the definition and use of Python classes and objects, provides support for Python engineers, and can flexibly use different types of methods in the project according to the actual situation.

Example code: Python-100-days-day010

reference resources:

https://www.readwithu.com/Article/python9/Preface.html

Pay attention to the official account: python technology, reply to "python", learn and communicate with each other.

Posted by clairian on Sun, 24 May 2020 02:29:45 -0700