當前位置:係統粉 > 電腦問答 > 其他問答 > JAVA構造類問題

JAVA構造類問題

提問者:不吵不鬧o_o  |  瀏覽 次  |  提問時間:2017-03-01  |  回答數量:4

JAVA構造類問題 構造1個類A 聲明其屬性和構造函數B 為每一個屬性設置GET和SET方法C 按要求寫出一些特定方法D 將該類設置到指定包中E 在該類中寫出MAIN函數測試以上編寫的方法F 將該類打包成JAR包 有那位大大回答下小弟的初級問題啊,實在是不會,以前女同學要我幫忙問下人。。。

已有4條答案
尖叫小新

尖叫小新

回答數:99  |  被采納數:1

2017-03-01 18:58:25
希望對你有所幫助
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();}}複製代碼
讚 9
yt2001728

yt2001728

回答數:94  |  被采納數:115

2017-03-01 21:22:24
如果你需要構造類的話 可以這樣寫
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) {}}複製代碼
讚 9
鈴喵果醬

鈴喵果醬

回答數:82  |  被采納數:119

2017-03-01 23:23:03
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;
}
}
讚 8
remy_鈺婷

remy_鈺婷

回答數:146  |  被采納數:21

2017-03-01 20:51:32
3樓的大大寫得夠清楚 謝謝咯研究下先~~
讚 14
解決方法
版權信息

Copyright @ 2011 係統粉 版權聲明 最新發布內容 網站導航