Saturday, 15 June 2013

java - Making an functional interface working with different object types -


I have defined for my project a functional interface called function . Its only way, call , like this:

  public interface function {public zero call (); }   

And in my field object, I have this:

  public square field {private class [] [] matrix; // Square is suitable for public areas (int rows, int colals) {matrix = new square [rows] [column]; For (int i = 0; i & lt; rows; i ++) {for (int j = 0; j & lt; cols; j ++) {this.matrix = new square (i * square.NORMAL_WIDTH, J * square. NORMAL_HEIGHT); }}}}   

It works fine, and it looks like javascript, but still I can not give it the purpose of my attention. But consider that I want to develop this method:

  public zeros each (function function) {int rows = matrix.length; {Int cols = matrix [i] for (int i = 0; i & lt; rows; i ++). Length; For (int j = 0; j & lt; cols; j ++) {f.call (); }}}   

This will attach some specific code of each element of the matrix (in this case, function implementation). In this way, I can use my properties. But every object of the matrix is ​​a square. How can I reach it? I can pass it to the function, make a small change in

  // parameter. Public Interface Function {Public Zero Call (Square Square); } Each of the public zeros (function f) {int rows = matrix.length; {Int cols = matrix [i] for (int i = 0; i & lt; rows; i ++). Length; For (int j = 0; j and lt; cols; j ++) {ficol (matrix [i] [j]); }}}   

But still, I will be stuck in the class type. I can use the common type, maybe?

Yes, you need to make it normal

The simplest case: < / P>

  Public interface function & lt; T & gt; {Public Zero Call (T-AGR); } Better practice  each ()  is to declare as follows: {0}}   

Each of the public zeros (Tasks & Square;

  Each of the public zeros (function & lt; super squire & gt; f) {...}   

so that you only get the function & Lt; Square & gt; , but its any supertype's function , such as

  function & lt; Object & gt; Print = new function & lt; Object & gt; () {Public Zero Calls (Object Arg) {System.out.println (arg); }}    

No comments:

Post a Comment