向部分类添加功能 [英] Add functionality to the partial Class

查看:65
本文介绍了向部分类添加功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为部分类魔法添加功能。我使用了与你的一个人(codeproject expert)帮助建立的相同类程序,并创建了一个部分类魔术。我是使用免费的C#书籍参考之一做的,不确定我是否正确使用了它。它虽然没有给我任何错误信息。如果有人可以看看它并指出我是否做错了什么或错过了,并帮助我纠正代码,我将非常感激。

I am trying to add functionality to the partial class magic. I used the same "class" program that I built with one of your guys(codeproject expert) help and created a partial class magic. I did it using one of the free C# book reference and not sure if I did it correctly. It didn't give me any error message though. If anyone can look look at it and point me if I did anything wrong or missing, and help me correct the code, I will really appreciate that.

namespace PartialClassMethod
{
    partial 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!");
        }

        partial void Print(int t, int s); //Defining partial method

        public void PrintPets(int t, int s)
        {
            Print(t, s);
        }
    }

    partial class Magic
    {
        partial void Print(int t, int s) //Implementing partial method
        {
            Console.WriteLine("Partial class Toads= {0}\nPartial class Spiders= {1}", t, s);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            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}\n", mymagic.Spiders, mymagic.Toads);

            Magic partialMagic = new Magic();
            partialMagic.PrintPets(10, 20);
            Console.ReadKey();
        }
    }
}

推荐答案

它看起来是正确的,并且使用部分类型会产生很多感觉并且是微不足道的,但这里的局部方法显然毫无意义。未实现的部分方法是一个相当难以理解的非常精细的特征。如果适用范围非常有限,那么它对某些东西非常有用。我在过去的答案中详细解释了这一点:关于部分方法实施 [ ^ ] 。



请充分注意,特别是后的部分;理解起来并不容易。微软的解释并不那么清楚。



简而言之:你可以轻易忘记部分方法而不会有任何降低生产力的风险,但部分类型可以大大丰富你的风格剧目。请记住:这里没有什么根本,这完全取决于代码的样式和可维护性。



-SA
It looks correct, and using partial types makes a lot of sense and is trivial, but the partial method here is clearly pointless. Non-implemented partial method is a pretty delicate feature which is relatively hard to understand. If has very limited domain of applicability, where it is really useful for something. I explained it in detail in my past answer: Regarding partial method implementation[^].

Please see it with full attention, especially the part after ; it's not so easy to understand. Microsoft explanation is not so clear.

In a nutshell: you can easily forget about partial methods without any risk to compromise your productivity, but partial types can greatly enrich your style repertoire. Remember: there is nothing fundamental here, this is all about style and maintainability of your code.

—SA


这篇关于向部分类添加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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