面向对象编程简介
先从各类数据结构讲起
列表(lists)
- Each position in a list can be of whatever type we wish.
- We can even store a list within a list
- This allows us to create complex data structures with variable structure.
列表里的各个位置可以是任何我们想要的类型,我们甚至可以在列表中存储列表。这个性质让我们能够用各种变量类型创造更复杂的数据结构。
类与对象(class and objects)
概念
- We can use a class to store different information
我们可以用类来储存不同的信息,一个类封装了信息和函数。
- We can create a class student.
- A student has a name, is doing a course and is enrolled in a list of modules.
举个例子,我们创造一个学生的类:
学生拥有自己的名字、有给的专业和课程。
- The class student is a generic concept.
- We can have instances of the class student
- Each student has a name, a course and a list of modules
If you think of individual students as student objects, then a Student class would be the general concept of a student.
叫做学生的类是个一般概念,这个类下面有很多的“例子”,也就是一个个学生个体,每个学生有各自的名字,专业与一系列课程。也就是类与对象,学生这个群体是一个类,而一个个学生个体是对象。
类的设计(Class Design)
- A class should encapsulate a concept
- The attributes (fields) of the class stored information about this concept.
- Methods manipulates the information in the class and add functionalities to the informaion.
刚有提到,类是一个概念。那么概念由什么去完成呢,即类的属性存储着这个概念的信息,而方法来处理着些信息并能添加功能。还是由刚刚的class Student来举例子:
# student Class
class Student:
""" Student has a name, takes a course and registers for a list of modules. """
def __init__(self, name, course):
""" Creates a student object with a name and a course and an empty list of modules """
self._name = name
self._course = course
self._modules = []
def addModules(self):
number = int(input("Enter number of modules: ")
for i in range(number):
module = input("Enter module name: ")
self._modules.append(module)
做点注释:
这个类使用了一个 constructor method (构造函数方法,一种魔法函数)一个构造函数方法的名字总是 __init__。(左右各两个下划线)
- Methods are functions that live inside a class and describe the behaviour of that class.
- All the methods need at least a parameter called self which is a reference to the object.
- Attributes in a class are variables that live inside the class. They can be declared anywhere in the class definition.
- Attributes of a class starts with _ (underscore) to project attribute and differentiate them from local variables.
- self.name: public 公开
- self._name: protect 保护
- self.__name: private 私有
- Every instance of a class will have its own methods and values for the attributes.
方法是位于类内部并描述该类行为的函数。
所有方法都至少需要一个名为self的参数,该参数是对对象的引用。
类中的属性是类内部的变量。它们可以在类定义中的任何位置声明。
类的属性以_(下划线)开头以投影属性,并将它们与局部变量区分开来。
类的每个实例都有自己的方法和属性值。
使用对象实例
student = Student("张三", "计算机") # 创造一个叫张三,学科是计算机的对象
student.addModule() # 调用了添加课程的方法(函数)
print("Student: ", student._name)
print("Modules: ", student._modules)
'此时我们调用属性的时候为了保护属性,我们最好不直接调用,需要对属性进行封装'
'也就是所谓的,getter methods 和 setter methods'
Getter and Setter Methods
# student Class
class Student:
""" Student has a name, takes a course and registers for a list of modules. """
def __init__(self, name, course):
""" Creates a student object with a name and a course and an empty list of modules """
self._name = name
self._course = course
self._modules = []
'To protect attributes.'
def getName(self): # getter method for attribute name
return self._name
def getCourse(self): # getter method for attribute course
return self._course
def getModule(self):
return self._modules
def setName(self, name): # setter method for attribute name
self._name = name
def setCourse(self, course): # setter method for attribute course
self._course = course
def addModules(self):
number = int(input("Enter number of modules: ")
for i in range(number):
module = input("Enter module name: ")
self._modules. Append(module)
# Each attribute may need getter and setter methods in pairs.
那么此时再创建对象:
student = Student("李四", "计算机科学与技术")
student.addModules()
print("Student: ", student.getName())
print("Course: ", student.getCourse())
print("Modules: ", student.getModules())
'若此时李四同学转专业转为软件工程'
student.setCourse("软件工程")
'以此完成对course这个属性的修改。'
以上就是对python面向对象编程的基本介绍,有任何问题可以进行评论欢迎一起探讨学习,一起进步。