第一个简单的类
class FirstClass: #Define a class object def setdata(self, value): #Define class methods self.data = value #self is the instance def display(self): print(self.data) #self.data: per instance
测试结果:
继承
class FirstClass: #Define a class object def setdata(self, value): #Define class methods self.data = value #self is the instance def display(self): print(self.data) #self.data: per instance class SecondClass(FirstClass): #Inherits setdata def display(self): #Changes display print('Current value = "%s"' % self.data)
测试结果:
运算符重载
class FirstClass: #Define a class object def setdata(self, value): #Define class methods self.data = value #self is the instance def display(self): print(self.data) #self.data: per instance class SecondClass(FirstClass): #Inherits setdata def display(self): #Changes display print('Current value = "%s"' % self.data) class ThirdClass(SecondClass): def __init__(self, value): #__init__方法,也称为构造函数方法,用于初始化对象的状态 self.data = value def __add__(self, other): #运算符重载,"+"表达式会触发"__add__"方法 return ThirdClass(self.data + other) #对于print,Python把要打印的对象传递给__str__中的self,该方法返回的字符串看做是对象的打印字符串 def __str__(self): return '[ThirdClass: %s]' % self.data def mul(self, other): #in-place change: named self.data *= other
测试结果:
一个简单完整的类
一个完整的例子(Encapsulation, Inheritance, Polymorphism)
class Person: def __init__(self, name, job=None, pay=0): self.name = name self.job = job self.pay = pay def getLastName(self): return self.name.split()[-1] def giveRaise(self, percent): self.pay = int(self.pay * (1 + percent)) def __str__(self): return '[Person: %s, %s]' % (self.name, self.pay) class Manager(Person): def __init__(self, name, pay): #Redefine constructor Person.__init__(self, name, 'manager', pay) def giveRaise(self, percent, bonus=.10): #Redefine at this level Person.giveRaise(self, percent + bonus) #Call Person's version def __str__(self): return '[Manager: %s, %s]' % (self.name, self.pay) if __name__ == '__main__': p1 = Person('Michael Jordan') p2 = Person('Kobe Bryant', job='developer', pay=100000) print(p1) print(p2) print(p1.getLastName(), p2.getLastName()) p2.giveRaise(.10) print(p2) p3 = Manager("David Stern", 50000) p3.giveRaise(.10) print(p3.getLastName()) print(p3) print('--All three--') for object in (p1, p2, p3): object.giveRaise(.10) print(object)
测试结果:
相关推荐
面向对象编程(OOP)是Python的一大特点。类(class)是创建对象的模板,通过关键字class定义。类中包含属性(attribute)和方法(method),实例化后的对象可以通过点运算符访问。继承(inheritance)和多态...
最后,面向对象编程(OOP)是Python的另一大特点。Python支持类(class)的定义,以及继承、封装和多态等概念。例如: ```python class Person: def __init__(self, name, age): self.name = name self.age = age...
For the engineer who is already proficient in Java, it would be a waste of time to study a Python textbook that begins with the basic concept of object-oriented programming, since the concept of OOP ...
Chapter 8 Introduction to Object Orienting Programming (OOP) 139 8.1 OBJECT PARADIGM AND PYTHON 139 8.2 EXPLORING THE JARGON 140 8.3 CREATING CLASSES 142 8.4 INHERITANCE 145 8.5 SPECIAL METHODS 149 ...
This course is meant for programmers who wants to learn Python programming from a basic to an expert level. The course is mostly self-contained and introduces Python programming to a new reader and ...
面向对象编程(OOP)也是Python的重要特性。你可以定义类,使用class关键字,以及封装数据和方法。类的继承使你能创建层次结构的类,多态性则允许不同类的对象以相同的方式进行操作。 最后,让我们提到Python的几个...
在这个压缩包中,"basic_grammar"文件夹很可能是包含了Python的基础语法教程或者示例代码。 在Python的语法基础中,首先我们需要理解变量的使用。Python中的变量不需要预先声明类型,可以直接赋值,如`name = ...
"Basic_Python:Python走吧"这个标题暗示我们将探讨Python的基础知识,帮助初学者开始他们的Python编程之旅。 首先,Python的核心概念是“易于学习,易于阅读”。它的语法结构清晰,使得代码更接近自然语言,降低了...
在Python中,面向对象编程(OOP)是常见的编程范式,而PyBBI项目就是基于这一理念构建的。它将数据库表视为类的实例,每个字段对应类的属性。通过这种方式,开发者可以轻松地创建与数据库交互的对象,同时这些对象...
Python supports basic arithmetic operations such as addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (**). **Strings** Strings are sequences of characters enclosed ...
basic programming skills in this book, and the basic concepts of flow control, OOP, file access, exception handling, and the like are assumed. This book may also be of use to users of earlier versions...
最后,Python的面向对象编程(OOP)特性,如类(class)和对象(object),也是其强大的功能之一。你可以定义自己的类,继承已有的类,以及实现方法和属性。例如,`class Person: def __init__(self, name, age): ...
"basic.rar_界面编程_Python_"这个标题暗示了这是一个关于Python界面编程基础的资源包,可能包含了教程、代码示例或者项目实践。下面我们将深入探讨Python中的界面编程以及与之相关的知识点。 1. **Python基础**: ...
在“basic_python_projects”这个项目中,我们可以看到一个初学者或者有一定基础的Python开发者所完成的一些基础项目。这些项目通常是学习Python编程时的典型实践,旨在帮助开发者巩固基础知识,理解Python语言的...
5. **面向对象编程**:Python的面向对象编程(OOP)与VB类似但更灵活,包括类定义、对象创建、继承、封装和多态等概念。 6. **编程思维的培养**:Python更注重编程思维的培养,如问题分解、逻辑推理和算法设计。...
7. **Object-Oriented Programming (OOP)**: While not the main focus, some introduction to OOP concepts in Python might be included to help readers write more maintainable and scalable code. 8. **Error...
1. **面向对象编程**:Python中的面向对象编程(OOP)是构建这个游戏的基础。游戏的每个元素,如棋盘、瓷砖、玩家等,都可以被设计为独立的对象,拥有自己的属性和行为。例如,棋盘对象可以包含其尺寸、当前状态等...