小编drm*_*irk的帖子

在Dart中,是否可以单例传递参数?

class Peoples {
  int id;
  String name;

  static final Peoples _inst = Peoples._internal();

  Peoples._internal();

  factory Peoples() {
    return _inst;
  }
}
Run Code Online (Sandbox Code Playgroud)

我有这个单例课程。这样可以确保只创建一个类的实例。因此,即使有人尝试实例化它,他们也将使用相同的实例。我可以创建和设置值,例如:

  Peoples ps1 = Peoples();
  Peoples ps2 = Peoples();

  ps1.id = 1;
  ps1.name = "First";

  ps2.id = 2;
  ps2.name = "Second";
Run Code Online (Sandbox Code Playgroud)

是否可以实例化和设置值,例如:

  Peoples ps1 = Peoples(1, "First");
  Peoples ps2 = Peoples(2, "Second");
Run Code Online (Sandbox Code Playgroud)

因此,现在“ ps1”和“ ps2”都将具有(2,“ Second”)。

oop singleton dart flutter

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

与结构体一起使用时 calloc 的初始值

我正在尝试使用以下代码编写一个非常简单的哈希表:

#include <stdio.h>
#include <string.h>
#define SIZE 10

typedef struct {
    char *first_name;
    char *last_name;
} Employee;

typedef struct {
    Employee *table;
} Hashtable;

void initHashtable(Hashtable *ht) {
    ht->table = (Employee *)calloc(SIZE, sizeof(Employee));
}

int hashKey(char *key) {
    int length = strlen(key);
    return length % SIZE;
}

void put(Hashtable *ht, Employee emp, char *key) {
    int hashedKey = hashKey(key);
    ht->table[hashedKey] = emp;
}
Run Code Online (Sandbox Code Playgroud)

我可以通过执行以下操作插入元素:

int main(int argc, char const *argv[]) {

    Employee e1;
    e1.first_name = "John";
    e1.last_name = "Doe";

    Hashtable …
Run Code Online (Sandbox Code Playgroud)

c struct heap-memory calloc dynamic-memory-allocation

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