通过代码实例跟我学Java面向对象编程技术及应用提高(第7部分)

时间:2025-05-15

通过代码实例跟我学Java面向对象编程技术及应用提高(第7部分)

1.1 通过代码实例跟我学Java面向对象编程技术及应用提高(第7部分)

1.1.1 继承中的this和super的正确用法

1、如何在子类初始化从基类中所继承来的成员——super()/super和this()/this的应用 (1)直接使用基类中的成员属性名,将会出现错误

(2)此时应该采用super()委托调用基类的构造方法。

(3)代码示例

package com.px1987.j2seexample;

public class DoctorStudentInfo extends StudentInfo {

private int doctorType; public DoctorStudentInfo() { }

public DoctorStudentInfo(String name,int doctorType) {

this("张三",1);

通过代码实例跟我学Java面向对象编程技术及应用提高(第7部分)

/*

studentName=name;

*/ super(name);

}

public void studyCourse(String courseContent){ //override(覆盖,重写) }

super.studyCourse(courseContent); //String infoText="ID号为"+studentID+"并且";

System.out.println("姓名为"+getStudentName()+"的学生正在学习"+

courseContent+"课程");

this.doctorType=doctorType;

/* 下面的各个方法是继承

public void trainSelf(String item){ // overLoad(重载) }

public void trainSelf(String item,Date time){ }

System.out.println("姓名为"+studentName+"的学生正在复习"+

item+"课程");

System.out.println("姓名为"+studentName+"的学生正在复习"+

item+"课程");

*/ }

public void doResearch(String item){ }

System.out.println("姓名为"+getStudentName()+"的学生正在进行科学研究"+

item+"项目");

通过代码实例跟我学Java面向对象编程技术及应用提高(第7部分)

2、StudentInfo类的代码示例 package com.px1987.j2seexample; public class StudentInfo {

private String studentName; private int studentID; private int studentAge; private String major;

public StudentInfo(String studentName,int studentID ) {

this.studentName=studentName; this.studentID=studentID;

通过代码实例跟我学Java面向对象编程技术及应用提高(第7部分)

}

public StudentInfo() { }

public void studyCourse(String courseContent){ }

public void trainSelf(String item){ }

public void trainSelf(String itemOne,String itemTwo){ }

System.out.println("姓名为"+studentName+"的学生正在复习"+

itemOne+"课程");

System.out.println("姓名为"+studentName+"的学生正在复习"+

item+"课程");

System.out.println("姓名为"+studentName+"的学生正在学习"+

courseContent+"课程");

this("",0);

3、学员练习:在前面的Circle类中的成员方法中正确地应用super和this。 package com.px1987.j2seexample; public class Circle extends Point { private int r;

private static final float PI=(float)3.1415926;

public Circle() {

this(0,0,100);

通过代码实例跟我学Java面向对象编程技术及应用提高(第7部分)

}

public Circle(int newX, int newY,int newR) { }

public void draw(){ }

public float getGround(){ }

public float getArea(){ }

float area; area=PI*r*r; return area; float length; length=2*PI*r; return length; super.draw();

System.out.println("扩展的功能");

super(newX, newY); r=newR;

4、学员练习:

在前面的TeacherInfo(讲师)类和Professor(教授)类中的成员方法中正确地应用super和this。

1.1.2 Java中的接口及应用实例 1、 什么是接口

(1)在项目中添加一个名称为StudyInterface的接口

通过代码实例跟我学Java面向对象编程技术及应用提高(第7部分)

(2)接口也可以继承——在添加一个子接口

2、熟悉接口定义的基本语法规则

(1)接口中的成员属性必须要进行初始化赋值

通过代码实例跟我学Java面向对象编程技术及应用提高(第7部分)

正确赋值后的结果示图

(2)interface中的数据成员也变为了

public,static,final

通过代码实例跟我学Java面向对象编程技术及应用提高(第7部分)

(3)interface中的方法无需声明,都会自动设为了public。

3、为什么要提供接口

4、如何编程接口 (1)利用类来实现

(2)创建出实现接口的类定义

通过代码实例跟我学Java面向对象编程技术及应用提高(第7部分)

(3)重写所有的方法

5、接口和抽象类的差别 (1)定义:部分和全部

(2)应用:都是达到实现重用的目的!

继承-----紧密关联基类(紧密藕合)、导致受限制 实现-----实现类与接口不会出现紧密关联

由于接口重点在于规范“功能”实现的要求,抽象类重点在于对象的“共性”。因此抽象类主要用于关系密切的对象,而接口最适合为不相关的类提供通用功能。 6、Interface还有一重要功能是产生常量集

由于interface中的数据成员会自动成为public static final,所以如下的接口: public interface Months{

通过代码实例跟我学Java面向对象编程技术及应用提高(第7部分)

int JANURAUY=1 …… 此处隐藏:1116字,全部文档内容请下载后查看。喜欢就下载吧 ……

通过代码实例跟我学Java面向对象编程技术及应用提高(第7部分).doc 将本文的Word文档下载到电脑

    精彩图片

    热门精选

    大家正在看

    × 游客快捷下载通道(下载后可以自由复制和排版)

    限时特价:7 元/份 原价:20元

    支付方式:

    开通VIP包月会员 特价:29元/月

    注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
    微信:fanwen365 QQ:370150219