是否可以在javascript中为一个类创建多个构造函数?即一个零参数,一个一个,一个两个,等等...
如果是这样,怎么样?
谢谢!
在绝对初学者,第2版书的C++编程中,有以下声明:
HeapPoint::HeapPoint(int x, int y): thePoint(new Point(x,y)) { }
Run Code Online (Sandbox Code Playgroud)
这等于:
HeapPoint::HeapPoint(int x, int y) { thePoint = new Point(x,y); }
Run Code Online (Sandbox Code Playgroud)
而且,既然我们在构造函数中执行此操作,那么为x和分配的值是y什么?我们应该写值insted的中x和y在new Point(x,y)?或者,这是正确的吗?
更新:我认为我有了初始化的想法,x并且y正如在书中它在函数中有以下内容:
HeapPoint myHeapPoint(2,4);
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建这样的泛型类:
class A[T](v: Option[T]) {
def this(v: T) = this(Some(v))
def this() = this(None)
def getV = v
}
Run Code Online (Sandbox Code Playgroud)
然后我做了一些测试:
scala> new A getV
res21: Option[Nothing] = None
scala> new A(8) getV
res22: Option[Int] = Some(8)
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.但是当我尝试调用主构造函数时,我得到了这个:
scala> new A(Some(8)) getV
<console>:9: error: ambiguous reference to overloaded definition,
both constructor A in class A of type (v: T)A[T]
and constructor A in class A of type (v: Option[T])A[T]
match argument types (Some[Int])
new A(Some(8)) getV
^
scala> new A(None) getV …Run Code Online (Sandbox Code Playgroud) 这是一种反模式,但我很好奇实际会发生什么。
如果显式定义一个无参数构造函数和一个带自动装配参数的构造函数,spring框架将如何初始化它?
@Service
class Clazz {
private MyBean myBean;
public Clazz(){}
@Autowired
public Clazz(MyBean myBean){
this.myBean = myBean;
}
}
Run Code Online (Sandbox Code Playgroud) java spring dependency-injection javabeans multiple-constructors
我想在PHP类中定义一些构造函数.但是,我的构造函数代码目前非常相似.如果可能的话,我宁愿不重复代码.有没有办法从php类中的一个构造函数中调用其他构造函数?有没有办法在PHP类中有多个构造函数?
function __construct($service, $action)
{
if(empty($service) || empty($action))
{
throw new Exception("Both service and action must have a value");
}
$this->$mService = $service;
$this->$mAction = $action;
$this->$mHasSecurity = false;
}
function __construct($service, $action, $security)
{
__construct($service, $action); // This is what I want to be able to do, so I don't have to repeat code
if(!empty($security))
{
$this->$mHasSecurity = true;
$this->$mSecurity = $security;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过创建一些Init方法来解决这个问题.但有没有办法绕过这个?
我希望子类使用其父类的构造函数.但似乎我总是需要在子类中再次定义它们才能使它工作,如下所示:
public SubClass(int x, int y) : base (x, y) {
//no code here
}
Run Code Online (Sandbox Code Playgroud)
所以我想知道我是不是在父类中正确地声明了构造函数,或者根本没有直接的构造函数继承?
我想为一个类有两个构造函数,如下所示:
public MyClass()
{
// do stuff here
}
public MyClass(int num)
{
MyClass();
// do other stuff here
}
Run Code Online (Sandbox Code Playgroud)
以上是达到我目的的正确方法吗?有什么样的速记更好吗?
是否可以为C#类定义备用无参数构造函数?
换句话说,我有一堂课Foo.我想要一个默认的构造函数Foo()和另一个构造函数SpecialFoo().我不介意SpecialFoo()构造函数是否调用Foo()构造函数.
我能这样做吗?
我不明白使用构造函数Rational()创建Rational对象时会发生什么.我的书说它将创建一个Rational对象,其值为0但内部存储为0/1.这个(0)如何存储为0/1?不是num和den 0的实例变量的默认值吗?
public class Rational{
public Rational(){
this(0);
}
public Rational(int n){
this(n,1);
}
public Rational(int x, int y){
num = x;
den = y;
}
private int num;
private int den;
}
Run Code Online (Sandbox Code Playgroud) 我不太确定这是如何工作的,但是如果我想给一个类的对象提供更多或更少的变量的选项,这是否适用于这样的多个构造函数?
假设我想创建一个多项选择问卷,但是我不知道我的用户想要输入多少个答案,可能是 2、3、4、5、6?所以为此:
public class Quiz {
private int counter;
private String question;
private String answer1;
private String answer2;
private String answer3;
private String answer4;
private String answer5;
private String answer6;
private String rightAnswer;
public Quiz(int counter,String question, String answer1, String answer2, String rightAnswer){
super();
this.counter = counter;
this.question = question;
this.answer1 = answer1;
this.answer2 = answer2;
this.rightAnswer = rightAnswer;
}
public Quiz(int counter, String question, String answer1, String answer2, String answer3, String rightAnswer) {
super();
this.counter = counter;
this.question …Run Code Online (Sandbox Code Playgroud) 在这里,当我运行下面的代码时,我得到called输出,我想知道为什么不called new.因为1来自两个short和int范围.
public class MyClass {
private int x;
public MyClass(){
this(1);
}
public MyClass(int x){
System.out.println("called");
this.x = x;
}
public MyClass(short y){
System.out.println("called new");
this.x = y;
}
public static void main(String args[]) {
MyClass m = new MyClass();
System.out.println("hello");
}
}
Run Code Online (Sandbox Code Playgroud) 我是C#的新手,并试图找出字符串插入(即"some {0} string", toInsert),并遇到了一个我没想到的问题......
在您有两个构造函数的情况下:
public MyClass(String arg1) { ... }
public MyClass(String arg1, String arg2) { ... }
Run Code Online (Sandbox Code Playgroud)
我可以使用带有字符串插入的第一个构造函数吗?
...
toInsert = "def"
myClass = new MyClass("abc{0}ghi", toInsert)
...
Run Code Online (Sandbox Code Playgroud)
或者C#将此解释为第二个构造函数并将文字"abc{0}ghi"作为第一个参数传递?
为什么不编译?
class test
{
constructor() {
var a = Date().day
this(a)
}
constructor(a:Int) {
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:无法将类型为“ test”的表达式“ this”作为函数调用。找不到函数“ invoke()”。
建议的解决方案是添加以下内容:
private operator fun invoke(i: Int) {}
Run Code Online (Sandbox Code Playgroud)
为什么?
c# ×4
java ×4
constructor ×3
c++ ×1
generics ×1
inheritance ×1
javabeans ×1
javascript ×1
kotlin ×1
php ×1
pointers ×1
scala ×1
scala-2.9 ×1
spring ×1
this ×1