日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

函数对象 java_函数对象

發(fā)布時間:2025/3/20 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 函数对象 java_函数对象 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

函數(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。