标签: class-constructors

为什么我的C#构造函数不能使用我正在尝试使用的方法?

也许我误解了构造函数是如何工作的,但无论如何,我正在尝试创建一个数组并在构造函数中填充它.

我有以下代码 -

class ClsDeck
{
    private string[] deck = new string[52];
    private string[] hand = new string[12];
    BuildDeck()
    {
        //lots of code assigning images to each individual element of the "deck" array.
    }

    //many other methods that need to be called by a form.
}
Run Code Online (Sandbox Code Playgroud)

Visual Studio 2012坚持该方法具有返回类型.我只是在BuildDeck方法中添加了"void",并且错误消失了,但是我看到的构造函数的每个例子都必须与类具有相同的名称,并且它是类中唯一的方法.

c# class-constructors

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

Python 3 中默认的 ```__new__``` 是什么?

我相信我对应该做什么有某种理解__new__(创建一个类的实例,但不初始化它,这是 的工作__init__)。但是,我想了解 Python 3__new__默认实现的方法。

我还发现它cls是 的一个参数有点令人困惑__new__,但它__new__是一个静态方法而不是一个类方法(我从文档中得到了这个)......现在它如何作为它的第一个参数传递一个类型?

python class-constructors default-constructor python-3.x

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

构造函数中的多个参数

我把下面的构造函数放在一起.我的一个问题是:如何使用相同的构造函数,没有参数,同时使用两个或三个?有不止一种方法可以做到这一点吗?谢谢

public class bankaccount {

  String firstnameString;
  String lastnameString;
  int accountnumber;
  double accountbalance;;

  public bankaccount(String firstname,String lastname){
    int accountnumber=999999;
    double accountbalance=0.0;
  }
}
Run Code Online (Sandbox Code Playgroud)

java class-constructors

2
推荐指数
1
解决办法
606
查看次数

function():this(null){}

有人可以解释下面的语法糖吗?

protected MyConstructor() : this(null)
Run Code Online (Sandbox Code Playgroud)

主要是我对这部分感兴趣:" : this(null)"

我知道受保护的,构造函数和"this"关键字是如何工作的,但我很困惑,无法在我的所有在线搜索中找到最后一部分的所有详细信息.

编辑:我应该补充一点,它是在一个公共抽象类中.所以我猜构造函数正在调用实现者构造函数.

谢谢

c# syntax null constructor class-constructors

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

C++ - 使用指针作为数据字段复制构造函数

我有以下代码: -

#include <iostream>
using namespace std;

class A { 
    int *val;
public:
    A() { val = new int; *val = 0; }
    int get() { return ++(*val); } 
};

