一些逻辑 OOP 概念

发布于 2024-09-30 14:05:23 字数 265 浏览 4 评论 0原文

我有一个问题。

class A
{
    static void m1()
    {
       int x=10;
    }
}

class B
{
    // if i want to access the variable x in b class how can i access it 

    A a = new A();
    // a. what should i write here to access x variable
}

I have a question.

class A
{
    static void m1()
    {
       int x=10;
    }
}

class B
{
    // if i want to access the variable x in b class how can i access it 

    A a = new A();
    // a. what should i write here to access x variable
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤独岁月 2024-10-07 14:05:23

为了访问 x ,您必须将其设置为 A 上的字段:

class A
{
    public int X;
}
class B
{
    static void Main()
    {
        A a = new A();
        a.X = 17;
    }
}

但是,从类中公开公共字段通常是不好的做法 - 最好将该字段包装在封装它的属性:

class A
{
    int _x;
    public int X
    {
        get { return _x; }
        set { _x = value; }
    }
}

如果这个语法看起来很麻烦,你可以稍微简化一下。 C# 有一个名为“自动实现属性”的功能,如果您执行以下操作,编译器将为您生成上面的代码:

class A
{
    public int X { get; set; }
}

In order to access x you must make it a field on A:

class A
{
    public int X;
}
class B
{
    static void Main()
    {
        A a = new A();
        a.X = 17;
    }
}

However it is generally bad practice to expose public fields from a class - it is better to wrap the field in a property to encapsulate it:

class A
{
    int _x;
    public int X
    {
        get { return _x; }
        set { _x = value; }
    }
}

If this syntax seems cumbersome you can simplify it a bit. C# has a feature called automatically implemented properties in which the compiler will generate the code above for you if you do this:

class A
{
    public int X { get; set; }
}
一梦等七年七年为一梦 2024-10-07 14:05:23

它应该是属性或公共变量。

It should be either property or a public variable.

一抹微笑 2024-10-07 14:05:23

首先,提出问题时,请花 30 秒的时间正确。你的代码就是废话。

call 不是 C# 中的有效关键字。您指的是class吗?或者其他什么?其次,要求您通过拼写检查器运行文本是否不合理?我们没有人会因为回答您的问题而获得报酬,我们是在自己的空闲时间免费做的。因此,如果您想要答案,请让我们轻松理解并回答您的问题。不要因为我们的懒惰而付出代价,因为那样我们也会偷懒,并忽略你的问题。

现在,据我了解你的问题,你不能。 x 是在函数内部声明的局部变量。在其他地方是看不到的。

First, when asking a question, please spend just 30 seconds trying to get it right. Your code is nonsense.

call isn't a valid keyword in C#. Did you mean class? Or something else? Second, is it unreasonable to ask you to run the text through a spell checker? None of us are getting paid to answer your questions, we're doing it for free, in our own free time. So if you want answers, make it easy for us to understand and answer your questions. Don't be lazy at our expense, because then we'll be lazy too, and ignore your question.

Now, as I understand your question, you can't. x is a local variable declared inside the function. It's not visible anywhere else.

不顾 2024-10-07 14:05:23
    class Class1
    {
        public int x;

        public void M1()
        {
            x = 10;
        }

    }

class ClassB
{

void Method()
{
    Class1 a = new Class1();
    a.M1();
    a.x = 5;
    //at this point the x will contain 5
}
}

exmaple 使用实例变量,而不是静态变量。

要访问静态变量,您必须有一个静态方法 M1,然后在 ClassB 中您可以使用类名而不是对象名来访问 x 变量,如下所示:

Class1.x = 5;

变量 x1 也必须声明为静态,如: public static x = 10;

    class Class1
    {
        public int x;

        public void M1()
        {
            x = 10;
        }

    }

class ClassB
{

void Method()
{
    Class1 a = new Class1();
    a.M1();
    a.x = 5;
    //at this point the x will contain 5
}
}

The exmaple uses instance variables , not static.

To access static variables you must have a static method M1 then in ClassB you access the x variable usign the class name not the object name, like this:

Class1.x = 5;

Variable x1 must also be declared as static like: public static x = 10;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文