package xiao5;import java.util.Random;abstract class Person { int age;
char sex;
}class ReportCard { private int English;
private int math; void setEnglish(int English) {
this.English = English;
} void setMath(int math) {
this.math = math;
} int getEnglish() {
return English;
} int getMath() {
return math;
}
}class Student extends Person { int grade;
ReportCard RC = new ReportCard(); void setScore(int a, int b) {
RC.setEnglish(a);
RC.setMath(b);
}
}class OurClass { Random r = new Random();
Student S = new Student();
ReportCard RC = new ReportCard();
int[] count = new int[10];
int i; void generateReportCard() {
int c, d;
c = r.nextInt(51) + 50;
d = r.nextInt(51) + 50;
S.setScore(c, d);
} void sort() {
int q;
q = RC.getEnglish() + RC.getMath(); }
}public class Main { public static void main(String[] args) {
OurClass OC = new OurClass();
ReportCard RC = new ReportCard();
OC.generateReportCard();
System.out.println(RC.getEnglish() + " " + RC.getMath());
}
}
char sex;
}class ReportCard { private int English;
private int math; void setEnglish(int English) {
this.English = English;
} void setMath(int math) {
this.math = math;
} int getEnglish() {
return English;
} int getMath() {
return math;
}
}class Student extends Person { int grade;
ReportCard RC = new ReportCard(); void setScore(int a, int b) {
RC.setEnglish(a);
RC.setMath(b);
}
}class OurClass { Random r = new Random();
Student S = new Student();
ReportCard RC = new ReportCard();
int[] count = new int[10];
int i; void generateReportCard() {
int c, d;
c = r.nextInt(51) + 50;
d = r.nextInt(51) + 50;
S.setScore(c, d);
} void sort() {
int q;
q = RC.getEnglish() + RC.getMath(); }
}public class Main { public static void main(String[] args) {
OurClass OC = new OurClass();
ReportCard RC = new ReportCard();
OC.generateReportCard();
System.out.println(RC.getEnglish() + " " + RC.getMath());
}
}
package a;import java.util.HashMap;
import java.util.Map;import java.util.Random;abstract class Person { int age;
char sex;
}class ReportCard { private int English;
private int math; void setEnglish(int English) {
this.English = English;
} void setMath(int math) {
this.math = math;
} int getEnglish() {
return English;
} int getMath() {
return math;
}
}class Student extends Person { int grade;
ReportCard RC = new ReportCard(); void setScore(int a, int b) {
RC.setEnglish(a);
RC.setMath(b);
}
public ReportCard getReportCard(){
return RC;
}
}class OurClass { Random r = new Random();
Student S = new Student();
ReportCard RC = new ReportCard();
int[] count = new int[10];
int i;
public Student getStudent(){
return S;
} void generateReportCard() {
int c, d;
c = r.nextInt(51) + 50;
d = r.nextInt(51) + 50;
S.setScore(c, d);
} void sort() {
int q;
q = RC.getEnglish() + RC.getMath(); }
}public class Main { public static void main(String[] args) {
OurClass OC = new OurClass();
ReportCard RC = OC.getStudent().getReportCard();
OC.generateReportCard();
System.out.println(RC.getEnglish() + " " + RC.getMath());
}
}帮你改下
ReportCard RC = new ReportCard(); void setScore(int a, int b) {
RC.setEnglish(a);
RC.setMath(b);
}
}这个对象RC 跟你main里面的不同,
int c, d;
c = r.nextInt(51) + 50;
d = r.nextInt(51) + 50;
S.setScore(c, d,rc);
}class Student extends Person { int grade;
ReportCard RC
void setScore(int a, int b,ReportCard) {this.Rc=rc
RC.setEnglish(a);
RC.setMath(b);
}
} OurClass OC = new OurClass(Rc);它们指向的是不同的对象
当你用new ReporteCard()时,新建了一个对象,
ReportCard RC = new ReportCard();
他的預設值是 0你的 OurClass 想做什麼? 不是很明確,如果是想處理 ReportCard 那應該是把ReportCard當參數傳入,
而不是自己又產生一個新的。然後不管是當參數傳入OurClass 去修改分數或是自己設定分數,
都要有RC.setEnglish(100); RC.setMath(30); 這樣給定分數之後,分數才會改變。lz的基礎知識要再加強...