Core Knowledge of Object-oriented Programming Foundation Part Two

Article Directory

5. Combing Core Knowledge

2. Face-to-Object Inheritance

2.1 Parent and Child Classes

Example

class animals:
    def eat(self):
      return self.obj_name + 'Having dinner'
    def drink(self):
      return self.obj_name + 'Drink'

class cat(animals):
    # class Derived class Name (Base class Name): //... Base class Name is written in parentheses, and Base class is specified in tuple post when it is defined.
    def __init__(self, name):
        self.obj_name = name

    def action(self):
        return (self.obj_name + 'The cat is cute')


class dog(animals):
    def __init__(self, name):
        self.obj_name = name

    def action(self):
        return (self.obj_name + 'The dog is very naughty')


o = cat('Miao')
print (o.action())
print (o.eat())
print (o.drink())
print ('=========')

o = dog('Barking')
print (o.action())
print (o.eat())
print (o.drink())

2.2 Some features inherited in python
  • Some features inherited in python:

1. Constructions of base classes (u init_() methods) in inheritance are not called automatically; they need to be called specifically in the construction of their derived classes themselves.

2. When calling a method of a base class, the class name of the base class needs to be prefixed with the self parameter variable.Unlike calling a normal function in a class, you do not need to take a self parameter

3.python always finds the method of the corresponding type first, if it cannot find the corresponding method in the derived class, it starts to look one by one in the base class (now look for the method called in this class, not in the base class)

4. If more than one class is listed in the inheritance tuple, it is called "multiple inheritance"

3. Object-oriented membership

Example

class reflect:
    def __init__(self,name):
      self.name = name

    def show(self):
         print ('haha')


obj = reflect('test_name')
a = hasattr(obj, 'name') # Finding members of an object from an object
b = hasattr(obj,'show') # Finding members of a class from an object
c = hasattr(reflect,'name') # Find members of objects from classes
d = hasattr(reflect,'show') # Finding members of a class from a class
print (a)
print (b)
print (c)
print (d)

  • You can see from the output

1. Reflectively, find members from a class, only the members in the class, not the members in the object

2. Reflectively find members from objects, both within classes and within objects

4. Static and common fields for members such as object-oriented

Example

Common field:

class province:
    def __init__(self,name):
        self.s_name = name #Common Field

    def guangdong(self):
        country = 'china'
        return self.s_name + '_' + country

    def guangxi(self):
        country = 'china'
        return self.s_name + '_' + country


obj= province('Guangdong')
print (obj.guangdong())

Static field:

class province:
    country = 'china'  # Static field
    def __init__(self,name):
        self.s_name = name

    def guangdong(self):
        return self.s_name + '_' + self.country

    def guangxi(self):
        country = 'china'
        return self.s_name + '_' + self.country


obj= province('Guangdong')
print (obj.guangdong())

country is globally valid at this time

  • What a static field means: Save only one copy of the duplicates in each object in the class

5. General Object-Oriented Member Access Rules

Example

class province:
    country = 'china'  # Static field
    def __init__(self,name):
        self.s_name = name

    def guangdong(self):
        return self.s_name + '_' + self.country

    def guangxi(self):
        country = 'china'
        return self.s_name + '_' + self.country


obj= province('Guangdong') # Instantiate a class province into an object obj
print (obj.guangdong()) # Accessing method guangdong() in class province by object obj
print (province('Guangdong').guangdong()) # Accessing the method guangdong() in a class via the class province
print (province.country)# Accessing fields in a class through a class

  • Unreasonable access

print (province('Guangdong'). guangdong())

6. Static and Class Methods for Object-Oriented Class Members

  • Static method: access by class
  • Common method: access by object

Example

class province:
    country = 'china'  # Static field
    def __init__(self,name):
        self.s_name = name

    def guangdong(self):
        return self.s_name + '_' + self.country

    def guangxi(self):
        country = 'china'
        return self.s_name + '_' + self.country

    @staticmethod
    def static_method():
        return 'static'

