Java internal class simulation achieves multi-Inheritance

Source: Internet
Author: User

Package com. test;

/**
* Rubber
**/
Public abstract class eraser {
Public abstract void erase ();
}
Package com. test;
/**
* Pencil
**/
Public abstract class penpencil {
Public abstract void wirte ();
}

Package com. test;
/**
* Make a functional class that includes both pencil and eraser.
* (It can be fully implemented using interfaces. Here we only assume that this situation is used to simulate multi-inheritance)
**/
Public class pencilwitheraser {

Private mypenpencil penpencil = new mypenpencil ();
Private myeraser eraser = new myeraser ();

/**
*
* Inherits the pencil Function
*
**/
Private class mypenpencil extends penpencil {

@ Override
Public void wirte (){
System. Out. println ("use to wirte ");
}


}
/* Write a class with both pen and rubber Functions

**/

Private class myeraser extends eraser {

@ Override
Public void erase (){
System. Out. println ("use to erase ");
}

}
 /*
* Methods of this class
**/
Public void wirte (){
Penpencil. wirte ();
}

Public void eraser (){
Eraser. Erase ();
}
}

Test class

Package com. test;

Public class test {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Pencilwitheraser = new pencilwitheraser ();
Pencilwitheraser. wirte ();
Pencilwitheraser. Eraser ();
}

}

This class has both pen and eraser functions. Simulate Java multi-inheritance.

 

Related Article

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.