
正文
Java创建两个类源代码 编一个java程序有两个类
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java编译的问题 两个类在同一个包中 源代码如下
javac *.java
这样试一下。
如果你这两个类,A引用了B类
那么就要选编译出B类。
如果相互引用了的话,编译那个都是不成功的,就用
javac *.java
相关问答
Q1: 用JAVA创建两个类,一个学生类,一个班级类,怎样实现班级类中含有学生类的对象数组??
通常实体类中Java创建两个类源代码,我们会创建子类Java创建两个类源代码的List对象在父类中,而非用数组,即在班级类中添加如下:
private List学生类 学生类s = new ArrayList学生类();
public get学生类s() { return 班级ID; }
public set学生类s(List学生类 学生类s) { this.学生类s = 学生类s; }
// or replace to your get/set method
不过最好的处理方式不是在班级中创建学生类的对象集,而是和数据库一样,在学生类中添加班级的ID,
private String 班级ID;
public get班级ID() { return 班级ID; }
public set班级ID(String 班级ID) { this.班级 = 班级ID; }
或者:
private 班级 cls;
public getCls() { return cls; }
public setCls(班级 c) { this.cls = c; }
Q2: 高分求两个简单的JAVA设计源代码
上面 wuzhikun12同学写的不错,但我想还不能运行,并且还不太完善。我给个能运行的:(注意:文件名为:Test.java)
//要实现对象间的比较,就必须实现Comparable接口,它里面有个compareTo方法
//Comparable最好使用泛型,这样,无论是速度还是代码量都会减少
@SuppressWarnings("unchecked")
class Student implements ComparableStudent{
private String studentNo; //学号
private String studentName; //姓名
private double englishScore; //英语成绩
private double computerScore; //计算机成绩
private double mathScore; //数学成绩
private double totalScore; //总成绩
//空构造函数
public Student() {}
//构造函数
public Student(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
this.studentNo = studentNo;
this.studentName = studentName;
this.englishScore = englishSocre;
this.computerScore = computerScore;
this.mathScore = mathScore;
}
//计算总成绩
public double sum() {
this.totalScore = englishScore+computerScore+mathScore;
return totalScore;
}
//计算评测成绩
public double testScore() {
return sum()/3;
}
//实现compareTO方法
@Override
public int compareTo(Student student) {
double studentTotal = student.getTotalScore();
return totalScore==studentTotal?0:(totalScorestudentTotal?1:-1);
}
//重写toString方法
public String toString(){
return "学号:"+this.getStudentNo()+" 姓名:"+this.getStudentName()+" 英语成绩:"+this.getEnglishScore()+" 数学成绩:"+this.getMathScore()+" 计算机成绩:"+this.getComputerScore()+" 总成绩:"+this.getTotalScore();
}
//重写equals方法
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(!(obj instanceof Student)){
return false;
}
Student student = (Student)obj;
if(this.studentNo.equals(student.getStudentName())) { //照现实来说,比较是不是同一个学生,应该只是看他的学号是不是相同
return true;
} else {
return false;
}
}
/*以下为get和set方法,我个人认为,totalScore的set的方法没必要要,因为它是由其它成绩计算出来的
在set方法中,没设置一次值,调用一次sum方法,即重新计算总成绩
*/
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
sum();
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
sum();
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
sum();
}
public double getComputerScore() {
return computerScore;
}
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
sum();
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
sum();
}
public double getTotalScore() {
return totalScore;
}
}
//Student子类学习委员类的实现
class StudentXW extends Student {
//重写父类Student的testScore()方法
@Override
public double testScore() {
return sum()/3+3;
}
public StudentXW() {}
//StudentXW的构造函数
public StudentXW(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//Student子类班长类的实现
class StudentBZ extends Student {
//重写父类Student的testScore()方法
@Override
public double testScore() {
return sum()/3+5;
}
public StudentBZ() {}
//StudentXW的构造函数
public StudentBZ(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//测试类
public class Test {
public static void main(String[] args) {
//生成若干个student类、StudentXW类、StudentBZ类
Student student1 = new Student("s001","张三",70.5,50,88.5);
Student student2 = new Student("s002","李四",88,65,88.5);
Student student3 = new Student("s003","王五",67,77,90);
StudentXW student4 = new StudentXW("s004","李六",99,88,99.5);
StudentBZ student5 = new StudentBZ("s005","朱漆",56,65.6,43.5);
Student[] students = {student1,student2,student3,student4,student5};
for(int i = 0 ; istudents.length; i++){
double avgScore = students[i].testScore();
System.out.println(students[i].getStudentName()+"学生的评测成绩为:"+ avgScore+"分");
}
}
}
运行结果为:
张三学生的评测成绩为:69.66666666666667分
李四学生的评测成绩为:80.5分
王五学生的评测成绩为:78.0分
李六学生的评测成绩为:98.5分
朱漆学生的评测成绩为:60.03333333333333分
Q3: 用java写两个类?
以下是实现要求的Person和Student类的示例代码:csharpCopy codepublic class Person { private Student student; // Person类的成员变量 public Person(String studentName, int studentAge) { // 构造方法中完成必要的初始化内容 this.student = new Student(studentName, studentAge); } public void doSomething() { // Person类的成员方法中调用Student类型成员变量的一个成员方法 this.student.study(); }}public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public void study() { System.out.println(this.name + "正在学习"); }}public class Main { public static void main(String[] args) { Person person = new Person("张三", 18); person.doSomething(); // 对Person类的成员方法的调用 }}以上代码中,Person类中有一个Student类型的成员变量,未初始化,构造方法中完成了该成员变量的初始化,Person类的某个成员方法中调用了Student类型成员变量的一个成员方法,而在主方法中完成了对该成员方法的调用
Java创建两个类源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于编一个java程序有两个类、Java创建两个类源代码的信息别忘了在本站进行查找喔。







