當前位置:首頁 » 編程軟體 » 子類編程

子類編程

發布時間: 2022-12-27 15:22:53

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 修飾符隱藏父類方法,達到重寫父類非虛函數的效果。

熱點內容
php辦公系統 發布:2025-07-19 03:06:35 瀏覽:900
奧德賽買什麼配置出去改裝 發布:2025-07-19 02:53:18 瀏覽:42
請與網路管理員聯系請求訪問許可權 發布:2025-07-19 02:37:34 瀏覽:189
ipad上b站緩存視頻怎麼下載 發布:2025-07-19 02:32:17 瀏覽:844
phpcgi與phpfpm 發布:2025-07-19 02:05:19 瀏覽:527
捷達方向機安全登錄密碼是多少 發布:2025-07-19 00:57:37 瀏覽:693
夜魔迅雷下載ftp 發布:2025-07-19 00:39:29 瀏覽:99
增值稅票安全接入伺服器地址 發布:2025-07-19 00:20:45 瀏覽:486
solidworkspcb伺服器地址 發布:2025-07-18 22:50:35 瀏覽:823
怎麼在堆疊交換機里配置vlan 發布:2025-07-18 22:42:35 瀏覽:630