为什么我把动态变量放入错误 [英] why i get error when put dynamic variable

查看:61
本文介绍了为什么我把动态变量放入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Base
   {
       int BAx;
       int BAy;
       public Base(int _x, int _y)
       {
           BAx = _x;
           BAy = _y;
       }
   }
   class Child : Base
   {
       int CHx;
       int CHy;
       public Child(int _x, int _y)
           : base(0, 1) // i do not get Error when the argument passed in base constructor is static, but if i change argument to dynamic like this 
//public Child(int _x, int _y) : base(int D,int E) i get compilation error
       {
           CHx = _x;
           CHy = _y;
       }
   }
   class Program
   {
       static void Main(string[] args)
       {
       }
   }

推荐答案

原则上,方法参数不能是静态。术语静态并不意味着非动态,对于成员而言,它意味着非实例,对于类型而言,不具有实例成员。您的代码中没有任何静态(静态 Main 除外),这是无关紧要的。这个问题根本就没有意义。



如果你有一行编译错误 public Child(int _x,int _y): base(int D,int E),这只是因为 D E 是没有宣布。当然,怎么回事?这里没有静态或动态的东西;你对这些术语的理解是完全错误的。请参阅我对这个问题的评论。



关于这个简单问题的基础教育,请参阅以下讨论:

是什么让静态方法可以访问? [ ^ ],

C#windows基于这个关键词及其在应用程序 [ ^ ]。



-SA
Method argument cannot be "static" in principle. The term "static" does not mean "not dynamic" it means "not instance" for members and "having no instance members" for a type. You have nothing static in your code (except static Main) which is irrelevant. The question as is makes no sense at all.

If you have compilation error with the line public Child(int _x, int _y) : base(int D,int E), this is only because D and E are not declared. Of course, how else? There is nothing about "static" or "dynamic" here; and your understanding of these terms is totally incorrect. Please see also my comment to the question.

For some basic education on this simple matter, please see these discussions:
What makes static methods accessible?[^],
C# windows base this key word related and its uses in the application[^].

—SA


这篇关于为什么我把动态变量放入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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