我如何制作密封课程? [英] How do I make a sealed class?

查看:126
本文介绍了我如何制作密封课程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码进行密封课程。但是不了解如何通过使用具有静态成员(函数,属性或索引器)的静态类扩展类来添加功能。我没有收到任何错误,但我认为这不是一个合适的密封课。



I am trying to make a sealed class from following codes. But not understanding how to add functionality to by extending the the class using a static class with static members (functions, properties, or indexers). I am not getting any errors but, I don't think it is a proper sealed class.

{
   sealed class Magic
    {
        private int toads;
        private int spiders;

        public Magic()
        {
            toads = 0;
            spiders = 0;
        }

        public int Toads
        {
            get { return toads; }
            set { toads = value; }
        }
        public int Spiders
        {
            get { return spiders; }
            set { spiders = value; }
        }
        public Magic(int t, int s)
        {
            Toads = t;
            Spiders = s;
        }
        public void doMagic()
        {
            Console.WriteLine("Poof!");

        }
      }
    static class ExtendMagic 
    {
      
    }
    class Program
    {
        static void Main()
        {
            Magic yourMagic = new Magic();
            Console.WriteLine("Spiders = {0} and Toads = {1}", yourMagic.Spiders, yourMagic.Toads);
            Magic mymagic = new Magic(3, 5);
            mymagic.doMagic();
            Console.WriteLine("Spiders = {0} and Toads = {1}", mymagic.Spiders, mymagic.Toads);
            Console.ReadKey();

        }
    }
}

推荐答案

当你使用<$ c一个类上的$ c>密封关键字,它可以防止从中派生任何类;你不能这样做:

When you use the sealed keyword on a class, it prevents any class being derived from it; you can't do this:
public sealed class A {}
public class B : A {}

它不会阻止声明扩展方法,因为它们不是从类中 派生 ,而是 扩展 它。

扩展方法不会获得对类内部的任何额外访问权限,它们只允许语法糖,因此您可以添加方法看起来像原始类的一部分。例如,DateTime类没有返回本月第一天的方法,因此您可以这样做:

It doesn't prevent extension methods being declared because they do not derive from the class, but extend it.
Extension methods do not get any additional access to the class internals, they just allow a syntactic sugar so you can add methods that look like part of the original class. For example, the DateTime class does not have a method to return the first of the month, so you can do this:

DateTime firstOfMonth;
DateTime now = DateTime.Now;
firstOfMonth = (now.AddDays(1 - now.Day)).AtMidnght();



或包括一个:


Or include one:

/// <summary>
/// Returns the first day of the month
/// </summary>
/// <example>
/// DateTime firstOfThisMonth = DateTime.Now.FirstOfMonth;
/// </example>
/// <param name="dt">Start date</param>
/// <returns></returns>
public static DateTime FirstOfMonth(this DateTime dt)
    {
    return (dt.AddDays(1 - dt.Day)).AtMidnight();
    }

现在你的代码可以使用更清晰的格式:

Now your code can use the clearer format:

DateTime firstOfMonth = DateTime.Now.FirstOfMonth();


这个问题毫无意义,看起来毫无意义。这是一个适当密封的课程。你没有证明扩展这个课程,甚至没有尝试过;你不能这样做。



-SA
The question makes no sense and looks more than pointless. This is a properly sealed class. You did not demonstrate extending this class, did not even try; and you cannot do so.

—SA


这篇关于我如何制作密封课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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