美文网首页
第六章:面向对象高级——继承的应用。

第六章:面向对象高级——继承的应用。

作者: 孤意的学习笔记 | 来源:发表于2017-10-31 10:05 被阅读0次

实例要求

  • 定义一个整形数组类,要求包含构造方法,增加数据及输出数据成员方法,并利用书序实现动态内存分配。再次基础上定义出以下子类:
    • A、排序类 —— 实现排序
    • B、反转类 —— 实现数据反向存放

1、本实例主要采用的知识

集成的概念

2、具体内容

class Array{        // 表示数组
    private int temp[] ;        // 整型数组
    private int foot ;  // 定义添加位置
    public Array(int len){
        if(len>0){
            this.temp = new int[len] ;
        }else{
            this.temp = new int[1] ;    // 最少维持空间是1个
        }
    }
    public boolean add(int i){  // 增加元素
        if(this.foot<this.temp.length){ // 还有空间
            this.temp[foot] = i ;   // 增加元素
            this.foot ++ ;// 修改脚标
            return true ;
        }else{
            return false ;
        }
    }
    public int[] getArray(){
        return this.temp ;
    }
};
class SortArray extends Array{  // 排序类
    public SortArray(int len){
        super(len) ;
    }
    public int[] getArray(){    // 覆写方法
        java.util.Arrays.sort(super.getArray()) ;   // 排序操作
        return super.getArray() ;
    }
};
class ReverseArray extends Array{   // 反转操作类
    public ReverseArray(int len){
        super(len) ;
    }
    public int[] getArray() {
        int t[] = new int[super.getArray().length] ;    // 开辟一个新的数组
        int count = t.length - 1 ;
        for(int x=0 ;x<t.length;x++){
            t[count] = super.getArray()[x] ;    // 数组反转
            count-- ;
        }
        return t ;
    }
};
public class ArrayDemo{
    public static void main(String args[]){
        ReverseArray a = null ; // 声明反转类对象
        a = new ReverseArray(5) ;   // 开辟5个空间大小
        System.out.print(a.add(23) + "\t") ;
        System.out.print(a.add(21) + "\t") ;
        System.out.print(a.add(2) + "\t") ;
        System.out.print(a.add(42) + "\t") ;
        System.out.print(a.add(5) + "\t") ;
        System.out.print(a.add(6) + "\t") ;
        print(a.getArray()) ;

    }
    public static void print(int i[]){  // 输出数组内容
        for(int x=0;x<i.length;x++){
            System.out.print(i[x] + "、") ;
        }
    }
};
输出结果:
true    true    true    true    true    false    5、 42、 2、 21、 23、

排序类,直接修改使用的子类即可:


输出结果:
true    true    true    true    true    false    2、5、 21、 23、 42、

3、总结

应用了继承的各个概念,包括覆写、子类对象的实例化过程,排序操作,可以发现使用继承可以让代码得到重用。

相关文章

  • 面对对象高级编程

    面向对象高级编程: 面向对象编程:封装、继承和多态 面向对象高级编程:多重继承、定制类和元类

  • 2019-07-23

    java 基础 基础语法: 1-2天 编程思想: 面向对象 封装,继承,多态 高级应用 : 线程;IO; 网络 J...

  • 面向对象五

    面向对象高级二 总结 PHP 里的面向对象、继承方式 JS 里的继承方式 call(构造函数伪装) 和 proto...

  • 第六章:面向对象高级——继承的应用。

    实例要求 定义一个整形数组类,要求包含构造方法,增加数据及输出数据成员方法,并利用书序实现动态内存分配。再次基础上...

  • Lua单继承与多继承理解

    Lua不具有面向对象、继承等高级语言的属性,但是借助万能的table,仍然可以模拟实现面向对象继承的特性。这里先要...

  • 读JavaScript高级程序设计之面向对象

    面向对象(Object - Oriented)OO 是现在高级程序语言必备的技能,面向对象一般封装,继承,多态等特...

  • js红宝书笔记九 第八章 类与面向对象编程

    本文继续对JavaScript高级程序设计第四版 第八章 对象、类与面向对象编程 进行学习 一、继承 继承是面向对...

  • JavaScript之面向对象编程

    五、面向对象编程 目录:面向对象原型继承、面向对象class继承(ES6引入的) 1.面向对象原型继承 类:模板 ...

  • 扒一扒所谓的面向对象

    面向对象是很多高级程序程序设计语言的核心。面向对象的标志就是类、对象、继承、派生等。js严格意义上来讲并不是面向对...

  • javascript高级程序设计(第6章)-- 面向对象的程序设

    第六章:面向对象的程序设计 本章内容: 理解对象属性 理解并创建对象 理解继承 ECMA-262把对象定义为:"无...

网友评论

      本文标题:第六章:面向对象高级——继承的应用。

      本文链接:https://www.haomeiwen.com/subject/ghpvpxtx.html