groovy 中的构造函数允许使用命名参数,方法参数允许使用默认值,但我似乎无法获得具有默认参数值的方法与命名参数一起使用。
理想情况下,我想要这样的东西:
def myMethod(def greeting = "Hello", def person = "World", def inflection = "!") {
"${greeting} ${person}${inflection}"
}
assert myMethod() == "Hello World!"
assert myMethod(greeting: "Hi") == "Hi World!"
assert myMethod(inflection: "?", person: "bob") == "Hello Bob?"
assert myMethod("Sup") == "Sup World!"
assert myMethod("Word", "Dawg") == "Word Dawg!"
Run Code Online (Sandbox Code Playgroud)
这是我能得到的最接近的结果,但它非常丑陋,感觉不是很“常规”,并且在所有情况下都不能完全工作:
def myMethod(Map vars = [:]){
vars.greeting = vars.greeting ?: "Hello"
vars.person = vars.person ?: "World"
vars.inflection = vars.inflection ?: "!"
"${vars.greeting} ${vars.person}${vars.inflection}"
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在方法中传递默认参数,但到目前为止还无法这样做。我编写了一个具有以下签名的方法。
def abc(a,b=22,c,d=55)
end
Run Code Online (Sandbox Code Playgroud)
我收到上述代码的错误“语法错误,意外的‘=’,期望‘)’”。
如果我将上面的代码替换为下面所示的代码,那么它就可以正常工作。
def abc(a,b=5,c)
end
Run Code Online (Sandbox Code Playgroud)
这可能是什么原因?
谢谢
我有B带有 to 参数的类x,并y从A带有x可选参数的类扩展(所有参数都非空),我如何B以一种方式定义它是可选的,并且它将在构造函数中使用可选值A
val y = 0
val b = if (y == 0) B(y) else B(y, 0)
class B(y: Int, x: Int = 238) : A(x)
open class A(x: Int = 238)
Run Code Online (Sandbox Code Playgroud)
x在这里,我已经在构造函数中设置了默认值,B有什么方法可以实现这一点,而不必在中设置默认值B
constructor instantiation instance default-parameters kotlin
我正在尝试构建一个能够将 aMap[String, Any]转换为class/case class实例的工具。如果类定义包含未在 中指定的默认参数,Map则将应用默认值。
以下代码片段允许检索主类构造函数:
import scala.reflect.runtime.universe._
def constructorOf[A: TypeTag]: MethodMirror = {
val constructor = typeOf[A]
.decl(termNames.CONSTRUCTOR)
// Getting all the constructors
.alternatives
.map(_.asMethod)
// Picking the primary constructor
.find(_.isPrimaryConstructor)
// A class must always have a primary constructor, so this is safe
.get
typeTag[A].mirror
.reflectClass(typeOf[A].typeSymbol.asClass)
.reflectConstructor(constructor)
}
Run Code Online (Sandbox Code Playgroud)
鉴于以下简单的类定义:
class Foo(a: String = "foo") {
override def toString: String = s"Foo($a)"
}
Run Code Online (Sandbox Code Playgroud)
Foo提供两个参数时,我可以轻松创建一个新实例:
val bar = constructorOf[Foo].apply("bar").asInstanceOf[Foo]
bar: …Run Code Online (Sandbox Code Playgroud) reflection scala default-value default-parameters scala-reflect
我将如何创建一个将另一个函数的地址作为参数的函数,以便不需要传递它。
我的第一个解决方案是这样的:
void empty_func() {}
void func(void (*additional_func) () = &empty_func)
{
additional_func();
// some other code...
}
Run Code Online (Sandbox Code Playgroud)
这个解决方案有效。
但是,我想知道是否有一些更方便的方法可以使传递additional_function可选,这样我就不必创建一个新函数(empty_func())并将其作为默认参数传递?
c++ parameters function-pointers function default-parameters
我正在尝试在C中创建一个indexOf函数.该函数必须找到参数中给出的任何字母或单词的位置.但是当我试图使用它们时,编译器会警告"参数太少".我怎样才能做到这一点?谢谢.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int indexOf(char*, char*, char);
int main(){
char stuff[] = "abcdefghijklmopqrstuvwxyz";
printf("Result: %d", indexOf(stuff, 'b') );
printf("Result: %d", indexOf(stuff, "defg") );
getch();
return 0;
}
int indexOf(char *text, char *word, char letter){
if(word == DEFAULT VALUE)
// find the letter in the text
else if(letter == DEFAULT VALUE)
// find the word in the text
}
Run Code Online (Sandbox Code Playgroud) 我正在制作一些函数来保存和加载我的值:
他们是这两个:
void safeFile(string &set_name) const;
void loadFile(string &set_name);
Run Code Online (Sandbox Code Playgroud)
这里的问题是我也希望它们为char*工作所以我像这样重载它们:
void safeFile(string &set_name) const;
void loadFile(string &set_name);
void safeFile(char* set_name) const;
void loadFile(char* set_name);
Run Code Online (Sandbox Code Playgroud)
在cpp文件中:
void myclass::loadFile(string &set_name)
{
...
}
/* loadFile */
void myclass::safeFile(string &set_name)
{
...
}
/* safeFile*/
void myclass::loadFile(char *set_name)
{
string mystring(set_name);
loadFile(mystring);
}
/* loadFile */
void myclass::safeFile(char *set_name) const
{
string mystring(set_name);
safeFile(mystring);
}
/* safeFile*/
Run Code Online (Sandbox Code Playgroud)
有没有更好的或其他方式我应该这样做?谢谢
我正在学习Prolog,我在本教程中学习 了图表.这是我的代码:
path(X, Y, Length, [X,Y], _) :-
connect(X, Y, Length).
path(X, Y, Length, [X|P], V) :-
\+ member(X, V),
connect(X, Z, Length1),
path(Z, Y, Length2, P, [X|V]),
Length is Length1 + Length2.
Run Code Online (Sandbox Code Playgroud)
为了使用它,我打电话
?- path(a, f, Length, Path, []).
Run Code Online (Sandbox Code Playgroud)
但是,我想将其缩短为:
?- path(a, f, Length, Path).
Run Code Online (Sandbox Code Playgroud)
但是我无法获得默认参数.
我正在玩ES6默认参数,我有一个奇怪的行为.
以下是该问题的一个简短示例:
function test(firstValue, secondValue=5){
console.log("firstValue: " + firstValue);
console.log("secondValue: " + secondValue);
console.log("----------")
}
test(2, secondValue = 3)
test(secondValue = 3)
Run Code Online (Sandbox Code Playgroud)
它的输出:
firstValue: 2
secondValue: 3
----------
firstValue: 3
secondValue: 5
----------
Run Code Online (Sandbox Code Playgroud)
在第二种情况下,我期待firstValue: undefined和secondValue: 3.这种行为是否正常.我错过了什么吗?
我写了这个小程序来测试我的理解。我很难理解的是构造函数没有被继承,但是类B能够调用类A的构造函数!
#include <iostream>
using namespace std;
class A {
public:
A(int x = 2) { //Constructor A
num = x;
}
int getNum() {
return num;
}
protected:
int num;
};
class B: public A { //Constructor B
public:
B() {
A(5);
}
};
int main() {
A a(3); //create obj a with num = 3
B b; //create obj b
cout << a.getNum() << endl;
cout << b.getNum() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出为:
3
2
Run Code Online (Sandbox Code Playgroud)
构造函数A的调用到底做了什么?它没有使用传递的参数来初始化对象b的编号!
此外,如果我从类A的构造函数中删除默认值,则会出现编译错误:
no …Run Code Online (Sandbox Code Playgroud) c++ ×3
constructor ×2
function ×2
c ×1
ecmascript-6 ×1
groovy ×1
instance ×1
javascript ×1
kotlin ×1
methods ×1
overloading ×1
parameters ×1
prolog ×1
reflection ×1
ruby ×1
scala ×1