Lua advanced 6--using the Require function for file calls __ functions

Source: Internet
Author: User
Tags lua require

Since it is not possible to put all the code in a file in programming, it is certain that some of the code will be open, so it is not difficult to make a file call in those high-level languages such as c++,c#. So in Lua,

These can be achieved as well. The Require function is used primarily. Here is the code to tell you.

The file being called Berequired.lua

--Private Functions
local function myprivatefunction ()
  print ("This is a private function!")
End
--common interface
function vprint ()
  myprivatefunction ()
  print ("This was a public function!")
  Print ("This is a required package!")
End
Vprint ()
--Identify the class name to create a class
complex = {Vprint = Vprint}


Calling files from other files Main.lua

Package.path = Package.path. ";?. Lua "     //template-style path where the local
Requiredpackage = require (" berequired ") is obtained by the Require function, and  this line is required for
print (Package.path)
Print ("\ n")
print (requiredpackage)
function vprint ()
print ("Main print!")
End
Vprint ()
local Requirecomplex = {}  //Declare an object
Requirecomplex = Complex  //Create the object
Requirecomplex. Vprint ()

Output results





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.