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

通过代码实例跟我学Java面向对象编程技术及应用提高(第7部分)
通过代码实例跟我学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() {

this("张三",1);

}

public DoctorStudentInfo(String name,int doctorType) {

/*

studentName=name;

*/ super(name);

this.doctorType=doctorType;

}

public void studyCourse(String courseContent){ //override(覆盖,重写)super.studyCourse(courseContent);

//String infoText="ID号为"+studentID+"并且";

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

courseContent+"课程");

}

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

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

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

item+"课程");

}

public void trainSelf(String item,Date time){

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

item+"课程");

}

*/

public void doResearch(String item){

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

}

}

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;

}

public StudentInfo() {

this("",0);

}

public void studyCourse(String courseContent){

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

courseContent+"课程");

}

public void trainSelf(String item){

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

item+"课程");

}

public void trainSelf(String itemOne,String itemTwo){

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

itemOne+"课程");

}

}

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);

}

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

super(newX, newY);

r=newR;

}

public void draw(){

super.draw();

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

}

public float getGround(){

float length;

length=2*PI*r;

return length;

}

public float getArea(){

float area;

area=PI*r*r;

return area;

}

}

4、学员练习:

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

1.1.2Java中的接口及应用实例

1、什么是接口

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

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

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

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

正确赋值后的结果示图

(2)interface中的数据成员也变为了public,static,final

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

3、为什么要提供接口

4、如何编程接口

(1)利用类来实现

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

(3)重写所有的方法

5、接口和抽象类的差别

(1)定义:部分和全部

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

继承-----紧密关联基类(紧密藕合)、导致受限制

实现-----实现类与接口不会出现紧密关联

由于接口重点在于规范“功能”实现的要求,抽象类重点在于对象的“共性”。因此抽象类主要用于关系密切的对象,而接口最适合为不相关的类提供通用功能。

6、Interface还有一重要功能是产生常量集

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

public interface Months{

int JANURAUY=1, FEBRUARY=2, MARCH=3;

}

然后要用的话,就直接Months. JANURAUY*10类似的直接用就行了

相关主题
相关文档
最新文档