我正在寻找一种方法将属性本身传递给一个函数.不是财产的价值.函数事先不知道哪个属性将用于排序.此示例中最简单的方法是:使用不同的参数类型创建4个覆盖.其他方式是使用typeof()内部功能.当Class1具有数百个属性时,这两种方式都是不可接受的.到目前为止我找到了以下方法:
class Class1
{
string vehName;
int maxSpeed;
int fuelCapacity;
bool isFlying;
}
class Processor
{
List<Class1> vehicles = null;
Processor(List<Class1> input)
{
vehicles = input;
}
List<Class1> sortBy(List<Class1> toSort, string propName)
{
if (toSort != null && toSort.Count > 0)
{
return toSort.OrderBy(x => typeof(Class1).GetProperty(propName).GetValue(x, null)).ToList();
}
else return null;
}
}
class OuterUser
{
List<Class1> vehicles = new List<Class1>();
// ... fill the list
Processor pr = new Processor(vehicles);
List<Class1> sorted = pr.sortBy("maxSpeed");
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢这种方法,因为在将字符串传递给处理函数时存在"人为错误"的风险.当字符串由代码的其他部分生成时,这将变得更加丑陋.请提出更优雅的方法来实现Class1属性的传递以进行进一步处理.使用恕我直言的最佳选择(或类似的东西):
vehicles …Run Code Online (Sandbox Code Playgroud) 如果我有一个功能
def foo(x, y):
pass
Run Code Online (Sandbox Code Playgroud)
我如何从函数内部判断y是按位置传递还是使用其关键字传递?
我想要类似的东西
def foo(x, y):
if passed_positionally(y):
print('y was passed positionally!')
else:
print('y was passed with its keyword')
Run Code Online (Sandbox Code Playgroud)
所以我得到
>>> foo(3, 4)
y was passed positionally
>>> foo(3, y=4)
y was passed with its keyword
Run Code Online (Sandbox Code Playgroud)
我意识到我最初没有指定这一点,但是可以在保留类型注释的同时做到这一点吗?到目前为止的最佳答案建议使用装饰器 - 但是,这不会保留返回类型
我有一个带变量和关联数组的函数,但我似乎无法让它们正确传递.我认为这与函数声明有关,但是我无法弄清楚它们在Perl中是如何工作的.对此有一个很好的参考,我如何实现我的需求?
我应该补充一点,它需要通过引用传递.
sub PrintAA
{
my $test = shift;
my %aa = shift;
print $test . "\n";
foreach (keys %aa)
{
print $_ . " : " . $aa{$_} . "\n";
$aa{$_} = $aa{$_} . "+";
}
}
Run Code Online (Sandbox Code Playgroud) 我正在为我的Java应用程序设计一个简单的数据访问对象.我有一些类(记录)代表表中的单行User和Fruit.
我想有一个方法来获取特定类型的所有记录.
目前我有这样的:
public List<User> getAllUsers() {
...
}
public List<Fruit> getAllFruits() {
...
}
....
Run Code Online (Sandbox Code Playgroud)
但我想有一个像这样的单一多态方法(错误):
public List<T> getAllRecords(Class<T> type) {
if(type instanceof User) {
// Use JDBC and SQL SELECT * FROM user
} else if(type instanceof Fruit) {
// Use JDBC and SQL SELECT * FROM fruit
}
return collection;
}
Run Code Online (Sandbox Code Playgroud)
用途示例:
List<Fruit> fruits = myDataAccessObject.getAllRecrods(Fruit.class);
List<User> users = myDataAccessObject.getAllRecords(User.class);
Run Code Online (Sandbox Code Playgroud)
我怎么能用Java做到这一点?
我可以将输入文本字段值传递给bean方法而不将值绑定到bean属性吗?
<h:inputText value="#{myBean.myProperty}" />
<h:commandButton value="Test" action="#{myBean.execute()} />
Run Code Online (Sandbox Code Playgroud)
我可以不进行临时保存#{myBean.myProperty}吗?
我已成功设置了一个快速测试,创建一个"类似REST"的服务,返回一个序列化为JSON的对象,这非常简单快捷(基于这篇文章).
但是,虽然返回JSON-ified对象很容易像桃子一样,但我还没有看到任何处理非基本输入参数的例子.如何将复杂对象作为参数传递?我正在使用Apache CXF,但是欢迎使用像Jackson这样的其他框架的例子:)
客户端可能类似于构建javascript对象,将其传递给JSON.stringify(complexObj),并将该字符串作为参数之一传递.
该服务可能看起来像这样
@Service("myService")
class RestService {
@GET
@Produces("application/json")
@Path("/fooBar")
public Result fooBar(@QueryParam("foo") double foo, @QueryParam("bar") double bar,
@QueryParam("object") MyComplex object) throws WebServiceException {
...
}
}
Run Code Online (Sandbox Code Playgroud)
将序列化对象作为参数发送可能会快速触及Internet Explorer强加的2KB URL限制.您是否建议在这些情况下使用POST,我是否需要在函数定义中进行更改?
可能重复:
Python中的"最小惊讶":可变默认参数
我对Python函数/方法中可选参数的工作原理感到困惑.
我有以下代码块:
>>> def F(a, b=[]):
... b.append(a)
... return b
...
>>> F(0)
[0]
>>> F(1)
[0, 1]
>>>
Run Code Online (Sandbox Code Playgroud)
为什么F(1)回归[0, 1]而不是[1]?
我的意思是,里面发生了什么?
我有两个PowerShell脚本,它们有switch参数:
编译tool1.ps1:
[CmdletBinding()]
param(
[switch]$VHDL2008
)
Write-Host "VHDL-2008 is enabled: $VHDL2008"
Run Code Online (Sandbox Code Playgroud)
compile.ps1:
[CmdletBinding()]
param(
[switch]$VHDL2008
)
if (-not $VHDL2008)
{ compile-tool1.ps1 }
else
{ compile-tool1.ps1 -VHDL2008 }
Run Code Online (Sandbox Code Playgroud)
如何在不编写大型if..then..else或case语句的情况下将switch参数传递给另一个PowerShell脚本?
我不想在参数转换$VHDL2008的compile-tool1.ps1类型为bool,因为,这两个脚本是前端脚本(由用户使用).后者是多个compile-tool*.ps1脚本的高级包装器.
我已经检查了维基百科,然后用Google搜索,但我还是无法理解ALGOL 60中的名称传递方式.
为什么我的参数不能
void example(int Array[][]){ /*statements*/}
Run Code Online (Sandbox Code Playgroud)
为什么我需要指定数组的列大小?比如说3
void example(int Array[][3]){/*statements*/}
Run Code Online (Sandbox Code Playgroud)
我的教授说它是强制性的,但我在学校开始之前编码,我记得当我把它作为我的参数时没有语法或语义错误?还是我错过了什么?
c arrays parameters parameter-passing multidimensional-array
function ×2
parameters ×2
python ×2
algol ×1
arrays ×1
c ×1
c# ×1
class ×1
cxf ×1
declaration ×1
generics ×1
history ×1
java ×1
jsf ×1
json ×1
managed-bean ×1
pass-by-name ×1
perl ×1
polymorphism ×1
powershell ×1
properties ×1