Python 简明教程
Python - Inner Classes
Inner Class in Python
在 Python 中,定义在另一个类内部的类被称为 inner class 。有时,内部类也称为嵌套类。如果内部类已实例化,则外层类也可以使用内部类的对象。内部类的对象成为外层类的属性之一。内部类自动继承外层类的属性,而无需正式建立继承关系。
A class defined inside another class is known as an inner class in Python. Sometimes inner class is also called nested class. If the inner class is instantiated, the object of inner class can also be used by the parent class. Object of inner class becomes one of the attributes of the outer class. Inner class automatically inherits the attributes of the outer class without formally establishing inheritance.
Syntax
class outer:
def __init__(self):
pass
class inner:
def __init__(self):
pass
使用内部类可以对类进行分组。嵌套类的优点之一就是可以轻而易举地搞清楚哪些类是相关的。内部类的作用域为局部作用域。它作为外部类的一个属性。
An inner class lets you group classes. One of the advantages of nesting classes is that it becomes easy to understand which classes are related. The inner class has a local scope. It acts as one of the attributes of the outer class.
Example
在以下代码中,我们有 student 作为外部类,subjects 作为内部类。student 的 init () 构造器初始化 name 属性和 subjects 类的实例。另一方面,内部类 subjects 的构造器初始化两个实例变量 sub1、sub2。
In the following code, we have student as the outer class and subjects as the inner class. The init() constructor of student initializes name attribute and an instance of subjects class. On the other hand, the constructor of inner subjects class initializes two instance variables sub1, sub2.
外部类的 show() 方法调用内部类的方法,并传递已实例化的对象。
A show() method of outer class calls the method of inner class with the object that has been instantiated.
class student:
def __init__(self):
self.name = "Ashish"
self.subs = self.subjects()
return
def show(self):
print ("Name:", self.name)
self.subs.display()
class subjects:
def __init__(self):
self.sub1 = "Phy"
self.sub2 = "Che"
return
def display(self):
print ("Subjects:",self.sub1, self.sub2)
s1 = student()
s1.show()
执行此代码时,它将生成以下 output -
When you execute this code, it will produce the following output −
Name: Ashish
Subjects: Phy Che
完全有可能会独立地声明一个外部类的对象,并使用其自己的 display() 方法。
It is quite possible to declare an object of outer class independently, and make it call its own display() method.
sub = student().subjects().display()
它会列出科目。
It will list out the subjects.
Types of Inner Class
在 Python 中,内部类分为两种类型 −
In Python, inner classes are of two types −
-
Multiple Inner Class
-
Multilevel Inner Class
Multiple Inner Class
在多个内部类中,一个外层类包含多个内部类。每个内部类都独立工作,但可以与外层类的成员进行交互。
In multiple inner class, a single outer class contains more than one inner class. Each inner class works independently but it can interact with the members of outer class.
Example
在下面的示例中,我们创建了一个名为 Organization 的外层类和两个内部类。
In the below example, we have created an outer class named Organization and two inner classes.
class Organization:
def __init__(self):
self.inner1 = self.Department1()
self.inner2 = self.Department2()
def showName(self):
print("Organization Name: Tutorials Point")
class Department1:
def displayDepartment1(self):
print("In Department 1")
class Department2:
def displayDepartment2(self):
print("In Department 2")
# instance of OuterClass
outer = Organization()
# Calling show method
outer.showName()
# InnerClass instance 1
inner1 = outer.inner1
# Calling display method
inner1.displayDepartment1()
# InnerClass instance 2
inner2 = outer.inner2
# Calling display method
inner2.displayDepartment2()
执行后,此代码将会产生以下 output −
On executing, this code will produce the following output −
Organization Name: Tutorials Point
In Department 1
In Department 2
Multilevel Inner Class
它指的是一个内部类,该内部类本身又包含另一个内部类。它创建了嵌套类的多个级别。
It refers to an inner class that itself contains another inner class. It creates multiple levels of nested classes.
Example
以下代码说明了多层内部类在 Python 中的工作原理 −
The following code explains the working of Multilevel Inner Class in Python −
class Organization:
def __init__(self):
self.inner = self.Department()
def showName(self):
print("Organization Name: Tutorials Point")
class Department:
def __init__(self):
self.innerTeam = self.Team1()
def displayDep(self):
print("In Department")
class Team1:
def displayTeam(self):
print("Team 1 of the department")
# instance of outer class
outer = Organization()
# call the method of outer class
outer.showName()
# Inner Class instance
inner = outer.inner
inner.displayDep()
# Access Team1 instance
innerTeam = inner.innerTeam
# Calling display method
innerTeam.displayTeam()
当您运行上述代码时,它将生成以下 output −
When you run the above code, it will produce the below output −
Organization Name: Tutorials Point
In Department
Team 1 of the department