Advanced Python object-oriented part of the Advanced --------

1. The class of simple architecture:

# 面向对象
# 类的最基本作用:封装代码
 
class Student():
    name = "菲神"
    age = 18
# 在类的里面写函数必须加上self即可
    def print_Students_files(self):
        print("I am "+self.name)
        print("I am "+str(self.age)+" years old")

# Python对象实例化的操作
student = Student()
#调用的是类方法而不是函数
student.print_Students_files()

Note: 1. The inside-defined functions in the class need to add self in brackets

           2. If the direct write print ( "I am" + name) compilation error occurs inside the print, solution manner as shown above by way of self.name, if not in the class but inside the module, the definition of the global variable name belong direct + name is correct

          3. Examples of objects when not required by the new keyword Python objects, but can write directly, as indicated above

          4. The method call is the class rather than a function (the two are different, there will be described below)

Code of the results:

Question: There is a class of students d1, then how to access the class in another module:

Solution: I started a d2.py file in the same folder

Code is as follows: (In other words we write code that is to say the best test class to instantiate an object when the new open a new file)

# d1中存在一个学生类,那么如何在另一个模块下访问这个类:
from d1 import Student

student = Student()
student.print_Students_files()

The result: From the point of view of the answer to the following operating results Ruqierzhi

Code Case 1:

class Student():
    name = ""
    age  = 20 
    # 构造函数:
    def __init__(self):
        print("student")
# 构造函数的调用是自动进行的
student = Student()
student.__init__()

operation result:

Question: Why there are two student?

Explanation : the constructor call is automatic

Code Case 2:

class Student():
    # 这实际就是类变量
    name = ""
    age  = 20 
    # 构造函数:
    def __init__(self, name, age):
        # 初始化对象的属性
        self.name = name
        self.age = age
        print("student")
student = Student("菲神", 18)
print(student.name)
print(student.age)

operation result:

annotation:

1. When the property is initialized object if write:

name = name
age = age

Run results:

Problems identified: first name not break out, although the second age, age printed, but the result is not what we are given the argument, but the beginning of the default age 20

Solution: The problem stems from the initialization object properties, that we correct way to write the code:

# 这实际就是实例变量
self.name = name
self.age = age

Question: Why does the above case, why is the value of the results of running the initialization of a start to define it? ? ? ?

答:在 初始化那里name=name age=age是局部变量,而不会改变全局变量,当访问实例变量,就会先从对象的实例列表里面寻找里面有没有name和age变量如果没有,不先返回None而是去到类变量寻找,这就是为什么不加self是打印的却是类变量的初始值。

区别类变量和实例变量:看上述的代码区”

 访问实例对象的信息:(就是说打印出你所创建对象的信息(实参))

# 在上面类的代码的基础上

  student1 = Student("菲神2019", 19)

  print(student1.__dict__)

运行结果:

构造函数的不仅仅可以用self还可以使用其他:可是Python建议我们最好使用self

def __init__(this, name, age):
    this.name = name
    this.age = age
#运行结果和上述一模一样

2.在构造函数的内部来访问类变量该如何做:

class Student():
    name = "勒布朗"
    age  = 20 
    sum1 = 0
    # 构造函数:
    def __init__(self, name, age):
        # 初始化对象的属性
        self.name = name
        self.age = age
        print(sum1)

运行结果:很明显,在构造函数里面是不能直接访问类变量的

解决方案:

class Student():
    sum1 = 0
    
    def __init__(self)
    
    # 方法一
    在构造函数里面访问:
    print(Student.sum1)
    print(self.__class__.sum1)
    # 在外面访问
print(Student.sum1)

运行结果:注意self.__class__.sum1的访问类变量的方式:

3.类变量的作用以及类变量的使用场景:

案例1:注意的是print(字符串+str(数据例如int))的写法

class Student():
    name = ""
    age = 0
    # 统计当前班级人数
    sum = 0

    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.__class__.sum += 1
        print("当前的班级的人数为:" + str(self.__class__.sum))
# 对象实例化的过程:
student1 = Student("菲神", 19)
student2 = Student("詹姆斯", 19)
student3 = Student("库里", 20)
print("班级的总人数为:" + str(Student.sum))

运行结果:

4.在Python里类方法和实例方法的区别:

参考:http://www.cnblogs.com/wcwnina/p/8644892.html

5.博主的案例------如何写类方法以及类方法的使用:还是上述的代码使用类方法来统计班级的人数

class Student():
    name = ""
    age = 0
    # 统计当前班级人数
    sum = 0

    
    def __init__(self, name, age):
        self.name = name
        self.age = age
    
    # 实例方法
    def dohomework(self):
        print("do your homework")

    # 类方法
    @classmethod
    def plus_sum(cls):
        cls.sum += 1
        print(cls.sum)

student1 = Student("菲神", 19)
Student.plus_sum()
student2 = Student("詹姆斯", 19)
Student.plus_sum()
student3 = Student("库里", 20)
Student.plus_sum()
print("班级的总人数为:" + str(Student.sum))

运行结果:

 

 

 

 

 

发布了98 篇原创文章 · 获赞 34 · 访问量 3万+

Guess you like

Origin blog.csdn.net/weixin_42133768/article/details/88074515