zoukankan      html  css  js  c++  java
  • LUA中点号和冒号的区别

    Student = {};
    Student.__index = Student;
    
    function Student:new(name, age)
        local temp = {};
        setmetatable(temp, Student);
        temp.name = name;
        temp.age = age;
        return temp;
    end
    
    
    function Student:info()
        print(self.name, self.age);
    --运行stu2时会报错 -- print("name:" .. self.name .. " age:" .. self.age); end --输出:stu1 10 stu1 = Student.new(nil, "stu1", 10); stu1:info(); --输出:10 nil stu2 = Student.new("stu2", 10); stu2:info(); --输出:nil stu3 stu3 = Student:new(nil, "stu3", 10); stu3:info(); --输出:stu4 10 stu4 = Student:new("stu4", 10); stu4:info();
    Student = {};
    Student.__index = Student;
    
    --此处做修改
    function Student.new(name, age)
        local temp = {};
        setmetatable(temp, Student);
        temp.name = name;
        temp.age = age;
        return temp;
    end
    
    
    function Student:info()
        print(self.name, self.age);
    end
    
    --输出:nil    stu1
    stu1 = Student.new(nil, "stu1", 10);    
    stu1:info();
    
    --输出:stu2    10
    stu2 = Student.new("stu2", 10);    
    stu2:info();
    
    --输出:table: 0037B200    nil
    stu3 = Student:new(nil, "stu3", 10);    
    stu3:info();
    
    --输出:table: 0037B200    stu4
    stu4 = Student:new("stu4", 10);    
    stu4:info();
    
    --输出:table: 0084B200    stu5
    stu5 = Student:new("stu5");    
    stu5:info();

    --:stu6    10
    stu6 = Student:new("stu6", 10);    
    stu6.info(stu6);
    Student = {};
    Student.__index = Student;
    
    function Student.new(name, age)
        local temp = {};
        setmetatable(temp, Student);
        temp.name = name;
        temp.age = age;
        return temp;
    end
    
    
    function Student:info()
        print(self.name, self.age);
    end
    
    function Student:message()
        print(self.name, self.age);
    end
    
    function Student.school()
        print("go to school!!!");
    end
    
    --声明类时用点号:Student.new(name, age)
    stu = Student:new("zhangsan", 10);
    stu:info();    --输出信息错误
    stu.school();
    --stu.message(); --报错
    stu.message(stu);--输出信息错误
    
    mes = Student.new("zhangsan", 10);
    --mes.info();    --报错
    mes.info(mes);
    mes.school();
    
    
    
    --声明类时用冒号:Student.new(name, age)
    --stu = Student:new("zhangsan", 10);
    --stu:info();
    --stu.info(stu);
    --stu.school();
    
    --mes = Student.new(nil, "mes", 20);
    --mes:message();
    --mes.school();
  • 相关阅读:
    Windows设置多用户同时远程登录
    Scala配置环境变量windows
    Java学习|强引用,软引用,弱引用,幻想引用有什么区别?
    Java学习|Exception和Error有什么区别?
    关于异常处理的几点建议
    win Server 2008 笔记
    .Net 初步学习笔记之三---变量
    认识与入门 MarkDown 标记语言
    C# winform基础 1、Timer不起作用 2、 设置图片透明
    IIS启动失败,启动Windows Process Activation Service时,出现错误13:数据无效 ;HTTP 错误 401.2
  • 原文地址:https://www.cnblogs.com/JimLy-BUG/p/5548730.html
Copyright © 2011-2022 走看看