所以如果我有一个带有大量命名参数的函数:
def foo(a = 1, b = 2, c = 3, d = 4, e = 5) # etc...
pass
Run Code Online (Sandbox Code Playgroud)
我用所有与定义名称完全相同的参数调用它:
a = 0
b = 0
c = 0
d = 0
e = 0
Run Code Online (Sandbox Code Playgroud)
有没有办法避免这样做?
foo(e = e, b = b, d = d, a = a, c = c)
Run Code Online (Sandbox Code Playgroud)
并且这样做:
foo(e, b, d, a, c)
Run Code Online (Sandbox Code Playgroud)
?
我想我可以这样做:
foo(a, b, c, d, e)
Run Code Online (Sandbox Code Playgroud)
但如果争论的名字复杂,我不记得他们的顺序呢?
关于将数组作为const参数传递的问题,我试图弄清楚如何编写一个方法,其中参数是一个固定大小const数组的const数组.唯一可写的东西就是这些数组的内容.
我在考虑这样的事情:
template <size_t N>
void myMethod(int* const (&inTab)[N])
{
inTab = 0; // this won't compile
inTab[0] = 0; // this won't compile
inTab[0][0] = 0; // this will compile
}
Run Code Online (Sandbox Code Playgroud)
此解决方案中唯一的问题是我们不知道第一个维度.有人有解决方案吗?
提前致谢,
凯文
[编辑]
我不想使用std :: vector或这样的动态分配数组.
我试图采取灵活的参数在JavaScript中操纵色彩与eyecon颜色拾取(www.eyecon.ro/colorpicker/)
但是当我试图改变页面的颜色时,是否会陷入arguments.length循环?
HTML是:
<div class="colorSelector" id="colorSelector3"><div style="background-color: #0000ff"></div></div>
Run Code Online (Sandbox Code Playgroud)
和jquery/javascript是:
$('#colorSelector3').click(function(){
colorPickDynamic('#colorSelector3','h1','color');
});
function colorPickDynamic(cp){
var i;
var j;
for (x=0, i = 1, j = 2; j < arguments.length; i+2, j+2, x++) {
var tag = [];
var colorProperty = [];
tag[x]=arguments[i];
colorProperty[x]=arguments[j];
}
$(cp).ColorPicker({
color: '#0000ff',
onShow: function (colpkr) {
$(colpkr).fadeIn(500);
return false;
},
onHide: function (colpkr) {
$(colpkr).fadeOut(500);
return false;
},
onChange: function (hsb, hex, rgb) {
$(tag[0]).css(colorProperty[0], '#' + hex);
$(cp + ' div').css('backgroundColor', '#' …Run Code Online (Sandbox Code Playgroud) 当我们将参数传递给java类中的方法时.是通过VALUE或BY REFERENCE传递的参数是默认值吗?
可以有两种可能性,
1.如果我们将参数传递给同一类中另一个方法的方法.
2.如果我们将参数传递给另一个类的方法.
其次,如果我想通过引用传递值(如果java的默认属性是By Value)那么我该怎么办?
我正在尝试ShadeRec使用其构造函数初始化我的类的实例:
ShadeRec(World& world);
Run Code Online (Sandbox Code Playgroud)
所以我转到它:
ShadeRec sr(*this);
Run Code Online (Sandbox Code Playgroud)
其中"this"是World类的一个实例.
我收到以下错误:
World.cpp: In member function ‘ShadeRec World::hitObjects(const Ray&) const’:
World.cpp:52: error: no matching function for call to ‘ShadeRec::ShadeRec(const World&)’
ShadeRec.h:17: note: candidates are: ShadeRec::ShadeRec(const ShadeRec&)
ShadeRec.h:15: note: ShadeRec::ShadeRec(World&)
Run Code Online (Sandbox Code Playgroud)
假设问题只是World实例具有属性const,我该如何摆脱此错误消息?
我想动态显示页面链接的可用菜单,具体取决于使用Tapestry登录的用户类型.
我的部分代码Layout.tml如下所示:
<div class="header">
<t:if t:test="userLoggedIn">
<div class="menu">
<ul>
<t:loop t:type="loop" source="pageNames" value="pageName" class="prop:classForPageName">
<t:if t:test="isUserAllowedOnPage('pageName')">
<li>
<t:pagelink page="prop:pageName.name">${pageName.displayName}</t:pagelink>
</li>
</t:if>
</t:loop>
</ul>
</div>
</t:if>
<div style="clear:both;"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
在我的Layout.java我有一个以下方法:
public boolean isUserAllowedOnPage(String pageName) {
// My logic here, returns either true or false
}
Run Code Online (Sandbox Code Playgroud)
问题是,我不知道如何实际的网页名称参数传递给isUserAllowedOnPage(String pageName)方法,因为下面的一行tml代码,
"isUserAllowedOnPage('pageName')"
我传递的实际字符串,"pageName"而不是期望的值之一(例如"Index","About","Contacts"...).
loops if-statement tapestry parameter-passing argument-passing
我刚开始研究如何用Java编程语言编写代码.
我遇到了一个问题,告诉我将数字,变量和表达式作为过程调用的参数传递.我遇到的问题是,当我尝试将数字,变量和表达式作为参数传递给过程调用时,我收到错误(我有27个错误).
下面是我的代码,如果有人能指出我的代码有什么问题,我将非常感激.谢谢.
public class test {
// this is the procedure definition
public static int computeCost ( int quantity , int price ) {
return quantity * price;
}
public static void main ( String args[] )
// passing numbers as arguments to the procedure call "cost"
System.out.println ( computeCost ( 7 , 12 ) );
// passing variables as arguments to the procedure call "cost"
int a = 5;
int b = 7;
System.out.println ( computeCost ( a …Run Code Online (Sandbox Code Playgroud) 如果我只将一个整数数组的元素(不是整个数组)传递给一个函数,它默认是通过值传递还是通过引用传递?
例如-
arr[]={2,3,4,5,6}
ans=fun(arr[2])
Run Code Online (Sandbox Code Playgroud)
和fun是一个函数,它将任意值乘以2,然后如果我之后在main函数中打印arr [2],我会将ans作为8或4吗?任何帮助,将不胜感激.
是否可以将闭包作为函数中的可选参数?
我需要这样的东西(伪代码):
fn function(x: int, optional expr |int| -> int) -> int
Run Code Online (Sandbox Code Playgroud)
和用法将是这样的:
// just the mandatory argument
n = function(z);
Run Code Online (Sandbox Code Playgroud)
或者可选:
// passed closure would be called inside the function
n = function(z, |x| x * x);
Run Code Online (Sandbox Code Playgroud)
如果它甚至可能,我只是无法掌握正确的语法(会欣赏正确匹配表达式的完整示例).
在Meteor文档中Meteor.call(),它显示:
如果你包含一个回调函数作为最后一个参数(它不能作为方法的参数,因为函数不可序列化)...
我运行了类似于Meteor.call('name', function() {console.log('abc');}, function() {})之前的东西,其中function() {console.log('abc');}作为参数传入,而empty function() {}用作异步回调的存根.它有效.
那个声明告诉我我不应该将任何函数作为参数传递给函数,或者只应用于回调函数.
在任何一种情况下,为什么该函数不可序列化?我的浅层理解只是一个可序列化的对象,你可以把它变成一个位序列(1's 0' 和's'),因为所有数字都是位序列,我不明白为什么函数不可序列化.
我找到了一些解释,但它们都与Java有关,对于那些还不知道序列化重要性的人来说,它并没有多大帮助.
为什么函数不可序列化?(它与它有什么关系Meteor.call()?)
argument-passing ×10
c++ ×2
function ×2
java ×2
javascript ×2
arrays ×1
c ×1
closures ×1
const ×1
constructor ×1
if-statement ×1
jquery ×1
loops ×1
meteor ×1
optional ×1
pointers ×1
python ×1
reference ×1
rust ×1
tapestry ×1