
正文
java代码定义客户类 java编写客户端程序
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用java语言定义一个客户要求类costomer,要求:
public class Costomer {
private long id;
private String name;
private int age;
private boolean sex;
private String phone;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String toString() {
return "[编号:" + id + ", 姓名:" + name + ", 年龄:" + age + ", 性别:" + sex
+ ", 联系电话:" + phone + "]";
}
public Costomer(long id, String name, int age, boolean sex, String phone) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.phone = phone;
}
public void print() {
System.out.println(toString());
}
}
相关问答
Q1: java中创建一个客户类数组customer[] 怎么给数组中的变量赋值?
先给数组元素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;ict.length;i++){
ct[i] = new customer();//实例化customer对象
ct[i].a1 = 2;//给customer类的a1属性赋值
}
Q2: JAVA编程题
这么明确java代码定义客户类了。根据问题可以直接写出来java代码定义客户类了!
Q3: 用java编一个程序,定义客户类Customer(有姓名年龄手机号码,帐号,密码五个字段)创建两个
附件是完整的eclipse/MyEclipse工程包,可以导入自行测试;
或许比别人的实现要长,主要是基于可扩展性的考虑。
// Entity Class
public class Student implements Comparable{
private int no;
private String name;
private int age;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(int no, String name, int age) {
super();
this.no = no;
this.name = name;
this.age = age;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [no=" + no +", name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(Object o) {
Student stu = (Student) o ;
int a = no - stu.getNo();
// TODO Auto-generated method stub
return a;
}
}
// Entity Factory
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
public class StudentFactory {
static String name[] = {"Leon","Matthew","Porter","Pope","Taylor"};
static int index = 1;
static Random random = new Random();
/**
* Create a new student with name and age randomly in numeric sequence
*/
public static Student create(){
Student student = new Student();
student.setNo((Integer)getRandomNo(1, 60).toArray()[index-1]);
student.setName(name[(Integer)getRandomNo(0, 5).toArray()[index-1]]);
// setting age between [18,22]
student.setAge(new Random().nextInt(18)%(22-18+1) + 18);
index++;
return student;
}
/**
* Create n students
*
*/
public static ListStudent create(int n){
ListStudent studentList = new ArrayListStudent();
for(int i=0; i n; i++){
Student student = create();
studentList.add(student);
}
return studentList;
}
/**
* 取得在[min,max]范围内5个不重复的随机数
*/
public static SetInteger getRandomNo(int min,int max){
Random random = new Random();
SetInteger set = new HashSetInteger();
if(max - min 5){
System.out.println("Error : The scope 5");
return null;
}
while (set.size() 5) {//the Elements of set is unique
int number=random.nextInt(max)%(max-min+1) + min;//
set.add(number);
}
return set;
}
public static void main(String[] args) {
StudentFactory studentFactory = new StudentFactory();
ListStudent studentList = studentFactory.create(5);
for(Student student: studentList){
System.out.println(student);
}
}
}
// requirement implementation
import java.util.Arrays;
import java.util.List;
public class Task {
/**
* (1) 将学生按学号按由小到大顺序输出
*/
public void sort(ListStudent studentList){
Object[] studentArr = studentList.toArray();
Arrays.sort(studentArr);
for(Object student : studentArr){
System.out.println(student);
}
}
/**
* (2) 给所有学生年龄加n
*/
public void agePlus(ListStudent studentList, int n){
for(Student student : studentList){
student.setAge(student.getAge() + n );
System.out.println(student);
}
}
/**
* (3) 统计大于age岁的学生人数
*/
public int total(ListStudent studentList,int age){
int totalNo = 0;
for(Student student : studentList){
if(student.getAge() age){
totalNo ++ ;
}
}
return totalNo;
}
public static void main(String[] args) {
ListStudent studentList = StudentFactory.create(5);
Task task = new Task();
System.out.println("-----sort by no-----");
task.sort(studentList);
System.out.println("-----All students' age plus 1------");
task.agePlus(studentList, 1);
System.out.println("-----Total number of age 20 : " + task.total(studentList, 20));
}
}
关于java代码定义客户类和java编写客户端程序的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。




