current position:Home>How IOS developers learn Python Programming 15 - object oriented programming 1

How IOS developers learn Python Programming 15 - object oriented programming 1

2022-02-01 01:10:15 Sissy's father

This is my participation 11 Yue Gengwen challenges 15 God , Check out the activity details :2021 The last time Gengwen challenge .

object-oriented programming (Object Oriented Programming), abbreviation OOP, It's a kind of programming idea .

What's the difference between process oriented and object oriented ?

  • Process oriented : Write code from top to bottom according to business logic ;
  • object-oriented : Bind data to functions , encapsulate . Reduce the rewriting process of repeated code .

Class and object introduction

class It's an abstract concept , just Templates . A collection used to describe objects with the same properties and methods . such as :

  • The company can be expressed as a class .

The existence of a specific thing , It can be seen and touched in the real world . such as :

  • Ali Represents a specific member of the company . This is the object .

The relationship between classes and objects

In development , We can classify objects .

such as , Analyze the characteristics of class students and classify them :

option = {
    title: {
        text: ' Zhang San ',
        subtext: ' Class two in three years '
    },
    tooltip: {
        trigger: 'item',
        formatter: "{a} <br/>{b} : {c}%"
    },
    toolbox: {
        feature: {
            dataView: {readOnly: false},
            restore: {},
            saveAsImage: {}
        }
    },
 
    series: [
        {
            name:' Funnel diagram ',
            type:'funnel',
            left: '10%',
            top: 60,
            //x2: 80,
            bottom: 60,
            width: '80%',
            // height: {totalHeight} - y - y2,
            min: 0,
            max: 100,
            minSize: '0%',
            maxSize: '100%',
            sort: 'descending',
            gap: 2,
            label: {
                show: true,
                position: 'inside'
            },
            labelLine: {
                length: 10,
                lineStyle: {
                    width: 1,
                    type: 'solid'
                }
            },
            itemStyle: {
                borderColor: '#fff',
                borderWidth: 1
            },
            emphasis: {
                label: {
                    fontSize: 20
                }
            },
            data: [
                {value: 60, name: ' hobby '},
                {value: 40, name: ' grade '},
                {value: 20, name: ' Age '},
                {value: 80, name: ' Gender '},
                {value: 100, name: ' full name '}
            ]
        }
    ]
};
 Copy code 

that , We can classify the public characteristics of the students in class two of three years . And build a student class :

option = {
    title: {
        text: ' Class two in three years '
    },
    tooltip: {},
    series: [
        {
            type: 'graph',
            layout: 'none',
            symbolSize: 70,
            label: {
                show: true
            },
            edgeSymbol: ['circle', 'arrow'],
            edgeSymbolSize: [4, 10],
            edgeLabel: {
                fontSize: 10
            },
            data: [{
                name: ' Students in class 2, grade 3 ',
                x: 300,
                y: 300
            }, {
                name: ' Zhang Si ',
                x: 800,
                y: 300
            }, {
                name: ' Li San ',
                x: 500,
                y: 100
            }, {
                name: ' Wang two pock marks ',
                x: 550,
                y: 500
            }],
            // links: [],
            links: [{
                source: 0,
                target: 1
            }, {
                source: 0,
                target: 2
            }, {
                source: 0,
                target: 3
            }],
            lineStyle: {
                opacity: 0.9,
                width: 2,
                curveness: 0.2
            }
        }
    ]
}
 Copy code 

Composition of classes

Class by 3 Parts make up :

  • The name of the class : Class name
  • Attributes of a class : A set of data
  • Class method : Methods that allow operations on classes

️ Be careful : Class names are usually humped , Try to make the literal meaning reflect the role of class . Python Use class Keyword to define the class , Its basic structure is as follows :

class  Class name :
    pass
 Copy code 

Python in , You can create objects according to the defined classes :

 Object name  =  Class name ()
 Copy code 

Define a complete class :

#  Definition 
class  Class name :
    def  Method name (self, Parameters ):  #  Function in class : Called method 
        pass
        
#  perform 
s =  Class name ()         #  Create objects ( example )  The whole process is instantiation 
s. Method name ( Parameters )     #  Call the method in the class 
 Copy code 

Object initialization

When defining a method in a class , You will find that the system helps us automatically create self Parameters , And when the method of the object is called , There is no need to pass in self Parameters . So this one self What is it? ?

actually , We need to be clear self Two concepts of :

  • self Itself is a formal parameter ;
  • self It's the object itself .
  1. __init__() Called initialization method , It can also be called construction method . When you create an object , The method is executed automatically , Set initial values for the properties of the object : 
class Student(object):
    def __init__(self,name,age):   
        self.name = name
        self.age = age
        
    def info(self):
        print(self.name,self.age)

ls = Student(" Li Si ",18)     
ls.info()
 Copy code 
  1. __str__() When printing out object variables , Print customized content at the same time , Pass custom content through return Keywords return : 
class Students:
    def __init__(self):
        self.name = " Li Si "
        self.age = 18

    def __str__(self):
        return self.name


ls = Students()
print(ls)       #  Li Si 
 Copy code 

️ Be careful : The return value must be a string .

copyright notice
author[Sissy's father],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010110126454.html

Random recommended