系统粉 > 电脑问答 > 其他问答

已解决JAVA构造类问题

提问者:不吵不闹o_o  |  浏览次  |  提问时间:2017-03-01  |  回答数量:4

JAVA构造类问题 构造1个类A 声明其属性和构造函数B 为每一个属性设置GET和SET方法C 按要求写出一些特定方法D 将该类设置到指定包中E 在该类中写出MAIN函数测试以上编写的方法F 将该类打包成JAR包 有那位大大回答下小弟的初级问题啊,实在是不会,以前女同学要我帮忙问下人。。。

已有4条答案
尖叫小新

尖叫小新

回答数:99  |  被采纳数:1

希望对你有所帮助
package com.neo.project;import java.util.ArrayList;import java.util.Collections;public class StudentModel {private String id;private String name;private Integer score;public void setId(String id) {this.id = id;}public String getId() {return id;}public void setName(String name) {this.name = name;}public String getName() {return name;}public void setScore(Integer score) {this.score = score;}public Integer getScore() {return score;}public ArrayList<Integer> sortByScore(ArrayList<Integer> scoreList) {Collections.sort(scoreList);return scoreList;}}复制代码

package com.neo.project;import java.util.ArrayList;public class TestStudent {private String[] studentIds = {"22", "11", "66", "44"}; private String[] studentNames = {"Neo", "Yen", "Roger", "Danny"};private Integer[] studentScores = {98, 58, 75, 41};private ArrayList<StudentModel> studentList = new ArrayList<StudentModel>();public TestStudent() {createStudentModel();outputStudentModel();sortByScore();}private void sortByScore() {ArrayList<Integer> scoreList = new ArrayList<Integer> ();if (isEmptyList(studentList)) {return;}for (int i = 0; i < studentList.size(); i++) {Integer score = studentList.get(i).getScore();scoreList.add(score);}if (isEmptyList(scoreList)) {return;}ArrayList<Integer> sortedList = (new StudentModel()).sortByScore(scoreList);System.out.println("\nSort by score:");for (int i = 0; i < sortedList.size(); i++) {System.out.println(sortedList.get(i));}}private void outputStudentModel() {if (isEmptyList(studentList)) {return;}for (int i = 0; i < studentList.size(); i++) {StudentModel model = studentList.get(i);String id = model.getId();String name = model.getName();Integer score = model.getScore();System.out.println("id = " + id + ", name = " + name + ", score = " + score);}}private void createStudentModel() {StudentModel studentModel = null;for (int i = 0; i < studentIds.length; i++) {studentModel = new StudentModel();String id = studentIds[i];String name = studentNames[i];Integer score = studentScores[i];studentModel.setId(id);studentModel.setName(name);studentModel.setScore(score);studentList.add(studentModel);}}private boolean isEmptyList(ArrayList list) {if (list == null || list.size() == 0) {return true;}return false;}public static void main(String[] args) {new TestStudent();}}复制代码
2017-03-01 18:58:25
赞 9
yt2001728

yt2001728

回答数:94  |  被采纳数:115

如果你需要构造类的话 可以这样写
package com.neo.test;public class StaticStudentModel {private String id;private String name;public StaticStudentModel(String id, String name) {if (isEmpty(id)) {System.out.println("student id should not be empty.");return;}if (isEmpty(name)) {System.out.println("student name should not be empty.");return;}this.id = id;this.name = name;}private boolean isEmpty(String str) {if (str == null || str.trim().length() == 0) {return true;}return false;}}复制代码
package com.neo.test;public class CustomizedStudentModel extends StaticStudentModel {private Integer score;public CustomizedStudentModel(String id, String name, Integer score) {this(id, name);this.score = score;}public CustomizedStudentModel(String id, String name) {super(id, name);}public static void main(String[] args) {}}复制代码
2017-03-01 21:22:24
赞 9
铃喵果酱

铃喵果酱

回答数:82  |  被采纳数:119

package cn.lusha.y09m03; //D 将该类设置到指定包中

public class Test {

// 声明其属性
private String str;

private int integer;

// 声明其构造函数
public Test() {

}

public void specialMethod(){
// C 按要求写出一些特定方法
}

// 在该类中写出MAIN函数测试以上编写的方法
public static void main(String[] args) {
new Test().specialMethod();
}

/**
*为每一个属性设置GET和SET方法
* @return
*/
public String getStr() {
return str;
}

public void setStr(String str) {
this.str = str;
}

public int getInteger() {
return integer;
}

public void setInteger(int integer) {
this.integer = integer;
}
}
2017-03-01 23:23:03
赞 8
remy_鈺婷

remy_鈺婷

回答数:146  |  被采纳数:21

3楼的大大写得够清楚 谢谢咯研究下先~~
2017-03-01 20:51:32
赞 14
相关问答
最新其他问答
解决方法