java创建对象数组然后用构造方法实例化
答案:4 悬赏:80 手机版
解决时间 2021-03-27 15:45
- 提问者网友:黑米和小志
- 2021-03-27 08:31
java创建对象数组然后用构造方法实例化
最佳答案
- 五星知识达人网友:狂恋
- 2021-03-27 10:09
Java创建对象数组然后构造方法实例化,如下:
package com.test;
import java.util.Arrays;
public class Employee {
private String number;
private String name;
private double salary;
public Employee() {
System.out.println("调用了构造函数方法一,实例化对象");
}
public Employee(String number, String name, double salary) {
super();
this.number = number;
this.name = name;
this.salary = salary;
System.out.println("调用构造函数方法二,实例化对象");
}
public static void main(String[] args) {
//构造Employee对象数组为2长度
Employee[] emp=new Employee[2];
//员工一(实例化),并且构造函数里设置值
Employee e1 = new Employee("e0001", "xiaoming", 5000.0);
//员工二(实例化),用set设置值,get的话可以获取到员工某个属性
Employee e2 = new Employee();
e2.setName("小二");
e2.setNumber("e0002");
e2.setSalary(5500.1);
//将实例化的对象存进数组中
emp[0]=e1;
emp[1]=e2;
System.out.println("实例化的数组对象为:"+Arrays.toString(emp));
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [number=" + number + ", name=" + name + ", salary=" +
salary + "]";
}
}
运行结果:
调用构造函数方法二,实例化对象
调用了构造函数方法一,实例化对象
实例化的数组对象为:[Employee [number=e0001, name=xiaoming, salary=5000.0], Employee [number=e0002, name=小二, salary=5500.1]]
package com.test;
import java.util.Arrays;
public class Employee {
private String number;
private String name;
private double salary;
public Employee() {
System.out.println("调用了构造函数方法一,实例化对象");
}
public Employee(String number, String name, double salary) {
super();
this.number = number;
this.name = name;
this.salary = salary;
System.out.println("调用构造函数方法二,实例化对象");
}
public static void main(String[] args) {
//构造Employee对象数组为2长度
Employee[] emp=new Employee[2];
//员工一(实例化),并且构造函数里设置值
Employee e1 = new Employee("e0001", "xiaoming", 5000.0);
//员工二(实例化),用set设置值,get的话可以获取到员工某个属性
Employee e2 = new Employee();
e2.setName("小二");
e2.setNumber("e0002");
e2.setSalary(5500.1);
//将实例化的对象存进数组中
emp[0]=e1;
emp[1]=e2;
System.out.println("实例化的数组对象为:"+Arrays.toString(emp));
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [number=" + number + ", name=" + name + ", salary=" +
salary + "]";
}
}
运行结果:
调用构造函数方法二,实例化对象
调用了构造函数方法一,实例化对象
实例化的数组对象为:[Employee [number=e0001, name=xiaoming, salary=5000.0], Employee [number=e0002, name=小二, salary=5500.1]]
全部回答
- 1楼网友:枭雄戏美人
- 2021-03-27 13:01
这个是你自己想着写的还是看视频写的??
- 2楼网友:人類模型
- 2021-03-27 12:37
public class Demo{
private Integer[] array;
Demo(Integer[] array){
this.array = array;
}
}
private Integer[] array;
Demo(Integer[] array){
this.array = array;
}
}
- 3楼网友:不甚了了
- 2021-03-27 11:05
对象数组,创建数组后,数组的各元素,还有初始化的——创建对象实例的追问具体怎么改才对呢。。追答xinxi[i]
每个元素,在使用之前,要赋值
xinxi[i]=new ssxx();
每个元素,在使用之前,要赋值
xinxi[i]=new ssxx();
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