首页 \ 问答 \ Java - 通过参数传递方法(Java - Passing a method through a parameter)

Java - 通过参数传递方法(Java - Passing a method through a parameter)

我正在尝试创建一种方法,允许我使用我认为称为lambdas的方法,在一系列连接上执行方法。

这是我在经过一些研究后得出的代码,但它不起作用:

performGlobalAction(()->{
    // doSomething();
});

你还需要看到我假设的方法:

private <T> void performGlobalAction(Callable<T> action) {
    for(int i = 0; i < connectionList.size(); i++) {
        connectionList.get(i).performAction(action);
    }
}

这提供了以下错误:

The method performAction(Callable<T>) in the type Connection is not
applicable for the arguments (() -> {})

这种方法的目标是允许自己构建一个“在旅途中”的方法而不为它创建空白。

这可能吗? 看起来我之前已经使用了大量的声明。 看起来这实际上就是lambdas语句的工作原理。


I'm trying to create a method that allows me to make use of what I believe is called lambdas, to execute a method over a series of connections.

Here's my code that I've come up with after some research, but it doesn't work:

performGlobalAction(()->{
    // doSomething();
});

You'll also need to see the method I would assume:

private <T> void performGlobalAction(Callable<T> action) {
    for(int i = 0; i < connectionList.size(); i++) {
        connectionList.get(i).performAction(action);
    }
}

This provides the following error:

The method performAction(Callable<T>) in the type Connection is not
applicable for the arguments (() -> {})

The goal of this method is to allow myself to construct a method "on the go" without creating a void for it.

Is this possible? It seems like I've used plenty of statements that have done this before. It seems like this is actually exactly how lambdas statements work.


原文:https://stackoverflow.com/questions/26985390
更新时间:2023-08-26 13:08

最满意答案

由于您只希望它在完全脱离视口时变得固定,然后滑入,只需修改top属性,然后将其动画回到视图中。 看一个工作示例的小提琴

UPDATE

这个更新的小提琴应该更好,因为它只应用行为(如果尚未应用),并在返回到正常的“静态”位置时完全删除动态样式。

请注意,向上滚动时仍然会出现闪烁 - 这是因为导航从其固定位置“跳跃”回到静态位置。 使用与上述动画类似的技术可以很容易地解决这个问题。


Since you only want it to become fixed when it is completely out of viewport, then slide in, just modify the top property and then animate it back into view. See this fiddle for a working example.

UPDATE

This updated fiddle should work better, as it only applies the behaviour if not already applied, and completely removes dynamic styles when returning to normal 'static' position.

Note there is still a flicker when scrolling back up - this is because the nav 'jumps' from its fixed position back to its static position. This can easily be resolved using a similar technique to the animation above.

相关问答

更多
  • 这应该做你想要的。 您必须找到您想要隐藏的值。 $(window).scroll( function() { var valueOfScroll = $(document).scrollTop().valueOf(); if(valueOfScroll <= ???) $('#sticker').show(); else $('#sticker').hide(); }); 从您网站上的测试值来看,它似乎运行良好,因此它会在主要内容结束之前消失: $(w ...
  • 如何在滚动上应用这些特定样式? 如果 您可以使用jQuery库 ,则可以使用以下代码检测scroll事件: $(window).scroll(function(){ // your code }); 然后你可以使用addClass()和removeClass()来玩class。 有没有办法检测顶部导航何时退出视野? 您可以使用这段代码获取导航栏的高度,再次使用jQuery并使用scrollTop()方法在scroll()函数中进行测试,如下所示: var stickyHeight = $('.st ...
  • 这可能更容易 看起来问题是你需要在window scrollTop后面有一个() 。 var n = $("#navheader");//get nav var nh = n.offset().top;//get nav offset var go = true;//toggle execute so it doesn't fire on every match $(window).scroll(function(){ var wh = $(this).scrollTop();//window sc ...
  • 当您的内容恰好是粘性元素设置为position: relative高度时,就会出现问题position: relative ,它足够高以垂直滚动,当从文档流中删除时,内容不再高到足以强制滚动。 因此,当它切换到固定时,您会快速回到顶部,因为您的文档不够高,无法滚动,这会将您的粘性元素设置为position: relative ,这会带回滚动条。 您可以通过向页面添加更多内容来测试这一点 - 如果它足够高,即使没有粘性元素,它仍然会滚动,您将不会遇到此问题。 但是,您应该注意到另一个问题,那就是您的内容会稍微 ...
  • 由于您只希望它在完全脱离视口时变得固定,然后滑入,只需修改top属性,然后将其动画回到视图中。 看一个工作示例的小提琴 。 UPDATE 这个更新的小提琴应该更好,因为它只应用行为(如果尚未应用),并在返回到正常的“静态”位置时完全删除动态样式。 请注意,向上滚动时仍然会出现闪烁 - 这是因为导航从其固定位置“跳跃”回到静态位置。 使用与上述动画类似的技术可以很容易地解决这个问题。 Since you only want it to become fixed when it is completely ou ...
  • 我强烈建议不要更改库文件 ,它首先会打败您使用库的目的。 库被设计为以特定方式工作,并且大部分遵循严格的代码,这意味着如果更改单个组件,则反响可能是坏的或更糟糕的不可调试。 如果你愿意,你必须总是用自己的自定义文件覆盖代码。 在你的情况下, 在 创建一个你自己说的js文件, 和在那里添加(这只是一个基本的例子) element.css({"width" ...
  • 你只需要添加到你的元素的css,它具有类“banner”: position : absolute; bottom : 0; PS:实际上你的盒子正在调整大小,但它默认设置在你设置的标题的左上角。 由于父框设置在position:relative上,因此所有绝对元素都将引用该父元素而不是整个文档。 You just have to add to your element's css that has the class "banner" : position : absolute; bottom : 0; ...
  • 问题是当标题被赋予固定位置时,标题的高度会超过其余元素,因此它会跳到文档的顶部。 我更改了一些CSS,因此标题是绝对的,内容有一个从顶部开始的边距。 请参阅: http : //jsfiddle.net/FZpf7/2/ ,您将看到跳跃消失了。 改变了CSS: nav { background-color:#FBFBFB; color:white; position:absolute; top:0px; left:0px; width:100%; } div.header_med ...
  • 用另一个div (占位符)
  • 您应该限制事件回调的次数,例如resize , scroll触发。 实现这一目标的常用方法称为油门和去抖动。 许多javascript库,如下划线,lodash等实现这一点。 在jQuery的情况下,有一个jQuery插件jquery-throttle-debounce 你可以像使用它一样 jQuery(window).resize($.debounce( 250, function() { navOffset = jQuery('.nav').offset().top; jQuery('.nav' ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)