子類編程
A. 父類子類體現了什麼樣的編程思想
面向對象的編程思想,另外面向對象還有抽象、封裝、多態的特性
B. 編寫程序實現子類對父類構造方法的調用:定義一個水果類Fruit(父類)和蘋果類Apple(子類),
java">//父類
publicclassFruits{
publicFruits(){
System.out.println("Iamdefaultmethod.");
}
publicFruits(Stringargs){
System.out.println(args);
}
}
//子類1
publicclassAppleextendsFruits{
//默認含有不帶參數的構造方法,其中默認調用父類不帶參數的構造方法
publicstaticvoidmain(String[]args){
newApple();//輸出Iamdefaultmethod.
}
}
//子類2
publicclassAppleextendsFruits{
publicApple(){
//默認調用父類不含參數的構造方法
}
publicstaticvoidmain(String[]args){
newApple();//輸出Iamdefaultmethod.
}
}
//子類3
publicclassAppleextendsFruits{
publicApple(){
super();//還是調用不帶參數的構造方法
}
publicstaticvoidmain(String[]args){
newApple();//輸出Iamdefaultmethod.
}
}
//子類4
publicclassAppleextendsFruits{
publicApple(){
super("HHHHAAAA");//調用帶參數的父類構造方法
}
publicstaticvoidmain(String[]args){
newApple();//輸出HHHHAAAA
}
}
C. JAVA父類及子類的聲明編程題(編寫的代碼行後面要配有必要的注釋)
程序如下供你參考.說明:
1.0 你題目中只提供了兩門成績,英語和計算機,但是下面說三門.
如果是的話,自己添加上去吧.很好修改的.
2.0 如果有什麼不清楚的話,補充問題吧.或者可以給你留言.
3.0 希望你自己多看書把語言的基礎知識學好.
下面共四個java類文件.
/***Student.java **/
public class Student {
protected int studentID;
protected String name;
protected int englishScore;
protected int computerScore;
protected int totalScore;
public Student(int studentID, String name) {
super();
this.studentID = studentID;
this.name = name;
}
public Student(int studentID, String name, int englishScore,
int computerScore) {
super();
this.studentID = studentID;
this.name = name;
this.englishScore = englishScore;
this.computerScore = computerScore;
}
public int getStudentID() {
return studentID;
}
public void setStudentID(Integer studentID) {
this.studentID = studentID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
public int getComputerScore() {
return computerScore;
}
public void setComputerScore(int computerScore) {
this.computerScore = computerScore;
}
public int getTotalScore() {
return totalScore;
}
/**totalScore 沒有set 方法 */
@Override
public String toString() {
return "Student [studentID=" + studentID + ", name=" + name
+ ", englishScore=" + englishScore + ", computerScore="
+ computerScore + ", totalScore=" + totalScore + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (studentID != other.studentID)
return false;
return true;
}
/** @return 返回1 表示 大於,返回 0 表示等於, 返回-1表示小於 */
public int compare(Student other){
if(totalScore < other.totalScore)
return -1; //
if(totalScore == other.totalScore)
return 0;
return 1;
}
private void sum(){
totalScore = englishScore+computerScore;
}
/**計算評測成績 */
public double testScore(){
return (englishScore+computerScore)/2;
}
}
/***** StudentXW.java******/
public class StudentXW extends Student {
private String response;///責任
public StudentXW(int studentID, String name, String response) {
super(studentID, name);
this.response = response;
}
public StudentXW(int studentID, String name, int englishScore,
int computerScore, String response) {
super(studentID, name, englishScore, computerScore);
this.response = response;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
@Override
public double testScore(){
return (englishScore+computerScore)/2+3;
}
}
/*****StudentBZ.java****/
public class StudentBZ extends Student {
private String response;///責任
public StudentBZ(int studentID, String name, String response) {
super(studentID, name);
this.response = response;
}
public StudentBZ(int studentID, String name, int englishScore,
int computerScore, String response) {
super(studentID, name, englishScore, computerScore);
this.response = response;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
@Override
public double testScore(){
return (englishScore+computerScore)/2+5;
}
}
/*****測試類 TestStudent.java****/
/** Student ,StudentXW, 及StudentBZ測試類 */
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student(1,"one");
s1.setEnglishScore(80);
s1.setComputerScore(80);
StudentXW sx1 = new StudentXW(2,"xone","學習委員");
sx1.setEnglishScore(90);
sx1.setComputerScore(90);
StudentBZ sb1 = new StudentBZ(3,"bone","班長");
sb1.setEnglishScore(70);
sb1.setComputerScore(70);
System.out.println("學生 One 的評測成績為:"+s1.testScore());
System.out.println("學生 xone,學習委員的評測成績為:"+sx1.testScore());
System.out.println("學生 bone,班長的評測成績為:"+sb1.testScore());
Student [] studentArray = new Student[5];
studentArray[0] = new Student(101,"studentOne",60,60);
studentArray[1] = new Student(102,"studentTwo",65,65);
studentArray[2] = new Student(103,"studentThree",70,70);
studentArray[3] = new StudentXW(104,"studentXW",75,75,"學習委員");
studentArray[4] = new StudentBZ(104,"studentXW",80,80,"學習委員");
for(Student student:studentArray){
System.out.println("名字為:"+student.getName()+"的成績為:"+ student.testScore());
}
}
}
D. C 編程中 子類(派生類)能不能重寫父類(基類)的函數(方法),除了虛函數
override 方法重寫,而能夠方法重寫的方法需要使用virtual 關鍵字修飾,即虛方法,所以理論上除了虛函數,其他方法是不能重寫的,但可以通過new 修飾符隱藏父類方法,達到重寫父類非虛函數的效果。