duanpin2034 2016-05-29 13:57
浏览 312
已采纳

Go-静态成员变量,例如OOP语言

I'm pretty new with Go and then I searched a lot how to have a static class with static function/variables such as C# by example. But, I couldn't find anything which answered well about it. Maybe this question seems stupid, but I don't like either when I'm not sure or when I don't understand completely something.

Let say we have this code:

public class Program
{
    public static string name = "Program tester.";

    public enum Importance
    {
        None,
        Trivial,
        Regular,
        Important,
        Critical
    };

    public static void tester(Importance value)
    {
        // ... Test against known Importance values.
        if (value == Importance.Trivial)
        {
            Console.WriteLine("Not true");
        }
        else if (value == Importance.Critical)
        {
            Console.WriteLine("True");
        }
    }
}

Golang is a C-like if I understand, so does it have some behavior like this one above, such as C++/C# languages? My code above can be achieved as C++/C# or the way to do it is to passing by a language as C (using the C modular programming way)?

  • 写回答

2条回答 默认 最新

  • doutan8601 2016-06-05 14:22
    关注

    There is no inheritance in Go,
    but you can do all OOP stuff in Golang way.

    also see:
    https://github.com/luciotato/golang-notes/blob/master/OOP.md https://www.goinggo.net/2013/07/object-oriented-programming-in-go.html

    1: static var in C# class => global var in Golang package
    2: enum in C# => new package with enum name and const type of enum elements
    3: class in OOP => struct type
    4: class methods => struct with receiver methods
    5: C#/Java abstract methods(pure virtual functions in C++) => interface methods like io.Reader
    6: public => first letter Upper case Name
    7: private => first letter lower case name
    8: namespace => package name
    9: inheritance => embedded struct and embedded interface
    10: Thread => Go routines
    11: lock => sync.Mutex
    ...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1