XNA C# 中的基本继承

发布于 2024-11-26 00:43:51 字数 197 浏览 0 评论 0原文

我正在尝试在 C# 中扩展 System.TimeSpan,因为我需要一个额外的方法将 GameTime 转换为 TimeSpan。这就是我尝试过的:

class TimeSpanner : TimeSpan
{
}

显然不可能扩展 Timespan 结构。有谁知道为什么不能延长吗?我该如何正确地完成这项工作?

I'm trying to expand System.TimeSpan in C# because I need a extra method to convert the GameTime to a TimeSpan. This is what i tried:

class TimeSpanner : TimeSpan
{
}

apperently it's not possible to extend the Timespan struct. Does anyone know why it can't be extended? And how do I properly make this work?

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

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

发布评论

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

评论(4

梦里兽 2024-12-03 00:43:52

你采取了完全错误的方法。 GameTime 具有成员属性 ElapsedGameTimeTotalGameTime,它们提供组成 GameTime 的两个不同的 TimeSpan对象。像这样使用它们:

protected override void Update(GameTime gameTime)
{
    TimeSpan elapsed = gameTime.ElapsedGameTime;
    TimeSpan total = gameTime.TotalGameTime;

    // Personally, I just do this:
    float seconds = (float)gameTime.ElapsedGameTime.TotalSeconds;

    // ...
}

You're taking completely the wrong approach. GameTime has member properties ElapsedGameTime and TotalGameTime that provide the two different TimeSpans that make up a GameTime object. Use them like this:

protected override void Update(GameTime gameTime)
{
    TimeSpan elapsed = gameTime.ElapsedGameTime;
    TimeSpan total = gameTime.TotalGameTime;

    // Personally, I just do this:
    float seconds = (float)gameTime.ElapsedGameTime.TotalSeconds;

    // ...
}
喜你已久 2024-12-03 00:43:52

您不能从struct继承。

结构 上形成 MSDN:

结构没有类的继承。结构不能从另一个结构或类继承,也不能是类的基类。然而,结构体继承自基类 Object。结构可以实现接口,并且它的作用与类完全一样。

作为您尝试的替代方法,请考虑使用扩展方法

You can not inherit from a struct.

Form MSDN on structs:

There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class Object. A struct can implement interfaces, and it does that exactly as classes do.

As an alternative to what you are trying, consider using extension methods.

内心荒芜 2024-12-03 00:43:52

TimeSpan 是一个struct,并且结构(属于值类型)不能被子类化。您可能应该研究使用运算符来执行转换。

你可以这样做:

public static implicit operator TimeSpan(GameTime gt) 
{
    return gt.GetTimeSpan();
}

然后你可以这样使用它:

TimeSpan timeSpan = (TimeSpan)myGameTime;

A TimeSpan is a struct and structs (which are a value type) cannot be subclassed. You should probably investigate using an operator to perform a conversion.

You could do something like this:

public static implicit operator TimeSpan(GameTime gt) 
{
    return gt.GetTimeSpan();
}

You could then use this like this:

TimeSpan timeSpan = (TimeSpan)myGameTime;
踏月而来 2024-12-03 00:43:52

如果您需要一个额外的方法来进行转换,那么为什么不直接创建一个扩展方法呢?

public static void DoStuff(this TimeSpan timeSpan, GameTime gameTime)
{
     ...
}

If you need an extra method to do a conversion then why not just make an extension method then?

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