类装饰器和继承类之间的区别 [英] Difference between class decorator and Inheriting class
问题描述
我正在学习NUnit,发现我们在每个测试函数之前使用 [TestMethod]
之类的类修饰符。
I was learning about NUnit and found we use class decorators like [TestMethod]
before every test function.
然后我在此链接中了解了装饰器。根据这个答案
Then i learnt about decorators at this link. According to this answer
在C#中添加装饰器时,就像在 class / method 中添加属性一样。
我只是想问一下我是否要使用整个类中使用的属性(不是在谈论一种方法)那我为什么不选择继承于类装饰器
I just want to ask if I want to use property to be used in whole class (not talking about one method) then why don't I choose inheritance over class decorator
有什么区别...
推荐答案
请小心,因为您提供的链接使用装饰器一词来指2个完全不同的概念。该链接中的问题是在问这个问题:
Be careful because the link you provided is using the word Decorator to refer to 2 entirely different concepts. The question in that link is asking this question:
装饰类或参数是什么意思?
What does it mean to Decorate a class or parameter?
一些答案,请正确回答问题,特别是此和此。两者都在谈论装饰器模式。
Some answers, answer the question correctly specifically this and this. Both of these are talking about the Decorator Pattern.
但是,此答案是在谈论完全不同的东西。尽管 decorator 可以用来表示属性(在C#中带有方括号),但是SO线程正在使用它来表示 Decorator Pattern >和属性。
However, this answer is talking about something entirely different. Although decorator can be used to mean Attributes (the ones with square brackets in C#) but that SO thread is using it to mean both the Decorator Pattern and Attributes.
属性
属性在此处中进行解释。
装饰图案
装饰图案的说明此处。
装饰器模式与继承
有一个很好的比较这里。
这篇关于类装饰器和继承类之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!