
正文
java属性类代码 java基本属性
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java程序设计,求代码 1.定义学生类,学生类有学号,姓名,语文成绩,数学成绩的属性和有参的构造
import java.util.Comparator;
public class Student implements ComparableStudent {
private int no;
private String name;
private String sex;
private int roomNo;
private double score;
public Student(int no, String name, String sex, int roomNo, double score) {
this.no = no;
this.name = name;
this.sex = sex;
this.roomNo = roomNo;
this.score = score;
}
public Student(int no, String name, String sex, double score) {
this.no = no;
this.name = name;
this.sex = sex;
this.score = score;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getRoomNo() {
return roomNo;
}
public void setRoomNo(int roomNo) {
this.roomNo = roomNo;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public int compareTo(Student o) {
if (this.no o.no) return 1;
else if (this.no o.no) return -1;
else return 0;
}
@Override
public String toString() {
return "Student{" +
"no=" + no +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", roomNo=" + roomNo +
", score=" + score +
'}';
}
}
//性别比较器类
class SexComparator implements ComparatorStudent {
@Override
public int compare(Student o1, Student o2) {
if (o1.getSex().compareTo(o2.getSex()) 0) return 1;
else if (o1.getSex().compareTo(o2.getSex()) 0) return -1;
else return 0;
}
}
//寝室号比较器类
class RoomNoComparator implements ComparatorStudent {
@Override
public int compare(Student o1, Student o2) {
if (o1.getRoomNo() o2.getRoomNo()) return 1;
else if (o1.getRoomNo() o2.getRoomNo()) return -1;
else return 0;
}
}
//入学成绩比较器类
class ScoreComparator implements ComparatorStudent {
@Override
public int compare(Student o1, Student o2) {
if (o1.getScore() o2.getScore()) return 1;
else if (o1.getScore() o2.getScore()) return -1;
else return 0;
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student(1, "jack", "boy", 90);
Student s2 = new Student(5, "jack", "boy", 90);
Student s3 = new Student(4, "jack", "boy", 90);
Student s4 = new Student(2, "jack", "boy", 90);
ListStudent studentList=new ArrayList();
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
Collections.sort(studentList);
System.out.println(Arrays.toString(studentList.toArray()));
}
}
相关问答
Q1: java 中 父类写初始化代码 反射获取类中的所有属性 并按照某种逻辑赋值(我是为了初始化Spring中的Bean)
这种想法是不可能实现的,父类中是获取不到子类的属性的,你需要换一个思路解决问题。
Q2: 求java代码,属性P有3个数据成员,A,B,C都可修改P,要求属性P的某个数据成员变化时,ABC都要做相应动作
这些术语不规范太模糊
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class P {
private String p1;
private String p2;
private String p3;
private ListHandle handleList = new ArrayList();
public void addHandler(Handle h) {
handleList.add(h);
}
public void changeP1(String p1) {
this.p1 = p1;
Iterator it = handleList.iterator();
while(it.hasNext()) {
Handle handle = (Handle) it.next();
handle.handleP1Modified();
}
};
public void changeP2(String p2) {
this.p2 = p2;
Iterator it = handleList.iterator();
while(it.hasNext()) {
Handle handle = (Handle) it.next();
handle.handleP2Modified();
}
};
public void changeP3(String p3) {
this.p3 = p3;
Iterator it = handleList.iterator();
while(it.hasNext()) {
Handle handle = (Handle) it.next();
handle.handleP3Modified();
}
};
public static void main(String[] args) {
P p = new P();
//添加模块
A a = new A();
B b = new B();
p.addHandler(a);
p.addHandler(b);
//改变p
p.changeP1("3");
p.changeP2("4");
p.changeP3("5");
}
}
interface Handle {
public void handleP1Modified();
public void handleP2Modified();
public void handleP3Modified();
}
class A implements Handle {
@Override
public void handleP1Modified() {
System.out.println("p1 is changing, A is dealing with it now ......");
}
@Override
public void handleP2Modified() {
System.out.println("p2 is changing, A is dealing with it now ......");
}
@Override
public void handleP3Modified() {
System.out.println("p3 is changing, A is dealing with it now ......");
}
}
class B implements Handle {
@Override
public void handleP1Modified() {
System.out.println("p1 is changing, B is dealing with it now ......");
}
@Override
public void handleP2Modified() {
System.out.println("p2 is changing, B is dealing with it now ......");
}
@Override
public void handleP3Modified() {
System.out.println("p3 is changing, B is dealing with it now ......");
}
}
java属性类代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java基本属性、java属性类代码的信息别忘了在本站进行查找喔。






