我试图了解函数中传递的赋值操作符、构造函数和参数是如何在python中使用列表和对象的。我有一个以list作为参数的类。我希望将其初始化为空列表,然后使用构造函数填充它。我不太清楚该怎么做。
让我们说我的课是--
class A:
List = [] # Point 1
def __init1__(self, begin=[]): # Point 2
for item in begin:
self.List.append(item)
def __init2__(self, begin):
我有一个包含两个构造函数的类。
一个构造函数File_Attachment(int Attachment_ID)创建类的一个实例,该实例由来自单个数据库行的数据填充。
第二个构造函数File_Attachment(string type, int mid)用第一个构造函数创建类的许多实例,并在List<>中收集它们。
下面是完整的类代码:
using System;
using System.IO;
using System.Web;
using System.Collections.Generic;
using System.Data.SqlClient;
public cl
我有一堂课:
public class Car {
public int year;
public String model;
}
还有一份清单:
List<Car> list = new DB<>(new Car()).getList();
我需要从我的类车中填充这个列表,例如填充json数组或sqlite中的一个表。
下面是我的泛型类,它应该填充它:
public class DB<T>{
private T type;
public DB(T type){this.type = type;}
public List&
我使用以下内容构造了一个Purse类:
public Purse(){
ArrayList<String> coins = new ArrayList();
}
用于向对象添加字符串的函数:
public void addCoin(String coinName){
coins.add(coinName);
}
这个想法是每个Purse对象都可以包含一个字符串列表。但是,我不能用我的方法访问ArrayList<String> coins。
例如,我编写的这个方法是为了反转字符串的顺序。
public void reverse(){
for (in
我已经创建了自定义列表:
public class BList : IEnumerable<Position>
{
private List<Position> positions = new List<BPosition>();
public void Add(Position p)
{ this.positions.Add(p); }
...
}
则此列表是类Invoice的成员:
public class Invoice : Record
{
public BList Po
我已经开发了一个类,我需要我的对象能够保存程序一生中发生的事情的“历史”。
type
TPipelineSimulator = class
private
class var history: TStringList;
class constructor Create;
class destructor Destroy;
//other code
public
class property bitHistory: TStringList read history;
//other code
end;
简单的实现是:
class construct
在本例中,我有一个带有预先确定项的ArrayList的类。在我的代码中,我有一个构造函数,调用一个填充列表的方法。
问题是每次实例化这个类的实例时,列表都会被重新填充。如何创建并填充列表一次?
public class Test {
ArrayList<String> lst = new ArrayList<String>();
public Test() {
populate();
}
public void populate() {
lst.add("Apple");
因此,在我的主form类中,我有以下列表,该列表由用户输入填充:
List<string> ProduseAlese = new List<string>();
在填充之后,它将进入这个构造函数:
Comenzi comanda = new Comenzi(nrCom, dataCom, dataLiv, factura, ProduseAlese);
我的类看起来是这样的:
public class Comenzi
{
public int NrComanda { get; set; }
public DateTime DataComanda { ge
我已经摆弄一个程序大约20分钟了,我发现出于某种原因,它不允许我在初始化列表中使用继承的变量。例如,该方案:
class A {
protected:
int i;
};
class B : public A {
public:
B() : i(45) { }
};
int main() {
B b;
}
会给出错误
错误:类“B”没有任何名为“i”的字段
但是,如果将构造函数更改为:
B() { i = 45; }
它会编译。
我不知道你不能初始化继承的变量。我的问题是,为什么?
在Delphi中,我创建了一个类,其目的是从数据库中读取记录。这个记录是纯只读的,在阅读之后,我需要导出一些额外的属性。我的问题与我想在类定义中使用的Stringlist有关。我有一个名为fVENDORS_TO_COLORCODE的私有类成员。这是一个逗号分隔的字符串。我想要建立一个属性是一个TStringlist。我使用TStringList.CommaToText将我的值加载到字符串列表中。我在Constructor中这样做。我遇到的问题是,虽然StringList在构造函数中是有效的,但它在构造函数之外是零的,而且我不知道我做错了什么。下面是代码的相关部分。
type
TProfil
我正在尝试创建一个类来填充结构化数据项列表,以及从文件和IO设备填充列表的一些方法。
我的方法存在问题,该方法填充新的数据结构并将其附加到列表中。它是作为一个协同线来设置的,它用(产)函数中的数据填充一个临时结构。完成后,它会将数据附加到列表中(例如,self.list.append(newdata))。我的问题是,这个附加是通过引用实现的,我不知道如何将新数据初始化到新的memoryspace。最后发生的是,我有一个数据列表,所有这些数据都指向相同的数据结构(例如,"myclass.listn is myclass.listm“总是产生TRUE)。有人能告诉我怎么做吗?
如果我是用C
以下代码是一个测试程序。为什么我不能在这个程序中使用A(int a)?
public class A {
int a;
void meth(int b) {
a+=b;
}
A(int a) {
this.a=a;
}
}
class B extends A {
int a;
void meh2(int b) {
a+=b;
}
}
为什么不能将参数传递给构造函数?原因是什么?Netbeans错误消息:
类tma1.A中的构造函数A不能应用于给定类型;必需: int found:无
如何在两个类之间共享数组列表。我有一个设置应用程序GUI的主类,我正在尝试创建一个执行mysql语句的Database类,以将数据存储、更新和检索到主类的数组列表中。
这是我想做的.
主类
public class Main
{
public static ArrayList<Animal> animal = new ArrayList<Animal>();
public static ArrayList<Farm> farm = new ArrayList<Farm>();
Database db;
public M
请看以下示例:
class A {
protected $a;
public function __construct() {
$this->a = "foo";
}
}
trait Q {
protected $q;
public function __construct() {
$this->q = "happy";
}
}
class B extends A {
use Q;
protected $b;
public function _
我正在将一个数组传递给一个构造器,但它给出了错误“构造函数在类差异中的差异不能应用于给定的类型-差异差异=新的差异(A);”根据错误,实际参数列表和形式参数列表的长度不同。为什么会这样呢?
class Difference {
private int[] elements;
public int maximumDifference;
Difference(int[] elementsArray) {
for (int i = 0; i < elementsArray.length; i++) {
elements[i] =
因此,我正在尝试编写一个方法,该方法将学生列表写入树状图中,但我的树状图在StudentViewcontroller中。我是否可以从我的write学生方法中调用它。
Here is my WriteStudent method so far:
Public void WriteStudent(PrintWriter pw) {
try{
for (Map.Entry<String, Student> s : studentMap.entrySet()){
}
}catch(FileNotFoundException e){
}
我有一个类,它有11个变量,需要使用用户提供的输入进行设置。该类具有以下构造函数和方法:
public class Sample {
int id;
Date date;
String location;
int sampleNum;
(...)
String[] keywords;
public Sample()
{
keywords = new String[5];
setData();
}
public void setData()
{
location = JOptionPane.showInputDialog("");
sampleNumb
我很难理解如何初始化对象列表并将其与类中的方法一起使用。我理解列表的机制,但不了解如何在方法中初始化它并在以后使用它。
例如,我希望有一个类在创建列表时创建它。然后,我想使用该类的一个方法将元素添加到列表中。列表中的元素是由SolidWorks API定义的对象。
因此,为了构造列表,我使用了:
public class ExportPoints : Exporter
{
public List<SldWorks.SketchPoint> listOfSketchPoints;
public ExportPoints(SldWorks.SldWorks swApp