Python 面向对象编程:类、对象、继承和构造函数

OOP 中 Python
OOP 中 Python 是一种与其他通用编程语言一样注重使用对象和类的编程方法。对象可以是任何现实世界的实体。 Python 允许开发人员使用 OOP 方法开发应用程序,主要关注代码可重用性。在 Python.
什么是类?
一堂课 Python 是一个逻辑组ping 它支持数据和函数。它允许用户创建包含任意内容的数据结构,从而方便访问。
例如,任何想要在线获取客户详细信息的银行员工都可以访问 客户等级其中将列出其所有属性,如交易详情、取款和存款详情、未偿债务等。
如何定义 Python 类
要定义类,你需要考虑以下几点
步骤1) In Python,类由 "Class" 关键词
class myClass():
步骤2) 在类内部,你可以定义属于该类的函数或方法
def method1 (self): print "Guru99" def method2 (self,someString): print "Software Testing:" + someString
- Here we have defined method1 that prints "Guru99“。
- Another method we have defined is method2 that prints "Software Testing"+ SomeString. SomeString is the variable supplied by the calling method
步骤3) 类中的所有内容都缩进,就像函数、循环、if 语句等中的代码一样。任何未缩进的内容都不属于类
注意: About using "self" in Python
- `self` 参数指的是对象本身,因此使用了 `self` 这个词。所以在这个方法内部,`self` 指的是正在被操作的这个对象的特定实例。
- Self 是 Pythons 表示实例方法的第一个参数 Python。 它是一部分 Python 访问对象成员的语法
步骤4) 创建类的对象
c = myClass()
步骤5) 调用类中的方法
c.method1()
c.method2(" Testing is fun")
- 请注意,当我们调用方法1或方法2时,无需提供self关键字。这由程序自动处理。 Python 运行。
- Python runtime will pass "self" value when you call an instance method on in instance, whether you provide it deliberately or not
- 你只需要关心非自我论点
步骤6) 以下是完整的代码
# Example file for working with classes
class myClass():
def method1(self):
print("Guru99")
def method2(self,someString):
print("Software Testing:" + someString)
def main():
# exercise the class methods
c = myClass ()
c.method1()
c.method2(" Testing is fun")
if __name__== "__main__":
main()
继承如何运作
继承是面向对象编程中使用的一个特性;它指的是定义一个新类,而对现有类的修改很少或没有修改。新类称为 派生类 它继承自一个叫做 基地. Python 支持继承;它还支持 多重继承。一个类可以从另一个类继承属性和行为方法,另一个类称为子类或继承者类。
Python 继承语法
class DerivedClass(BaseClass):
body_of_derived_class
步骤1) 运行以下代码
# Example file for working with classes
class myClass():
def method1(self):
print("Guru99")
class childClass(myClass):
#def method1(self):
#myClass.method1(self);
#print ("childClass Method1")
def method2(self):
print("childClass method2")
def main():
# exercise the class methods
c2 = childClass()
c2.method1()
#c2.method2()
if __name__== "__main__":
main()
Notice that the in childClass, method1 is not defined but it is derived from the parent myClass. The output is "Guru99“。
步骤2) 取消注释第 8 行和第 10 行。运行代码
Now, the method 1 is defined in the childClass and output "childClass Method1" is correctly shown.
步骤3) 取消注释第 9 行。运行代码
您可以使用以下语法调用父类的方法
ParentClassName.MethodName(self)
在我们的例子中,我们调用 myClass.method1(self) 和 Guru99 按预期打印出来。
第四步)取消注释第19行。运行代码。
Method 2 of the child class is called and "childClass method2" is printed as expected.
Python 构造函数
构造函数是一个将对象实例化为预定义值的类函数。
它以双下划线 (_) 开头。它是 __init__() 方法
在下面的例子中,我们使用构造函数获取用户的名称。
class User:
name = ""
def __init__(self, name):
self.name = name
def sayHello(self):
print("Welcome to Guru99, " + self.name)
User1 = User("Alex")
User1.sayHello()
输出将是:
歡迎來到 Guru99,亚历克斯
Python 2示例
以上代码是 Python 3 个例子,如果你想跑步 Python 2 请考虑以下代码。
# How to define Python classes
# Example file for working with classes
class myClass():
def method1(self):
print "Guru99"
def method2(self,someString):
print "Software Testing:" + someString
def main():
# exercise the class methods
c = myClass ()
c.method1()
c.method2(" Testing is fun")
if __name__== "__main__":
main()
#How Inheritance works
# Example file for working with classes
class myClass():
def method1(self):
print "Guru99"
class childClass(myClass):
#def method1(self):
#myClass.method1(self);
#print "childClass Method1"
def method2(self):
print "childClass method2"
def main():
# exercise the class methods
c2 = childClass()
c2.method1()
#c2.method2()
if __name__== "__main__":
main()
学习我们的下一个教程 多态性 Python


