java中创建一个客户类数组customer[] 怎么给数组中的变量赋值?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中创建一个客户类数组customer[] 怎么给数组中的变量赋值?相关的知识,希望对你有一定的参考价值。

如果可以的话,采用另外的方法也可以。
要求输出结果相同:

输入会员编号:20101
输入会员积分:1800
***会员列表***
编号 积分
20101 1800
1. 创建会员类 Customer 添加两个属性:会员编号、会员积分
2。创建会员操作类 CustManager

添加属性会员数组 customers
Customer [] customer = new Customer[100];

可添加多个会员编号和积分,要求接收add() 和打印所有 show()

先给数组元素new customer对象,然后通过customer对象在给属性变量赋值。

过程

customer类:

class customer//定义customer类
   public int a1;//顶一个变量属性a1

1、顶一个customer数组

customer[] ct = new customer[3];//定一个customer数组,数组长度是3

2、给数组中的customer赋值

for(int i=0;i<ct.length;i++)
    ct[i] = new customer();//实例化customer对象
    ct[i].a1 = 2;//给customer类的a1属性赋值

参考技术A 把会员信息封装成对象加到数据里面就行 要赋值就取出对象用setID 或者setJF方法就可以把 参考技术B public class Customer
int custNum;
int points;

public Customer()
public Customer(int custNum,int points)

this.custNum = custNum;
this.points = points;


public int getCustNum()
return custNum;

public void setCustNum(int custNum)
this.custNum = custNum;

public int getPoints()
return points;

public void setPoints(int points)
this.points = points;


public String lineInfo()
return this.custNum + "/t" + this.points;


/**
* 你的 show()
*/
public static void printList(Customer[] ca)
System.out.println("***会员列表***/r/n编号/t 积分");

if(ca!=null)
for(int i=0; i<ca.length; i++)
if(ca[i]!=null)
System.out.println(ca[i].lineInfo());



/**
* 你的 add()
* @return
* @throws IOException
*/
public Customer input() throws IOException
Customer c = new Customer();
System.out.println("输入会员号:");
c.setCustNum(System.in.read());
System.out.println("输入会员积分:");
c.setPoints(System.in.read());

return c;
参考技术C import java.io.IOException;

public class Customer
int custNum;
int points;

public Customer()
public Customer(int custNum,int points)

this.custNum = custNum;
this.points = points;


public int getCustNum()
return custNum;

public void setCustNum(int custNum)
this.custNum = custNum;

public int getPoints()
return points;

public void setPoints(int points)
this.points = points;


public String lineInfo()
return this.custNum + "/t" + this.points;


/**
* 你的 show()
*/
public static void printList(Customer[] ca)
System.out.println("***会员列表***/r/n编号/t 积分");

if(ca!=null)
for(int i=0; i<ca.length; i++)
if(ca[i]!=null)
System.out.println(ca[i].lineInfo());



/**
* 你的 add()
* @return
* @throws IOException
*/
public Customer input() throws IOException
Customer c = new Customer();
System.out.println("输入会员号:");
c.setCustNum(System.in.read());
System.out.println("输入会员积分:");
c.setPoints(System.in.read());

return c;




你要的大部分功能有了,自己拆补去吧!
参考技术D 为什么用数组而不用List。。。
Customer类:
int age;
String name;
Customer(int age,String name)

this.age = age;
this.name = name;


@Override
public String toString()

return "[" + name + "," + age + "]";


调用:
Customer[] customer = new customer[3];
customer[0] = new Customer (xxx,"XXX");

以上是关于java中创建一个客户类数组customer[] 怎么给数组中的变量赋值?的主要内容,如果未能解决你的问题,请参考以下文章

编程Customer.java:编写Customer类

在 Java 中创建对象数组

在 C# 中创建一个每次都可以具有不同属性的类或对象

如何在 Java 中创建锯齿状二维数组?

RubyRuby 类案例

用java语言定义一个客户要求类costomer,要求: