constructor

Scala: Constructor taking either Seq or varargs

旧街凉风 提交于 2019-12-18 14:08:16
问题 I am guessing that, for compatibility reasons, the type of vararg parameters Any* is Array[Any] - please correct this if I'm wrong. However, this does not explain the following error: class Api(api_url: String, params: Seq[(String, String)]) { def this(api_url: String, params: (String, String)*) = this(api_url, params.seq) } This code does not compile, but gives the warning: double definition: constructor Api:(api_url: String, params: (String, String)*)Api and constructor Api:(api_url: String

java force an extending class

☆樱花仙子☆ 提交于 2019-12-18 14:04:11
问题 In Java, can I somehow force a class that extends an abstract class to implement its constructor with a Object as a parameter? Something like public abstract class Points { //add some abstract method to force constructor to have object. } public class ExtendPoints extends Points { /** * I want the abstract class to force this implementation to have * a constructor with an object in it? * @param o */ public ExtendPoints(Object o){ } } 回答1: You can use a constructor with a parameter in your

java force an extending class

烈酒焚心 提交于 2019-12-18 14:03:28
问题 In Java, can I somehow force a class that extends an abstract class to implement its constructor with a Object as a parameter? Something like public abstract class Points { //add some abstract method to force constructor to have object. } public class ExtendPoints extends Points { /** * I want the abstract class to force this implementation to have * a constructor with an object in it? * @param o */ public ExtendPoints(Object o){ } } 回答1: You can use a constructor with a parameter in your

Java error: Default constructor cannot handle exception type FileNotFound Exception

人走茶凉 提交于 2019-12-18 13:45:36
问题 I'm trying to read input from a file to be taken into a Java applet to be displayed as a Pac-man level, but I need to use something similar to getLine()... So I searched for something similar, and this is the code I found: File inFile = new File("textfile.txt"); FileInputStream fstream = new FileInputStream(inFile);//ERROR // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); The line I marked

JavaScript better way to modify function prototype

大兔子大兔子 提交于 2019-12-18 13:30:52
问题 I wish to create a constructor of constructors. Relating to this thread : JavaScript build a constructor of constructors, it seems the only solutions are : Function.prototype.add = function(name, value) { this.prototype[name] = value; }; Function.prototype.remove = function(name) { delete this.prototype[name]; }; But I don't want to modify the generic Function prototype... and also : var A = new ConstBuilder().add('test', function() { console.log('test'); }).getConstructor(); But I don't want

Best way to write constructor of a class who holds a STL container in C++11

橙三吉。 提交于 2019-12-18 13:24:54
问题 class Foo { std::vector<SomeType> data_; }; Say Foo can only be constructed by make a copy (technically I mean a copy or move) of a std::vector<SomeType> object. What's the best way to write constructor(s) for Foo ? My first feeling is Foo(std::vector<SomeType> data) noexcept : data_(std::move(data)) {}; Using it, construction of an instance takes 0 or 1 times of vector copy, depending on whether the argument for {data} is moveable or not. 回答1: Your first feeling is good. Strictly speaking it

Can you invoke an instantiated object's class constructor explicity in C++?

狂风中的少年 提交于 2019-12-18 13:13:35
问题 After creating a instance of a class, can we invoke the constructor explicitly? For example class A{ A(int a) { } } A instance; instance.A(2); Can we do this? 回答1: You can use placement new, which permits new (&instance) A(2); However, from your example you'd be calling a constructor on an object twice which is very bad practice. Instead I'd recommend you just do A instance(2); Placement new is usually only used when you need to pre-allocate the memory (e.g. in a custom memory manager) and

Accessing a Private Constructor from Outside the Class in C#

前提是你 提交于 2019-12-18 12:52:30
问题 If I define a class with a private default constructor and a public constructor that has parameters, how can I access the private constructor? public class Bob { public String Surname { get; set; } private Bob() { } public Bob(string surname) { Surname = surname; } } I can access the private constructor via a static method on the class like this: public static Bob GetBob() { return new Bob(); } I thought that I could access the private constructor via an extension method, since (according to

Call one constructor from the body of another in C#

若如初见. 提交于 2019-12-18 12:43:35
问题 I need to call one constructor from the body of another one. How can I do that? Basically class foo { public foo (int x, int y) { } public foo (string s) { // ... do something // Call another constructor this (x, y); // Doesn't work foo (x, y); // neither } } 回答1: You can't. You'll have to find a way to chain the constructors, as in: public foo (int x, int y) { } public foo (string s) : this(XFromString(s), YFromString(s)) { ... } or move your construction code into a common setup method,

When to use Long vs long in java?

柔情痞子 提交于 2019-12-18 12:12:21
问题 Below is my Interface - public interface IDBClient { public String read(ClientInput input); } This is my Implementation of the Interface - public class DatabaseClient implements IDBClient { @Override public String read(ClientInput input) { } } Now I have a factory which gets the instance of DatabaseClient like this - IDBClient client = DatabaseClientFactory.getInstance(); .... Now I need to make a call to read method of my DatabaseClient which accepts the ClientInput parameter and below is