int main() {
    A a,b = a;

    A c,d = c;

    cout << a.get() << b.get() ;
    cout << endl ; 

    cout << c.get() << endl ;//
    cout << d.get() << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它产生的输出为: -

21
1
2
Run Code Online (Sandbox Code Playgroud)

在c.get和d.get的情况下的行为很容易理解.c和d共享相同的指针val,a和b共享相同的指针val.

所以c.get()应该返回1而d.get()应该返回2.但我期望在a.get()和b.get()中有类似的行为.(也许我还没有正确理解cout)

我无法理解a.get()是如何产生2的.

你能解释一下为什么我会得到这样的输出.据我说,输出应该是: -

12
1
2
Run Code Online (Sandbox Code Playgroud)

c++ pointers class-constructors

2
推荐指数
1
解决办法
54
查看次数

java中构造函数的继承

每当在任何派生类中调用任何构造函数时,任务只能通过最终隐式或显式地调用基类构造函数来完成(如果我在这里错了,请纠正我).

因为我们打算创建派生类的实例,但是因为最后调用了基类构造函数.

那么,尽管调用了基类的构造函数,如何构造派生类的实例?

java class-constructors inheriting-constructors

2
推荐指数
1
解决办法
548
查看次数

Python 中函数和构造函数的枚举

我正在学习 Python,并正在为 PyGame 库制定边界。这没有什么问题,我只是想让方法和类更适合我的特定项目。简而言之,我想做一个颜色枚举,但是枚举中的每个值都有 RGB 和 Hex 版本。由于我之前已经在 J​​ava 中完成过此操作,因此我将粗略地展示我在 Java 枚举中的含义。

public enum Color {

    RED(new double[]{255, 0, 0}, "#ff0000"),
    CYAN(new double[]{0, 255, 255}, "#00e1ff");
    // Other colors...

    double[] rgb;
    String hex;

    Color(double[] rgb, String hex) {
        this.rgb = rgb;
        this.hex = hex;
    }

    public double[] getRGB() {
        return rgb;
    }

    public String getHex() {
        return hex;
    }
}
Run Code Online (Sandbox Code Playgroud)

我在这里所做的就是为枚举创建一个构造函数(在 Java 中默认为私有),并使枚举中的每个项目指定该构造函数中的参数。例如RED,通过构造函数指定rgb double数组等于{255,0,0},十六进制字符串为“ff0000”。使用底部的方法,您可以在代码中的任何位置检索这些值。

最后,如果我将枚举导入到代码中,然后使用 RED 项,我可以说 RED.getRGB() 并获得一个双精度数组。我想在此枚举中添加一堆颜色,但我也想了解一般的语法,因为它适用于我的程序的其他部分。

我想用 Python 来做这个。我几乎不理解如何在 Python 中进行常规枚举,也不理解整个“class Clazz(Enum):”的事情,但我当然不知道如何在 Python 中执行上面的代码。我只是不知道语法。

python java enums class-constructors

2
推荐指数
1
解决办法
5535
查看次数

我可以使子类隐式继承基类构造函数吗?

假设我有:

class A {
public:
        A(int x_) : x(x_) {}
        int x;
};

class B: public A { };
class C: public A { };
Run Code Online (Sandbox Code Playgroud)

使用此代码,B和C将不具有任何构造函数(复制构造函数除外).我想改变的东西类A(不是BC),这样既BC将继承的构造A.这有可能吗?

c++ inheritance class-constructors c++14

1
推荐指数
1
解决办法
55
查看次数

初始化模板类成员时出现问题

我的代码没有编译.以下是我的代码

template <typename T>

class TemplateClass
{

    const T constMember;
    public:

    TemplateClass()
    {
        constMember = T();
    }

};

int main()
{
   TemplateClass <int> obj;
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

error: uninitialized member 'TemplateClass<int>::constMember' with 'const' type 'const int'

我以为构造函数用于初始化数据成员.怎么了????

c++ templates class-constructors

0
推荐指数
1
解决办法
1284
查看次数

Kotlin主要构造函数调用次要构造函数

为什么不编译?

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)

为什么?

class-constructors multiple-constructors kotlin

0
推荐指数
1
解决办法
1583
查看次数

下面给出的代码中的C++构造函数定义差异

我是C++的新手.学习构造函数.请参阅下面提到的两个代码,并提供原因,为什么代码2不起作用.谢谢.

代码1:

#include <iostream>
using namespace std;

class Box
{
    int x;
public:
    Box::Box(int a=0)
    {
        x = a;
    }
    void print();
};

void Box::print()
{
    cout << "x=" << x << endl;
}

int main()
{
    Box x(100);
    x.print();
}
Run Code Online (Sandbox Code Playgroud)

代码2:

#include <iostream>
using namespace std;

class Box
{
    int x;
public:
    Box(int a=0);
    void print();
};

Box::Box(int a=0)
{
    x = a;
}

void Box::print()
{
    cout << "x=" << x << endl;
}

int main()
{
    Box …
Run Code Online (Sandbox Code Playgroud)

c++ class-constructors

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

在 Kotlin 中使用辅助构造函数

我收到错误:

期待会员声明

class MyAdapter(val context: Context)  {
    constructor(context: Context,  itemInfos: List<ItemInfo>): RecyclerView.Adapter<ContentItemViewHolder> {

    }
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

android constructor class-constructors android-adapter kotlin

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