子类编程
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 修饰符隐藏父类方法,达到重写父类非虚函数的效果。