静态成员变量,例如OOP语言 [英] Static member variable such as OOP langage

查看:60
本文介绍了静态成员变量,例如OOP语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Go很陌生,然后我搜索了很多关于如何使用静态函数/变量(例如C#)创建静态类的示例.但是,我找不到任何能很好回答的问题.也许这个问题看起来很愚蠢,但是当我不确定或当我完全不了解某些内容时,我不喜欢它.

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.

假设我们有以下代码:

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类似于C,因此它是否具有上述行为,例如C ++/C#语言?我上面的代码可以用C ++/C#来实现,还是可以通过以C语言传递(使用C模块化编程方式)来实现?

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)?

推荐答案

Go中没有继承,
但是您可以用Golang方式完成所有OOP任务.

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

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

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:C#类中的静态变量=> Golang包中的全局变量
2:C#=>带有枚举名称和枚举元素的const类型的新程序包中的枚举
3:OOP中的类=>结构类型
4:类方法=>具有接收方方法的结构
5:C#/Java抽象方法(C ++中的纯虚函数)=> io.Reader之类的接口方法
6:public =>首字母大写名称
7:私人=>首字母小写名称
8:名称空间=>包名称
9:继承=>嵌入式结构和嵌入式接口
10:线程=>转到例程
11:锁定=> sync.Mutex
...

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
...

这篇关于静态成员变量,例如OOP语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