Classes and methods in python

1.There are no hard and fast rules for classes in python. For example, like Java, every class ends in .class, but ends in .py;

class testClass:
    def setModels(self,name,age):
        self.age = age;
        self.name = name;

2. The methods in the class in python must have a default value, otherwise an error will be reported;

class testClass:
    def setModels(self,name,age):
        self.age = age;
        self.name = name;

3.There are class attributes in python, which are similar to static constants in Java;

class testClass:
    testClassName = "我是python的类属性,可以直接通过类名点我得到我";
    print(testClass.testClassName)

4.The construction method in python does not support method overloading, there is one and only one. Note that the python construction method defaults to_init__;

class testClass:
     def __init__(self,a,b):
         print("我是python的构造方法");

5. In python, the construction method can be defined as a flexible method for multi-parameter transmission;

class testClass:
    #定义灵活的可传递任何数据的构造方法
    def __init__(self,**map):
        print("我是python的构造方法" + map.get("name"));
        print("我是python的构造方法" + map.get("age"));

6.Class variables in python can be accessed by instantiating objects, but Java is not;

class testClass:

    testClassName = "我是python的类属性,可以直接通过类名点我得到我";

from src.test.test import test
from src.testClass.testClass import testClass

if __name__ == '__main__':
    # testObject = test();
    # testObject.testClassFunction();

    testClassA = testClass();
    print("测试实例化对象访问类变量".format(testClassA.testClassName));

7. Changing the class variable of the instantiated object in python will not modify the class variable. This may be more difficult to understand. The following code demonstrates;

from src.test.test import test
from src.testClass.testClass import testClass

if __name__ == '__main__':
    # testObject = test();
    # testObject.testClassFunction();

    testClassA = testClass();
    print("测试实例化对象访问类变量".format(testClassA.testClassName));
    # 修改实例化对象的变量 这个时候这个变量已经变味了实例化对象的属性,在内存中是单独的一个空间了
    testClassA.testClassName = "我会七十二变";
    # 打印实例化变量和类变量,发现类变量的值并未改变
    print(testClassA.testClassName);
    print(testClass.testClassName);

8. In python, class variables can be flexibly and dynamically defined. Personally, I feel that it is the operation of python. It should be noted that the attributes defined by the instantiated object and the attributes of the class definition cannot be mutually accessed;

from src.test.test import test
from src.testClass.testClass import testClass

if __name__ == '__main__':
   
    # 测试python动态定义类变量;
    testClassA = testClass();
    testClassA.dynamicVariablesA = "我是python的实例化对象的动态变量";
    testClass.dynamicVariables = "我是python的动态变量";
    print(testClass.dynamicVariables);
    print(testClassA.dynamicVariablesA);

9.In order to avoid confusion caused by arbitrarily defining class variables in python, it provides

class testSlots:

    # 使用了__slots__,那么这个实例化对象中就只存在name和age的属性,无法限制类属性;
    __slots__ = ("name","age");

10.Internal classes in python, python's internal classes can be defined not only in classes, but also in methods, which are more complicated and Baidu by themselves;

Guess you like

Origin blog.csdn.net/weixin_44887276/article/details/114383012