01python learning

--- --- restore content begins

Object-Oriented Programming Fundamentals

Live in the moment programmers should have heard "object-oriented programming" is, often someone asked if he could use the next sentence to explain what "object-oriented programming," we take a look at more formal argument.

Object-oriented programming: a set of data structures and the method for processing thereof subject (object), the behavior of the objects of the same class as summarized (class), by hiding internal details encapsulation class (encapsulation), implement (Inheritance) by inheritance specialization class (specialization) and generalization (generalization), based on the dynamic object type by assigning polymorphism (polymorphism).

Classes and Objects

Simply put, the class is a blueprint and template objects, and objects are instances of classes. Although this explanation was sort of like the concept in the interpretation of the concept, but at least we can see from this statement, the class is an abstract concept, but the object is a specific thing. In object-oriented programming world, everything is an object, the object has attributes and behavior of each object is unique, and the object must belong to a certain category (type). When we put the static characteristic (property) has a lot of common features of objects and dynamic characteristics (behavior) are extracted, you can define a named "class" thing.

Written functions

1.

Identify the function name (): def funcName ():

  Colon will indent a tab return ......

……                                                                              ……

2. Identify the class name (): class className (object):

  Colon will indent a tab ......

3. Import library import library name

4.for cycle, if counting is used in the case of

range (start, end, [step]) (start, end, step) Range interval after closing a front opening of the

5. Output Formatting

5.1% ->% (), then the integer, floating-point contact% f after, the% d% s, after a string.
5.2 {} -> the format (), any type of it can be directly passed, and then format of output.
6.print console (terminal) Print
6.1 end to end in what way, by default a newline "\ the n-"
6.2 flush flush the buffer.
Function class can have its own parameters
when you want to share this parameter, then hit the "self" mark.
The definition of class
class A(object):
    def __init__(self,a):
        self.a = a
    def B(self,b):
        self.b = b
        print(b)
    def C(self):
        print(self.b)
J = A(1000)
J.B(10)
J.C()
# Object instead of a parameter
Defining classes only two steps:
1. identifier ClassName class (Object):
2. __init__ rewrite function, but keep in mind, some play on a "self" label
   since the latter will learn inherited, inherited as when the situation might have collided with the function name.
   or, the class will contain multiple variables, multiple functions, then you need to use the "self" means to differentiate.
3. kind of mass participation in __init__ function. Note that if you use multiple parameters may be a function, then
   you just go to define the parameters in the __init__ function
Example: Enter two digits, these two numbers of the print is poor, and, multiply, divide, divisible, modulo square

Example: input an age, if less than 20 years older than 18 years can view four stages, if more than 20 less than 50 years may be viewed island, such as greater than 50 years, it do not look, could not carry.

Example: Defining three functions, the first function: to determine the age, the second function: sex determination (male, female, unknown) third function: to return the young (less than 18 years old), middle-aged (18-40 years old) , elderly (over 40 years) and sex.

Example: determining whether a number is a prime number

 

 

           

 

 

--- end --- restore content

Guess you like

Origin www.cnblogs.com/sunyuxin/p/11311433.html