标签: constructor

我在PHP中发现了一个错误,或者我错过了什么?

可能重复:
为什么即使类和构造函数的情况不同,我的构造函数仍然被调用?

<?php
 abstract class foo {
  function foof() {
   echo "Hello, I'm foo :)";
  }
 }

 class foo2 extends foo {
  function foo2f() {
   $this->foof();
  }
 }

 class foo3 extends foo2 {
  function foo3f() {
   $this->foo2f();
  }
 }

 $x = new foo3;
 $x->foo3f();
?>
Run Code Online (Sandbox Code Playgroud)

这段代码输出"Hello,I'm foo :)"(正如预期的那样),但当我将代码改为这样的代码时:http://pastebin.com/wNeyikpq

<?php
abstract class foo {
 function fooing() {
  echo "Hello, I'm foo :)";
 }
}

class foo2 extends foo {
 function foo2() {
  $this->fooing();
 }
}

class foo3 extends …
Run Code Online (Sandbox Code Playgroud)

php constructor php4

-3
推荐指数
1
解决办法
136
查看次数

为什么Java不允许在构造函数中递归?

我认为允许递归有时候非常方便,不是因为我可以在"递归"中编码,而是因为我可以为某些情况保存一些代码空间,如下所示

public class SomeClass
{
   private int a;

   SomeClass(int a)
   {
      this.a = a;
   }

   SomeClass()
   {
      SomeClass(3);
   }
}
Run Code Online (Sandbox Code Playgroud)

当一个构造函数试图利用包含大块代码的另一个构造函数时,这尤其有效.

但是,Java显然不支持这个功能,我相信它并不是一个很好的理由.有人可以解释为什么吗?

java recursion constructor

-3
推荐指数
2
解决办法
4025
查看次数

Java构造函数是否仅在参数化时调用?

显然Java认为我的构造函数代码并不重要,所以当我尝试访问我认为已初始化的ArrayList时,它完全忽略它然后用NullPointerException对我大喊大叫.只有当我向构造函数添加任意参数时,Java才会认为值得关注.

import java.util.ArrayList;

public class DataManager {
    ArrayList<Variable> vars;

    public DataManager() {
        vars = new ArrayList<Variable>();
    }

    public void createVar(String type, String name, String strValue, int numValue) {
        vars.add(new Variable(type, name, strValue, numValue));
    }
}
Run Code Online (Sandbox Code Playgroud)

以及调用此代码的代码:

DataManager data = new DataManager();
data.createVar(...);
Run Code Online (Sandbox Code Playgroud)

变量类:

class Variable {
    String type;
    String name;
    String strValue;
    int numValue;

    public Variable(String type, String name, String strValue, int numValue) {
            this.type = type; this.name = name;
            this.strValue = strValue;
            this.numValue = numValue;
    }
}
Run Code Online (Sandbox Code Playgroud)

运行此结果

线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException:在SudoCode.main(SudoCode.java:6)的SudoCode.go(SudoCode.java:10)的Parser.start(Parser.java:25)处出现1 …

java parameters constructor default-constructor

-3
推荐指数
1
解决办法
169
查看次数

使用构造函数时获取nullpointerexception

public class Author {
private int id;
private String name;
private String university;
private String department;
private String email;
private int article1;
private int article2;
private int article3;
private int article4;
private int article5;
//constructors and getter/setters are generated automatically, not adding to have space
}
Run Code Online (Sandbox Code Playgroud)

这是我的作者类.该类仅具有这些属性.我还readDaFile创建了一个用于读取author.txt和创建作者对象的类.

import java.util.Scanner;
import java.io.*;


public class readAuthor {

private Scanner reader;
private String temp;
private String[] split;
public Author[] authorList;
private int dummyInt,dummyArticle1=0 ,dummyArticle2=0 ,dummyArticle3=0,dummyArticle4,dummyArticle5;
private int i=0;
private …
Run Code Online (Sandbox Code Playgroud)

java constructor nullpointerexception

-3
推荐指数
1
解决办法
126
查看次数

C++:朋友作为课堂上的主要人物

主要功能可以在C++中成为好友功能吗?

