Self-Study Java Chapter Nineth interface

Source: Internet
Author: User

Learned the interface today.

First, the interface is similar to an abstract class. An abstract class must be defined as long as it contains an abstract method, but abstract methods must not necessarily be in an abstract class.

If you inherit an abstract class, the export class must implement all the abstract methods of the abstract class, or the exported class must be defined as an abstract class.

An interface can be said to be a completely abstract class, and its internally defined methods are abstract methods.

The interface keyword can be added with the Public keyword (but only if the interface is defined in a folder with the same name), otherwise there is a package access permission. Because the methods in the interface are implicitly public (that is, you cannot use private or protected, or compile an error), when implementing an interface, the methods defined in the interface must define their display as public in the class that implements the interface, otherwise the access permissions are reduced and the compilation will error.

Interfaces can contain fields, which are implicitly static and final.

An interface can inherit multiple interfaces, and one class can inherit another class and multiple interfaces at the same time.

Core reasons for using interfaces:

1. Ability to transition upward to multiple base types (for increased flexibility);

2. Make sure that you just create an interface that prevents the client programmer from creating its objects.

An interface can be nested, that is, you can define an interface in a class, or define an interface in an interface. These internal interfaces are public or protected or private.

Interface secondary to the design pattern:

A strategy design pattern that can behave differently depending on the parameter object being passed

Here is an example of a simple policy pattern:


public class Strategy {//a simple policy mode
private static String s= "This is A strategy Class";
public static void Main (string[] args) {
Processor[] p={
New Upcase (),
New Downcase ()
};
for (Processor x:p)
Apply.process (x, s);
}

}

Class processor{
Public String name () {
Return GetClass (). Getsimplename ();
}
Object process (object input) {
return input;
}
}

Class Upcase extends processor{
String process (Object input) {
return (String) input. toUpperCase ();
}
}

Class Downcase extends processor{
String process (Object input) {
return (String) input. toLowerCase ();
}
}

Class apply{
public static void process (Processor p,object s) {
System.out.println ("Using Processor" +p.name ());
System.out.println (p.process (s));
}
}

Adapter mode, http://www.cnblogs.com/java-my-life/archive/2012/04/13/2442795.html

An example of your own writing:

public class Adapter {//adapter policy, simple example
public static void Main (string[] args) {
Waveform w=new waveform ();
Apply1.process (New Filteradapter (New Lowpass ()), W);
Apply1.process (New Filteradapter (New Highpass ()), W);
Apply1.process (New Filteradapter (New Lowpass ()), W);
}

}

Class waveform{
private static Long Count;
Private final long id=count++;
Public String toString () {
Return "waveform" +ID;
}
}


Class apply1{
public static void process (Processor1 p,object s) {
System.out.println ("Using Processor1" +p.name ());
System.out.println (p.process (s));
}
}

Interface processor1{
String name ();
Object process (object input);
}

Class filter{

Public String name () {
Return GetClass (). Getsimplename ();
}
Public waveform process (Object input) {
return (waveform) input;
}

}

Class Lowpass extends filter{
Public waveform process (waveform W) {
Return w;
}
}

Class Highpass extends filter{
Public waveform process (waveform W) {
Return w;
}
}

Class Filteradapter implements processor1{
Private filter filter;
Filteradapter (Filter f) {
Filter=f;
}
@Override
Public String name () {
return Filter.name ();
}

@Override
public object Process (object input) {
Return Filter.process ((waveform) input);
}

}

Factory method Mode, http://www.cnblogs.com/forlina/archive/2011/06/21/2086114.html

Here is a simple example of my own writing:

Import Java.util.Random;
Import Java.util.Scanner;

public class Demo_ninteen {
public static void PlayGame (Gamefactory f) {
Game G=f.getgame ();
G.play ();
}
public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN ("Please select the game, toss a coin to choose 1, Dice 2:");
Scanner scan=new Scanner (system.in);
if (Scan.nextint () ==1)
PlayGame (New Throwcoinfactory ());
Else
PlayGame (New Throwdicefactory ());
}

}

Interface game{//abstract Game Products
void Play ();
}

Interface gamefactory{//abstract Factory
Game Getgame ();
}

Class Throwcoin implements game{//coin toss
Private random rand=new random ();
public void Play () {
if (Rand.nextint (2) ==1)
System.out.println ("You are throwing a positive");
Else
System.out.println ("You are throwing the opposite");
}
}

Class Throwcoinfactory implements gamefactory{//actual factory
Public Game Getgame () {
return new Throwcoin ();
}
}

Class Throwdice implements game{//Roll dice
Private random rand=new random ();
public void Play () {
Switch (rand.nextint (6) +1) {
Case 1:
System.out.println ("You are throwing 1");
Break
Case 2:
System.out.println ("You are throwing 2");
Break
Case 3:
System.out.println ("You are throwing 3");
Break
Case 4:
System.out.println ("You are throwing 4");
Break
Case 5:
System.out.println ("You are throwing 5");
Break
Case 6:
System.out.println ("You are throwing 6");
Break
}
}
}

Class Throwdicefactory implements gamefactory{//actual factory
Public Game Getgame () {
return new Throwdice ();
}
}

Self-Study Java Chapter Nineth interface

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.