当前位置:首页 » 编程软件 » 子类编程

子类编程

发布时间: 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 浏览:694
夜魔迅雷下载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