python 调用内部类的两种方法


class Car:#外部类
    class Door:#内部类
        def open(self):
            print('open door')
        
    class Wheel:
        def run(self):
            print('car run')
            
if __name__=="__main__":
    car=Car()#实例化外部类
    backDoor=Car.Door()#实例化内部类 第一种方法

    frontDoor=car.Door()#因为car已经实例化外部类,再次实例化Car的内部类 第二种方法
    backDoor.open()
    frontDoor.open()
    wheel=car.Wheel()#car已经实例化外部类,Wheel()再次实例化内部类
    wheel.run()#调用内部类的方法

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM