Example: Inherited apps:

Source: Internet
Author: User

Defines an array class that requires a constructor method, adds data, outputs a member method of data, and uses an array to implement dynamic memory allocation.

On this basis, the following subclasses are defined:

1) Sorting classes, implementing sorting.

2) Reverse class for data reverse storage.

Analysis

This program requires the array to implement dynamic memory allocation , that is, the size of the array inside the program is determined by the outside. That is, according to the object instantiation: Class object =new Class (parameters, parameters,,);

Therefore , you should initialize the array of classes in the constructor method (the array size is initialized by the parameters in the constructor method). )。

Each time you add data, you should determine whether the contents of the array are full. If it is full, it cannot be increased.

In addition, if you want to add data, you will need a pointer to the subscript that can be inserted, to record the location of the insertion.

classarray{//represents an array    Private intTemp[];//array of integral type    Private intfoot;//Define Add location     PublicArray (intLen) {        if(len>0){             This. temp =New int[Len]; }Else{             This. temp =New int[1];//minimum of 1 space to maintain        }    }     Public BooleanAddinti) {//add Element        if( This.foot< This. temp.length) {//there's room .             This. temp[foot] = i;//add Element             This. Foot + +;//Modify the foot label            return true ; }Else{            return false ; }    }     Public int[] GetArray () {return  This. temp; }};classSortarrayextendsarray{//Sort Class     PublicSortarray (intLen) {        Super(len); }     Public int[] GetArray () {//Overwrite MethodJava.util.Arrays.sort (Super. GetArray ());//Sort Operations        return Super. GetArray (); }};classReversearrayextendsarray{//Inverse Action Class     PublicReversearray (intLen) {        Super(len); }     Public int[] GetArray () {intT[] =New int[Super. GetArray (). length];//open up a new array        intCount = T.length-1 ;  for(intx=0; x<t.length;x++) {T[count]=Super. GetArray () [x];//Array Inversioncount-- ; }        returnT; }}; Public classarraydemo{ Public Static voidMain (String args[]) {//Reversearray a = null; //declaring an inverted class object//a = new Reversearray (5); //opens up 5 space sizesSortarray A =NULL ; A=NewSortarray (5) ; System.out.print (A.add (+ "\ T") ; System.out.print (A.add (+ "\ T") ; System.out.print (A.add (2) + "\ T") ; System.out.print (A.add () + "\ T") ; System.out.print (A.add (5) + "\ T") ; System.out.print (A.add (6) + "\ T") ;    Print (A.getarray ()); }     Public Static voidPrintintI[]) {//Output Array Contents         for(intx=0;x<i.length;x++) {System.out.print (i[x]+ ",") ; }    }};

Takes advantage of inheritance, inherits the methods of the parent class, uses overrides, and invokes the parent class method property with the Super keyword.

Example: Inherited apps:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.