Java interface

zhaozj2021-02-08  197

Adding a wonderful interface

Peng Jianxiong

Keyword

Java, Interface

Description

If you have a good job, the letter is too bad, please understand, I hope to help friends study COM.

Interface function introduction

1, 'Pure' Abstract Class Implementation (see Java Programming Ideology P / 153)

// Interface1.java interface is only responsible for describing your own look "For all the classes to achieve my class, it should look like me. All the methods, the implementation of the class must have!"

Public interface interface1 {

Public void sets (String Str);

Public string gets ();

Public void showmessage (string msg);

}

============================================================================================================================================================================================================= ===================

// Classitf.java "interface is just a very 'pure' abstract thing, your realization code is here!"

Public class classitf imports interface1 {

Public String S = "";

Public classitf () {

}

Public void showMessage (String MSG)

{

System.out.print (this.getclass (). Getname () "=====" msg "==== by interface1n /");

Public void sets (String STR)

{

S = STR;

}

Public String Gets ()

{

Return S;

}

}

2, multiple inheritance of implementation (see Java Programming Ideology P / 155)

//Interfase2.java interface

Public interface interface2 {

Public void showMessage2 (String MSG);

}

// Classitf.java Add multiple inheritance Classitf grasses to add changes, insert

Public class classitf imports interface1, interface2 {

Public String S = "";

Public classitf () {

}

Public void showMessage (String MSG)

{

System.out.print (this.getClass (). Getname () "=====" msg "==== by interface1n /");

}

Public void sets (String STR)

{

S = STR;

}

Public String Gets ()

{

Return S;

}

Public void showMessage2 (String MSG) {

System.out.print (this.getClass (). Getname () "=====" msg "==== by interface2n /");

}

}

Ha ha! My multi-inheritance function has a lot of applications in Java, such as:

Public Class Jframe Extends Frame Implements WINDOWCONSTANTS, Accessible, Rootpaner

3, look and achieve separation

// Classitf.java "interface is just a very 'pure' abstract thing, your realization code is here!"

Public class classitf imports interface1, interface2 {

Public String S = "";

Public classitf () {

}

Public void showmessage (string msg) // Implement interface 'interface1'ShowMessage

{

System.out.print (this.getClass (). Getname () "=====" msg "==== by interface1n /");

}

Public void sets (String str) // Implement interface 'interface1'sets

{

S = STR;

}

Public string gets () // Implement interface 'interface1'gets

{

Return S;

}

Public void showMessage2 (String msg) // Implement interface 'interface2' showMessage2

{

System.out.print (this.getClass (). Getname () "=====" msg "==== by interface2n /");

}

}

4, provide a shadow called

Public Void ShowMessage (Interface1 Req)

{

Req.ShowMessage ("111");

}

Public void showMessage2 (Interface2 Req)

{

Req.showMessage2 ("111");

}

Void JButton1_ActionPerformed (ActionEvent E) {

Private classitf c1 = new classitf ();

SHOWMESSAGE (C1);

SHOWMESSAGE2 (C1);

}

The same, my application is very wide!

For example, in servlet

Public void dopost (httpservletRequest Request, httpservletResponse response)

Public void doget (httpservletRequest request, httpservletResponse response)

HTTPSERVLETREQUEST, HTTPSERVLETRESPONSE My app!

Other such as:

Event Listen:

Public Void AddActionListener (ActionListener L)

Observer mode!

5, implementation of reference delivery through the interface

Public void sets1 (Interface1 Req)

{

Req.sets ("New Value with C1");

}

Public void sets2 (Classnoitf Req)

{

Req.sets ("New Value with C2");

}

Void JButton3_ActionPerformed (ActionEvent E) {

Classitf C1 = New classitf ();

Classnoitf C2 = New classnoitf ();

// set C1

SETS1 (C1);

// set C2;

Sets2 (C2);

// Get Value

System.out.print (c1.gets ());

System.out.print (C2.Gets ());

}

转载请注明原文地址:https://www.9cbs.com/read-3014.html

New Post(0)