instantiation

Does System.Activator.CreateInstance(T) have performance issues big enough to discourage us from using it casually?

旧巷老猫 提交于 2019-11-26 19:57:52
问题 Does System.Activator.CreateInstance(T) method have performance issues (since I'm suspecting it uses reflection) big enough to discourage us from using it casually? 回答1: As always, the only correct way to answer a question about performance is to actually measure the code. Here's a sample LINQPad program that tests: Activator.CreateInstance new T() calling a delegate that calls new T() As always, take the performance program with a grain of salt, there might be bugs here that skews the

Create Annotation instance with defaults, in Java

久未见 提交于 2019-11-26 19:42:36
问题 How can I create an instance of the following annotation (with all fields set to their default value). @Retention( RetentionPolicy.RUNTIME ) public @interface Settings { String a() default "AAA"; String b() default "BBB"; String c() default "CCC"; } I tried new Settings() , but that does not seem to work... 回答1: To create an instance you need to create a class that implements: java.lang.annotation.Annotation and the annotation you want to "simulate" For example: public class MySettings

Avoid instantiating a class in java

☆樱花仙子☆ 提交于 2019-11-26 19:25:17
问题 Recently I've faced a question : How to avoid instantiating a Java class? However, I answered by saying: If you don't want to instantiate a class, use "abstract" modifier. Ex: javax.servlet.HttpServlet, is declared as abstract(though none of its methods are abstract) to avoid instantiation. Declare a no argument private constructor. Now my question is a) are there any other ways? b) why does any one do not want to instantiate a class? - after searching in SO, I got to know from this that Util

Swift: Creating an Array with a Default Value of distinct object instances

ⅰ亾dé卋堺 提交于 2019-11-26 13:46:50
I noticed a bit weird ( and dangerous IMHO ) behavoir in Creating an Array with a Default Value . As stated in Swift 2.1: Collection Types Swift’s Array type also provides an initializer for creating an array of a certain size with all of its values set to the same default value. You pass this initializer the number of items to be added to the new array (called count) and a default value of the appropriate type (called repeatedValue): The point is: same default value ; in order to understand how it work, I tried to create an array of elements of this example class class User { private struct

C++ Object Instantiation

梦想与她 提交于 2019-11-26 12:49:32
问题 I\'m a C programmer trying to understand C++. Many tutorials demonstrate object instantiation using a snippet such as: Dog* sparky = new Dog(); which implies that later on you\'ll do: delete sparky; which makes sense. Now, in the case when dynamic memory allocation is unnecessary, is there any reason to use the above instead of Dog sparky; and let the destructor be called once sparky goes out of scope? Thanks! 回答1: On the contrary, you should always prefer stack allocations, to the extent

base() and this() constructors best practices

て烟熏妆下的殇ゞ 提交于 2019-11-26 12:35:57
问题 Under what conditions am I supposed to make the :base() and :this() constructor calls following my constructor\'s parentheses (or even in other places in the code). When are these calls good practices and when are they mandatory? 回答1: : base(...) If you omit the call to a base constructor it will call the default base constructor automatically. It is mandatory to call a base constructor explicitly if there is no default constructor. Even if there is a default constructor you may still wish to

Is there a way to instantiate a class without calling __init__?

时光怂恿深爱的人放手 提交于 2019-11-26 12:10:13
问题 Is there a way to circumvent the constructor __init__ of a class in python? Example: class A(object): def __init__(self): print \"FAILURE\" def Print(self): print \"YEHAA\" Now I would like to create an instance of A . It could look like this, however this syntax is not correct. a = A a.Print() EDIT: An even more complex example: Suppose I have an object C , which purpose it is to store one single parameter and do some computations with it. The parameter, however, is not passed as such but it

Determine if a type is static

梦想与她 提交于 2019-11-26 11:20:35
问题 Let\'s say I have a Type called type . I want to determine if I can do this with my type (without actually doing this to each type): If type is System.Windows.Point then I could do this: Point point1 = new Point(); However if type is System.Environment then this will not fly: Environment environment1 = new Environment(); //wrong So if I am iterating through every visible type in an assembly how do I skip all the types that will fail to create an instance like the second one? I\'m kind of new

Instance member cannot be used on type

巧了我就是萌 提交于 2019-11-26 11:00:56
问题 I have the following class: class ReportView: NSView { var categoriesPerPage = [[Int]]() var numPages: Int = { return categoriesPerPage.count } } Compilation fails with the message: Instance member \'categoriesPerPage\' cannot be used on type \'ReportView\' What does this mean? 回答1: You just have syntax error when saying = {return self.someValue} . The = isn't needed. Use : var numPages: Int { get{ return categoriesPerPage.count } } if you want get only you can write var numPages: Int {

Why is [] faster than list()?

强颜欢笑 提交于 2019-11-26 10:58:13
I recently compared the processing speeds of [] and list() and was surprised to discover that [] runs more than three times faster than list() . I ran the same test with {} and dict() and the results were practically identical: [] and {} both took around 0.128sec / million cycles, while list() and dict() took roughly 0.428sec / million cycles each. Why is this? Do [] and {} (and probably () and '' , too) immediately pass back a copies of some empty stock literal while their explicitly-named counterparts ( list() , dict() , tuple() , str() ) fully go about creating an object, whether or not