obj= province('Guangdong') # Instantiate a class province into an object obj
print (province.static_method()) # Class access to static methods is recommended!
print (obj.static_method()) # Accessing static methods through objects is not recommended!
print (obj.guangdong())# Accessing common methods through objects

summary

Accessed through classes: static fields, static methods

Access through objects: common fields, methods of classes

In a static method, there is no self, that is, any number of parameters can be passed to the static method.

7. Attributes of Object-Oriented Class Members

Feature: that is to fake a method into a field

1. Examples

class test:
    def __init__(self,name):
        self.s_name = name

    def start(self):
        return self.s_name + '_001'

    @property
    def end(self):
        return self.s_name + '_002'


obj = test('mangguo')
ret1 = obj.start() # There is no @property before the method, and parentheses () are required to execute the method.
ret2 = obj.end # The method is preceded by @property, and parentheses () are not required to execute the method
print (ret1,ret2)

2. Examples

class test:
    def __init__(self,name):
        self.s_name = name

    def start(self):
        return self.s_name


obj = test('mangguo') # Class instantiated into an object
print (obj.s_name) # Gets the field in the object
obj.s_name = '123' # Modify fields in objects
print (obj.s_name) # Gets the field in the object and finds that it has been modified successfully

3. Examples

class test:
    def __init__(self,name):
        self.s_name = name

    @property # Using property to disguise a method as a field (i.e., as a feature)
    def start(self):
        return self.s_name

    @start.setter # Modify the disguised field by @method name.setter
    def start(self,value):
    # print(value) # Output modified value
        self.s_name=value


obj = test('mangguo') # Class instantiated into an object
print (obj.start) # Accessing fields by object name. method name
obj.start = 'pro_change' # Modify fields by object name. method name, and by class name. method name='xxx'
print (obj.start) # Getting object names. How method names access fields
  • You can see from the results
    A change in the parameter value was found here.

8. Combing Object-Oriented Class Members

Object-oriented members consist of three parts: fields, methods, and attributes

Normal field: When you need each object to save a field of its own and save a different field

Static field: When the same data is required for each object

Static methods: Static methods are object-independent and do not need to use Object-Encapsulated content, because static methods are called through classes instead of objects, that is, static methods are used if you want to avoid accessing the contents of objects.

Class methods: Similar to static methods, class methods pass in class names.

Common methods: Common methods are generally called through objects, and when you need to use the contents of the objects, you can use the common methods.

Attribute: A common attribute that masquerades a method as a field for access.

Class calls include static fields, static methods

Calls using objects are: common fields, common methods, class methods

(with self, object call)

(no self, class call)

9. Object-oriented member modifiers

9.1 Member Modifier

Modifier: is to add some tags to the members and restrict them

Modifier Classification:

Private and common modifiers

Example

9.2 Static Field
class test:
    class_para = 'abc'
    __class_para_01 = '__abc' # Notice that there are two underscores before the static field class_para_01_u
    def __init__(self,name):
        self.s_name = name

    def test_func(self):
        print (self.s_name)

    def test_func01(self):
        return test.__class_para_01 # Returns the value of the modifier-modified member class_para_01

obj = test('haha') # Instantiate a class
print (test.class_para) # Output abc
print (obj.test_func01()) # Output_u abc
print (test.__class_para_01) # Report errors

9.3 Common Field
class test:
    class_para = 'abc'
    def __init__(self,name):
        self.__s_name = name

    def test_func(self):
        return self.__s_name # Returns a common field modified by a modifier

obj = test('abc')
print (obj.test_func())

class test:
    class_para = 'abc'
    def __init__(self,name):
        self.__s_name = name

    def test_func(self):
        return self.__s_name # Returns a common field modified by a modifier

obj = test('abc')
print (obj.__s_name) # Common fields in direct printing objects

103 original articles published, 8 praised, 4609 visits
Private letter follow

Tags: Python Attribute

Posted on Tue, 03 Mar 2020 17:45:35 -0800 by besly98