C#“非静态字段,方法或属性需要对象引用"

编程入门 行业动态 更新时间:2024-10-25 12:25:11
本文介绍了C#“非静态字段,方法或属性需要对象引用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

上个星期我也遇到过同样的问题,只是父母班的教he:

Having the same issue i was last week only with inheiriting from the parent class:

public ExtendedTime(int Hour, int Minute, String TimeZone) :base(hour, minute) { timeZone = TimeZone; }//end of ExtendedTime

:基准(小时,分钟)是我遇到此错误的地方.在小时和分钟都说相同的问题.现在通常我会说我只是在缺少某种属性,但是我尝试了一下,但可悲的是它没有任何好处.在父类中的小时和分钟声明为:

:base(hour,minute) is where i have this error. Says the same problem for both hour and minute. Now usually I would say that i'm missing something far as a property but i tried that and it didn't do any good sadly. in the parent class hour and minute are declared as following:

internal int hour; internal int minute;

我有设置者和获取者的设置.

And i have setters and getters setup.

推荐答案

您尝试在字段 hour 和 minute 时使用您可能打算使用构造函数参数.调用基类构造函数时,不能使用字段(或任何其他实例成员).

You're trying to use the fields hour and minute when you probably meant to use the constructor parameters. You can't use fields (or any other instance members) when calling a base class constructor.

我个人会更改构造函数参数以使用更常规的名称:

Personally I'd change the constructor parameters to have more conventional names:

public ExtendedTime(int hour, int minute, String timeZone) : base(hour, minute) { this.timeZone = timeZone; }

请注意,如果您将字段设为 private 而不是内部字段,则问题将更加明显,因为您一开始将无法访问这些字段:)

Note that if you made your fields private instead of internal, the issue would have been more obvious, as you wouldn't have access to the fields in the first place :)

更多推荐

C#“非静态字段,方法或属性需要对象引用"

本文发布于:2023-11-07 17:23:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567024.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   静态   属性   对象   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!