函数对象 java_函数对象
函數(shù)對象
* 一種將函數(shù)作為參數(shù)傳遞的獨創(chuàng)方法 是注意到 在我們想傳遞的參數(shù) 即包含數(shù)據(jù) 也保包含方法,于是我們可以定義一個
* 不包含數(shù)據(jù) 只 包含方法的類,并傳遞該類的一個實例,事實上,一個函數(shù)通過將其放在一個對象內(nèi)部而被傳遞,這樣的對象
* 通常稱為函數(shù)對象
package javabean.newTest51;
import com.sun.istack.internal.localization.NullLocalizable;
import com.sun.xml.internal.bind.AnyTypeAdapter;
import javabean.newTest51.javabean.CaseInsensitiveCompare;
import javabean.newTest51.javabean.Circle;
import javabean.newTest51.javabean.Shape;
import javabean.newTest51.javabean.Square;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
/**
* 2018/5/1
* 陳東
* 函數(shù)對象
*/
public class Test {
public static boolean contains(AnyType []arr,AnyType x){
return false;
}
public static double toTalArea(Collection extends Shape> shapes){
int toTalArea = 0;
for (Shape s:shapes
) {
if (s!=null){
toTalArea+=s.area();
}
}
return toTalArea;
}
@org.junit.Test
public void test(){
Collection squares = new ArrayList();
Square square1 = new Square(6.0,7.0);
Square square2 = new Square(5,3);
squares.add(square1);
squares.add(square2);
System.out.println(Test.toTalArea(squares));
}
/**
* 函數(shù)對象
* 一種將函數(shù)作為參數(shù)傳遞的獨創(chuàng)方法 是注意到 在我們想傳遞的參數(shù) 即包含數(shù)據(jù) 也保包含方法,于是我們可以定義一個
* 不包含數(shù)據(jù) 只 包含方法的類,并傳遞該類的一個實例,事實上,一個函數(shù)通過將其放在一個對象內(nèi)部而被傳遞,這樣的對象
* 通常稱為函數(shù)對象
*/
public static AnyType findMax(List anyTypes, Comparator super AnyType> cmp){
int maxindex = 0;
for (int i=0;i
if (cmp.compare(anyTypes.get(i),anyTypes.get(maxindex))>0){
maxindex = i;
}
}
return anyTypes.get(maxindex);
}
@org.junit.Test
public void testFun(){
List shapes = new ArrayList<>();
Square square = new Square(7.0,8.0);
shapes.add(square);
Circle circle = new Circle(8.0);
shapes.add(circle);
System.out.println(Test.findMax(shapes,new CaseInsensitiveCompare()).toString());
}
@org.junit.Test
public void testFUn() {
List squares = new ArrayList<>();
Square square = new Square(7.0, 8.0);
Square square2 = new Square(8.0, 8.0);
Square square3 = new Square(7.0, 9.0);
squares.add(square);
squares.add(square2);
squares.add(square3);
Comparator super Square> cmp = new CaseInsensitiveCompare();
System.out.println(Test.findMax(squares,cmp).toString());
}
}
涉及到的實體類
package javabean.newTest51.javabean;
import java.util.Comparator;
public class CaseInsensitiveCompare implements Comparator {
@Override
public int compare(Shape shape1,Shape shape2){
if (shape2.area() < shape1.area()){
return 1;
}
return 0;
}
}
package javabean.newTest51.javabean;
public class Circle extends Shape{
private double circlelength;
public double getCirclelength() {
return circlelength;
}
public void setCirclelength(double circlelength) {
this.circlelength = circlelength;
}
public Circle(Double d){
this.circlelength = d;
}
@Override
public Double area(){
return 3.14*2*circlelength;
};
@Override
public String toString() {
return "Cirle: banjing"+getCirclelength();
}
}
package javabean.newTest51.javabean;
public abstract class Shape {
public abstract Double area();
}
package javabean.newTest51.javabean;
public class Square extends Shape {
private double lenhth;
private double hehth;
public void setLenhth(double lenhth) {
this.lenhth = lenhth;
}
public double getLenhth() {
return lenhth;
}
public void setHehth(double hehth) {
this.hehth = hehth;
}
public double getHehth() {
return hehth;
}
@Override
public String toString() {
return "Squre: heth:"+getHehth()+"le:"+getLenhth();
}
public Square(double lenhth,double hehth){
this.hehth =hehth;
this.lenhth = lenhth;
}
@Override
public Double area(){
return hehth*lenhth;
};
}
總結(jié)
以上是生活随笔為你收集整理的函数对象 java_函数对象的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 容器 线程_JAVA多线程并发
- 下一篇: java 矩阵求逆_java实现的顺时针