 #include "stdafx.h"
#include <iostream>
using namespace std;
class A {
public:
    A():i(10){}
private:
    int i;
    friend int main();
};

int main()
{
    A obj;
    cout<<obj.i;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ constructor class friend

-3
推荐指数
1
解决办法
264
查看次数

C++中构造函数的条件

这个条件在构造函数中是正确的还是有更好的方法来做到这一点?

class Foo {
    public: Foo(int y) {
        if (y < 0 || y > 99)
           cout << "Error! Invalid input" << endl;

        else
            x = y;
        }

    private: int x;
    };
Run Code Online (Sandbox Code Playgroud)

c++ constructor

-3
推荐指数
1
解决办法
2048
查看次数

C++ 中的字符指针数组和字符串数组

我想创建一个类颜色,并声明 2 个类颜色对象来分别存储暖色和冷色的列表。

我如何初始化一个字符指针数组并在 C++ 的构造函数中定义它?

。H

Const int MAX = 5;

Class Color {

  Char* list[MAX];
  Int numList;
}
Run Code Online (Sandbox Code Playgroud)

.cpp

//default constructor
Color() {
  list[MAX] = nullptr;
  numList = 0;
}

//custom constructor
Color(char* list_, int numList_) {
  for (int i = 0; i < num_List_; i++) {
    list[i] = list_[i];
  }

  numList = numList_;
}
Run Code Online (Sandbox Code Playgroud)

主程序

//declaring 2 objects of Class Color
Color warm_color;
Char warmList = {red, pink, orange, yellow, brown};

Color cold_color;
Char coldList = {“green”, …
Run Code Online (Sandbox Code Playgroud)

c++ arrays constructor pointers char

-3
推荐指数
1
解决办法
242
查看次数

Python:为什么我会收到“意外的关键字参数”错误?

我正在尝试设置图表。对于初始化,我想要选择是否从节点和边的集合开始。所以我给了他们默认值None。或者我想:

def Graph():
    def __init__(self, nodes=None, edges=None, msg="test"):
        """
        assumes that the node and edge lists are the respective objects
        """
        if nodes == None:
            self.nodes = []
        else:
            self.nodes = nodes
            
        if edges == None:
            self.edges = []
        else:
            self.edges = edges
        
        self.node_names = []
        for node in nodes:
            self.node_names.append(node.get_name())
            
        self.msg = msg

Run Code Online (Sandbox Code Playgroud)

(味精部分是为了用最简单的例子测试代码)

我得到的是:

g = Graph(msg="33")
Traceback (most recent call last):

  File "<ipython-input-29-cc459c9baef3>", line 1, in <module>
    g = Graph(msg="33")

TypeError: Graph() got an unexpected keyword argument …
Run Code Online (Sandbox Code Playgroud)

python constructor keyword-argument

-3
推荐指数
1
解决办法
454
查看次数

有人可以解释为什么现代 C++ 构造函数语法中的对象名称后面有一个分号吗?例如,在构造函数中... rd; {};

大约 20 年前,我曾经写过一些 c++。我需要重新做一些工作,但事情已经改变了。尽管在 Stroustrup 的 c++ 书中的第四版中进行了搜索,但我确实不理解一些新语法。我正在使用 Apple clang 版本 15.0.0 (clang-1500.1.0.2.5) 目标:arm64-apple-darwin23.3.0 线程模型:posix。在构造函数中,我希望使用 rd{}; 之类的语法来初始化对象。我不明白为什么编译器想要在对象名称后面加一个分号,例如 rd;{};

以下代码编译并运行 - 我只是不明白注释中指出的部分。

#include <iostream>
#include <random>

using namespace std;

class normRand {

public:

    random_device rd;
    mt19937 gen;
    normal_distribution<double>  dist;

    normRand() {
          // The constructor is meant to initialise the Mersenne twister
          // random number generator with a seed from some device and then
          // use this generator to randomly select values from a standard
          // normal distribution.
      
          // Why is there …
Run Code Online (Sandbox Code Playgroud)

c++ syntax constructor

-3
推荐指数
1
解决办法
83
查看次数

PHP构造无法正常工作?

我有以下代码:

<?php

class Human()
{

    function __construct()
    {
        echo "A human was born";
    }

}

$Person1 = new Human();

?>
Run Code Online (Sandbox Code Playgroud)

我试图打印出"人类出生",但它只是没有它!

php oop constructor class

-4
推荐指数
1
解决办法
1104
查看次数