怎么创建带参数构造函数类的对象数组

怎么创建带参数构造函数类的对象数组,第1张

你可以输入以后再拷贝到数组里去(operator=),因为定义对象数组时就必须初始化每个对象,所以
student
=
new
student[n];
这一句实际上已经调用了每个对象的无参数构造函数,如果你没有提供一个无参数的构造函数那么这个句子会报错
另一种方法是
student
student;
student
=
new
student[n];
//
输入第i学生的信息
//
student[i]
=
new
student(构造参数);
这样就可以实现你说的输入以后再根据输入信息构造了

Array 对象用于在单个的变量中存储多个值,在JavaScript中声明一个数组的方法有很多。

工具原料:编辑器、浏览器

1、在JavaScript中声明一个数组的方法是 new Array(),声明一个数组并且存储值的方法如下:

<html>
<body>
<script type="text/javascript">
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"
for (i=0;i<mycarslength;i++)
{
documentwrite(mycars[i] + "<br />")
}
</script>
</body>
</html>

2、运行的结果如下:

类对象数组初始化
如有一个如下类:
class EquipmentPiece {
private:
int IDNumber;
public:
EquipmentPiece(int IDNumber) : IDNumber(IDNumber) {};
};
以下列出几种初始化的方法:
<一>、对象数组
int ID1, ID2, ID3;
EquipmentPiece bestPieces[] = { EquipmentPiece(ID1), EquipmentPiece(ID2), EquipmentPiece(ID3) };
注意:
EquipmentPiece bestPieces[10]; //no appropriate default constructor available
EquipmentPiece bestPieces = new EquipmentPiece[10]; //no appropriate default constructor available
当然,如果你将构造函数参数全都设了默认值,以上两种写法也成功,如将类中构造函数修改如下:

EquipmentPiece(int IDNumber = 0) : IDNumber(IDNumber) {};

<二>、指针数组
typedef EquipmentPiece PEP; //PEP是个指向EquipmentPiece的指针
PEP bestPieces[10]; //等同于 PEP bestPieces = new PEP[10];
//然后初始化
for(int i = 0; i < 10; i++){
bestPieces[i] = new EquipmentPiece( IDNumber );
}
注意:
要记得将此数组所指的所有对象删除。如果忘了会产生资源泄露。还有就是该方法与对象数组相比需要额外内存用于存放指针。(过度使用内存 这一问题可以避免,见第三种方法)
<三>、使用placement new
方法是:先为此数组分配raw memory,然后使用"placement new"在这块内存上构造EquipmentPiece objects;
//分配足够的raw memory,给一个预备容纳10个EquipmentPiece objects的数组使用
void rawMemory = operator new(10sizeof(EquipmentPiece));
//让bestPieces指向此内存,使这块内存被视为一个EquipmentPiece数组
EquipmentPiece bestPieces = reinterpret_cast<EquipmentPiece>(rawMemory);
//利用"placement new"构造这块内存中的EquipmentPiece objects。
int IDNumber = 0;
for(int i = 0; i < 10; i++){
new (&bestPieces[i]) EquipmentPiece( IDNumber );
}
注意:该方法维护比较困难。在数组内对象结束生命时,要以手动方式调用destructors,最后还得调用operator delete释放raw memory。
//将bestPieces中对象以构造次序的反序析构掉
for(i = 0; i < 10; i++){
bestPieces[i]~EquipmentPiece();
}
//释放raw memory
operator delete (rawMemory);

public class Employee {

public Employee() {
super();
// TODO Auto-generated constructor stub
}

public Employee(String no, String name, Float salary) {
super();
thisno = no;
thisname = name;
thissalary = salary;
}
private String no;// 工号
private String name;// 姓名
private Float salary = 0f;// 工资
public String getNo() {
return no;
}
public void setNo(String no) {
thisno = no;
}
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public Float getSalary() {
return salary;
}
public void setSalary(Float salary) {
thissalary = salary;
}
@Override
public String toString() {
return "Employee [no=" + no + ", name=" + name + ", salary=" + salary + "]";
}

}
public class TestEmployee {
public static void main(String[] args) {
TestEmployee testEmployee = new TestEmployee();
Employee[] emps = testEmployeegetEmployees();
emps = testEmployeeorderBySalary(emps);
for(int i = 0; i < empslength; i++) {
Systemoutprintln(emps[i]);
}
}

/
  获取一个随机工资数 3-5K
  @return
 /
public Float getRandomFloat() {
DecimalFormat dcmFmt = new DecimalFormat("000");
Random rand = new Random();
float f = 0f;
while(f < 3000) {
f = randnextFloat()  5000;
}
return FloatparseFloat(dcmFmtformat(f));
}

/
  获取员工
  @return 返回Employee[] 数组  length = 50
 /
public Employee[] getEmployees() {
Employee[] emps = new Employee[50];
for(int i = 0; i < 50 ; i++) {
String no = "no" + i;// 初始化员工号
String name = "name" + i;// 初始化姓名
Float salary = getRandomFloat();// 随机产生一个工资数
emps[i] = new Employee(no, name, salary);
}

return emps;
}

/
  根据工资高低 进行排序
  @param emps
  @return
 /
public Employee[] orderBySalary(Employee[] emps) {
for(int i = 0; i < empslength; i++) {
for(int j = i + 1; j < empslength; j++) {
if(emps[i]getSalary() < emps[j]getSalary()) {
Employee temp = emps[i];
emps[i] = emps[j];
emps[j] = temp;
}
}
}
return emps;
}



}

下面是三个子类:
Sub1 sub1=new Sub1();
Sub2 sub2=new Sub2();
Sub3 sub3=new Sub3();
对象数组:
Acar[] acars=new Acar[3];
赋值:
acars[0]=(Acar)sub1;
acars[1]=(Acar)sub2;
acars[2]=(Acar)sub3;
如果要调用这个对象数组中的第一个元素的方法(假如它实现了print1()方法)。需要这么写:
acars[0]print1();

你想用new动态分配是这样写

Point ptr=new Point[8];//这里是分配了对象数组
delete []ptr;//这里没办法指定删除第几个,这是 释放内存
比如你想删除第三个,这里是没办法执行的,这块内存分配到那了,你就可以这样
ptr[2]=NULL;//  这样就是初始化,但是,不是释放内存


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13348814.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-19
下一篇 2023-07-19

发表评论

登录后才能评论

评论列表(0条)

保存