C#语法[1] [英] C# grammar[1]

查看:82
本文介绍了C#语法[1]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace sa
{
	public class A
	{
		public static void f()
		{
		}
		public void g()
		{
		}
		static void Main()
		{
			f();
			A a=new A();
			a.g();
		}
	}

	class OuterClass
	{
		public static int y = 100;
		public int x=23;
		public class NestedClass
		{
			int i=y;
                        public void t()
                        {
			         OuterClass b=new OuterClass();
			         int j=b.x;
                        }
		}
	}
}



为什么类A中的f()可以直接在Main()中使用,但是我们必须创建对象a才能使用g()?

相同;为什么可以在嵌套NestedClass中直接使用类OuterClass中的y,但是我们必须创建对象b才能使用x?

这两个条件相似,谁能告诉我内部机制?谢谢您

解决方案

停止发布作业问题.

您早些时候问过一个非常类似的问题,但您未能理解.

回到你的老师那里去烦他,否则你将疏远甚至更糟的是使人们丧命,带来一千个毫无意义的问题.


所有这些原因的原因,以及您以前的问题,是由于所讨论的对象/方法/成员的范围"或可见性".

尝试搜索类似 c#方法范围之类的东西.

示例 [ ^ ]是免费的.

通过这样做,您将获得的收益比在这里不断询问所获得的收益要大得多.像Code Project这样的站点实际上并未设置为提供教程.


推销写道:

为什么要类A中的()可以直接在Main()中使用,但是我们必须创建对象a才能使用g()?


这是因为f()是静态的,并且不需要对象的实例来访问它.

shilf写道:

相同;为什么类OuterClass中的y可以直接用于嵌套NestedClass中,但是我们必须创建对象b才能使用x? /blockquote>
AFAIK,内部类知道外部类变量,反之亦然.



尝试这些示例并尝试深入学习某种语言的语法是一件好事.

但是,我建议您先阅读一本书,或者尝试在msdn上进行搜索,然后再在网站上发布此类问题.


namespace sa
{
	public class A
	{
		public static void f()
		{
		}
		public void g()
		{
		}
		static void Main()
		{
			f();
			A a=new A();
			a.g();
		}
	}

	class OuterClass
	{
		public static int y = 100;
		public int x=23;
		public class NestedClass
		{
			int i=y;
                        public void t()
                        {
			         OuterClass b=new OuterClass();
			         int j=b.x;
                        }
		}
	}
}



why f() within class A can be used directly in Main(),but we must create object a to use g()?

the same;why y within class OuterClass can be used directly in nested NestedClass,but we must create object b to use x?

the two conditions are similar, who can tell me the internal mechanism?thank you

解决方案

Stop posting homework questions.

You asked a very similar thing a little earlier, and you failed to understand that.

Go back to your teacher and bother him about it, otherwise you will alienate and, worse, bore people to death, with a thousand questions whose answers are meaningless.


The reasons for all of these, and to your previous question, are because of the ''scope'' or ''visibility'' of the objects/methods/members in question.

Try searching on something like c# method scope and so on.

An example[^] for free.

You will get far more by doing that than you will from continually asking here. A site like Code Project is not really set up to give tutorials.


shilf wrote:

why f() within class A can be used directly in Main(),but we must create object a to use g()?


This is because f() is static and does not require that an instance of an object to access it.

shilf wrote:

the same;why y within class OuterClass can be used directly in nested NestedClass,but we must create object b to use x?


AFAIK, the inner class is aware of the outer class variables and not vice versa.



It is good that you are trying out such samples and trying to learn the grammar of a language in depth.

However, I would recommend that you get a book and read it or try to search on msdn before posting such questions on a website.


这篇关于C#语法[1]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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