Starfruit Python Basic Tutorial-Chapter 8: Python Classes and Objects (7) Importing External Modules

My CSDN blog column: https://blog.csdn.net/yty_7
Github address: https://github.com/yot777/Python-Primary-Learning

 

8.9 Import external modules

Module ( Module1 ): for logically organized (a function to achieve) the Python codes (variable, function, class), is essentially * . Py file.

Package ( package ): defines a Python application execution environment composed of modules and subpackages , which is essentially a hierarchical file directory structure.

 

The role of import is to import external Python modules or packages into the program. Commonly used to import modules developed by external third parties.

The syntax is as follows:

# Import a module

import model_name

# Import plurality of modules

import module_name1,module_name2

# Import module specified properties, methods (without parentheses), class

 from module_name import module_element [as new_name]

 

Note Python and Java 's import difference:

Python in import when the module is executed all the code module, comprising generating an object instance.

Java in import class, class code is not executed, the class is not loaded into memory.

 

Examples of importing external objects from Python :

#Base_A.py文件如下:
class Base:
    def __init__(self):
        print('This is Base init function')
#A单继承自Base类
class A(Base):
    def __init__(self):
        Base.__init__(self)
    def printA(self):
        print('This is init function of A')
#实例化A的对象a
a=A()
a.printA()

#B.py文件如下:
import Base_A

class B:
    def printB(self):
        print('This is init function of B')

#实例化B的对象b
b=B()
b.printB()

B.py的运行结果:
This is Base init function
This is init function of A
This is init function of B
(B.py在运行的时候直接将Base_A.py也运行了)

Examples of importing external objects in Java :

Java program structure diagram :

//Base.java
package com.yty.test5;

public class Base {
  public void Base(){
    System.out.println("This is Base init function");
  }
}


//A.java
package com.yty.test5;

public class A extends Base{
  public void A() {
    //调用父类Base的构造函数
    super.Base();
    System.out.println("This is init function of A");
  }
}


//B.java
package com.yty.test6;

import com.yty.test5.A;

public class B {
  public void B(){
    System.out.println("This is init function of B");      
  }
  public static void main(String[] args) {
    //导入了A类之后,必须实例化一个A类的对象a,才能调用A类的方法
    A a = new A();
    a.A();
    B b = new B();
    b.B();
  }
}

B.java的运行结果:
This is Base init function
This is init function of A
This is init function of B

Reference tutorial:

Liao Xuefeng's Python tutorial

https://www.liaoxuefeng.com/wiki/1016959663602400

Liao Xuefeng's Java tutorial

https://www.liaoxuefeng.com/wiki/1252599548343744

Python3 Tutorial | Rookie Tutorial
https://www.runoob.com/python3/
 

If you think this chapter is helpful to you, welcome to follow, comment and like! Github welcomes your Follow and Star!
 

Published 55 original articles · won praise 16 · views 6111

Guess you like

Origin blog.csdn.net/yty_7/article/details/104234966