day23 learning objects finishing -Python basis

2019/08/26 Learning finishing

Object oriented python

Classes and Objects

That class category, type, object-oriented design is the most important concepts, objects and features of the combination of skills, and the category is a series of similar objects features and skills combination

So the question is, first there exists a specific object (such as the presence of a specific person), or to some human concept, this issue needs to look at two cases

In the real world: first, the object, and then have class

The world is certainly to be a variety of object actually exists, then with the development of human civilization, human beings different viewpoints summed up the different categories, such as the concept of humans, animals, plants, etc.

Also he said that the object is a concrete existence, while the class is just a concept, not real

In the program: sure that define the class, after generating the object

This function is similar to the use of first-defined function, after calling the function, the class is the same, the program will need to define a class, call the class after

Is not the same, the function to call the code body will execute the function returns a result of executing the function body, which will call the class generates an object, the object is returned

# 类的定义
class dog():
    def __init__(self):
        self.name = name
    def func(self,name):
        print(self.name)
dog1 = dog('happy')
dog1.func()


# happy

Class defined function is a function attribute class, a class can use, but use is a normal function of it, implies the need for full compliance with the rules of function parameters, which pass several values ​​to pass several

First, the introduction of

  • For the above-mentioned class of students if the class attribute changed, the properties of other objects will change
OldboyStudent.school = 'OLDBOY'
print(stu1.school)
OLDBOY
print(stu2.school)
OLDBOY

Second, the unique features of custom objects

print(stu1.__dict__)
{}
print(stu2.__dict__)
{}
  • Nature object similar to a class, but also a name space, but the name of the object space to store the object unique name, and class objects are stored in a common name. Therefore, we can directly target custom name alone.
stu1.name = 'tank'
stu1.age = 18
stu1.gender = 'male'

print(stu1.name, stu1.age, stu1.gender)
tank 18 male
try:
    print(stu2.name, stu2.age, stu2.gender)
except Exception as e:
    print(e)
'OldboyStudent' object has no attribute 'name'
stu2.name = 'sean'
stu2.age = 19
stu2.gender = 'female'

print(stu2.name, stu2.age, stu2.gender)
sean 19 female

Third, the property find
four custom attribute class definition phase

def init(obj, x, y, z):
    obj.name = x
    obj.age = y
    obj.gender = z


init(stu1, 'tank1', 181, 'male1')
print(stu1.name, stu1.age, stu1.gender)
tank1 181 male1
init(stu2, 'sean1', 191, 'female1')
print(stu2.name, stu2.age, stu2.gender)
sean1 191 female1
  • Using the above method, although let's custom properties is simpler, but still too much trouble, if the timing properties can be triggered automatically when an object is instantiated, it is more convenient, you can use __init__ method of the class.

[

class OldboyStudent:
    school = 'oldboy'

    def __init__(self, name, age, gender):
        """调用类的时候自动触发"""
        self.name = name
        self.age = age
        self.gender = gender
        print('*' * 50)

    def choose_course(self):
        print('is choosing course')


try:
    stu1 = OldboyStudent()
except Exception as e:
    print(e)
__init__() missing 3 required positional arguments: 'name', 'age', and 'gender'
stu1 = OldboyStudent('nick', 18, 'male')
**************************************************
  • Can be found through the above phenomenon occurs when the calling class two things:
    1. Create an empty object
    2. Automatic trigger class __init__ function will stu1 and parameters within the parentheses with the incoming call class
print(stu1.__dict__)
{'name': 'nick', 'age': 18, 'gender': 'male'}

Find a class of property

First, look for properties

  • Start with the object to find its own name space, then do not go to class to find if the class nor the error

Bound method object class

class OldboyStudent:
    school = 'oldboy'

    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.sex = gender

    def choose_course(self):
        print(f'{self.name} choosing course')

    def func(self):
        print('from func')
  • Class data attributes and function attributes are defined in a namespace for all shared objects with
  • Only data attributes defined object name space, and is unique to the object data attributes

Second, the class-bound objects object

nick
oldboy
  • Class defined function is a function attribute class, a class can use, but use is a normal function of it, implies the need for full compliance with the rules of function parameters, which pass several values ​​to pass several
print(OldboyStudent.choose_course)
<function OldboyStudent.choose_course at 0x10558e840>
try:
    OldboyStudent.choose_course(123)
except Exception as e:
    print(e)
'int' object has no attribute 'name'

Third, the object uses the object of binding method

print(id(stu1.choose_course))
print(id(stu2.choose_course))
print(id(stu3.choose_course))
print(id(OldboyStudent.choose_course))
4379911304
4379911304
4379911304
4384680000
print(id(stu1.school))
print(id(stu2.school))
print(id(stu3.school))
4380883688
4380883688
4380883688
print(id(stu1.name), id(stu2.name), id(stu3.name))
4384509600 4384506072 4384507864
stu1.choose_course()
nick choosing course
stu2.choose_course()
sean choosing course
stu3.choose_course()
tank choosing course
  • Added: class defined functions, classes can really use, but in fact a function of the class definition in many cases are used to bind to the target, so the functions defined in the class should own a parameter self
stu1.func()
from func
stu2.func()
from func

Guess you like

Origin www.cnblogs.com/Wunsch/p/11412740.html