private-constructor

Scala case class private constructor but public apply method

流过昼夜 提交于 2019-11-29 22:55:43
If I have the following case class with a private constructor and I can not access the apply-method in the companion object. case class Meter private (m: Int) val m = Meter(10) // constructor Meter in class Meter cannot be accessed... Is there a way to use a case class with a private constructor but keep the generated apply-method in the companion public? I am aware that there is no difference (in my example) between the two options: val m1 = new Meter(10) val m2 = Meter(10) but I want to forbid the first option. -- edit -- Surprisingly the following works (but is not really what i want): val

Difference between java enum with no values and utility class with private constructor

ε祈祈猫儿з 提交于 2019-11-29 10:40:19
问题 A common thing to do to utility classes is to give them a private constructor: public final class UtilClass { private UtilClass() {} ... } But unfortunately, some tools don't like that private constructor. They may warn that it's never called within the class, that it's not covered by tests, that the block doesn't contain a comment, etc. A lot of those warnings go away if you do this instead: public enum UtilClass {; ... } My question is: besides the unending hatred of future developers, what

private constructor [duplicate]

天涯浪子 提交于 2019-11-28 15:25:33
Possible Duplicate: What is the use of making constructor private in a class? Where do we need private constructor? How can we instantiate a class having private constructor? wkl Private constructor means a user cannot directly instantiate a class. Instead, you can create objects using something like the Named Constructor Idiom , where you have static class functions that can create and return instances of a class. The Named Constructor Idiom is for more intuitive usage of a class. The example provided at the C++ FAQ is for a class that can be used to represent multiple coordinate systems.

How to instantiate an object with a private constructor in C#?

余生长醉 提交于 2019-11-28 08:55:26
I definitely remember seeing somewhere an example of doing so using reflection or something. It was something that had to do with SqlParameterCollection which is not creatable by a user (if I'm not mistaken). Unfortunately cannot find it any longer. Can anyone please share this trick here? Not that I consider it a valid approach in development, I'm just very interested in the possibility of doing this. // the types of the constructor parameters, in order // use an empty Type[] array if the constructor takes no parameters Type[] paramTypes = new Type[] { typeof(string), typeof(int) }; // the

private constructor [duplicate]

风格不统一 提交于 2019-11-27 09:12:48
问题 Possible Duplicate: What is the use of making constructor private in a class? Where do we need private constructor? How can we instantiate a class having private constructor? 回答1: Private constructor means a user cannot directly instantiate a class. Instead, you can create objects using something like the Named Constructor Idiom, where you have static class functions that can create and return instances of a class. The Named Constructor Idiom is for more intuitive usage of a class. The