对于最佳实践,使用return "red"
模拟未知值的代码是否更好?或者我应该添加一个unknown("red")
选项return Shape.unknown.color;
吗?关于未知值的枚举是否有标准的Java约定?
private enum Shape {
triangle("yellow"),
square("green"),
circle("red");
private final String color;
Shape(String color) {
this.color = color;
}
};
public String getShapeColor() {
if(shape != null) {
return shape.color;
}
return "red";
}
Run Code Online (Sandbox Code Playgroud) 我对Ada很新,我正在尝试用一些文字做一些简单的工作.我想要做的就是读取文件,并删除任何不是字母,空格或新行的内容.所以删除所有的标点符号和数字.在其他语言中,我只想创建一个简单的[^ a-zA-Z]正则表达式,查看每个字符并删除它,如果它适合RegEx,但我似乎无法在Ada中找到有关RegEx的任何文档.那么,在Ada有RegEx吗?如果没有,那么对我来说这样的简单文本编辑的最佳方式是什么.
非常感谢,-jb
我正在根据屏幕上对象的位置处理对象的鼠标点击.我记录了鼠标单击的xy坐标,看它是否与允许点击的任何对象相匹配.对象在不同的列表中或只是单个对象,但是我希望它们在一个大的列表中,所以我可以遍历整个事物一次,如果点击其中一个对象,那么就完成工作,中断.您只能单击一个对象.
第一种方法:我现在是怎么做的:
list = [obj1, obj2, obj3]
singleobj
copylist = list
copylist.append(singleobj)
for item in copylist:
if item.pos == mouseclick.pos:
doWork(item)
break
Run Code Online (Sandbox Code Playgroud)
第二种方法:我宁愿做下面的事情,但显然这list+singleobj
是无效的:
for item in list+singleobj:
if item.pos == mouseclick.pos:
doWork(item)
break
Run Code Online (Sandbox Code Playgroud)
第三种方法:或者如果我绝对必须,我可以做这个可怕的可怕代码:
list = [obj1, obj2, obj3]
foundobj = None
for item in list:
if item.pos == mouseclick.pos:
foundobj = item
break
if foundobj is None:
if singleobj.pos == mouseclick.pos:
foundobj = singleobj
#possibly repeated several times here....
if foundobj is not None:
doWork(foundobj)
Run Code Online (Sandbox Code Playgroud)
第一种方法似乎很慢,因为我必须将所有(可能很多)列表和单个对象复制到一个列表中.
第二种方法看起来很理想,因为它结构紧凑,易于维护.虽然,现在它只是伪代码. …
这是一个组成的例子,当有很多参数时它会变得更有用.
这会让来电者使用new Person("Jim", 1950, 10, 2)
或new Person("Jim", datetimeobj)
.我知道可选参数,这不是我在这里寻找的.
在C#我可以这样做:
public Person(string name, int birthyear, int birthmonth, int birthday)
:this(name, new DateTime(birthyear, birthmonth, birthday)){ }
public Person(string name, DateTime birthdate)
{
this.name = name;
this.birthdate = birthdate;
}
Run Code Online (Sandbox Code Playgroud)
我可以在PHP中做类似的事情吗?就像是:
function __construct($name, $birthyear, $birthmonth, $birthday)
{
$date = new DateTime("{$birthyear}\\{$birthmonth}\\{$birthyear}");
__construct($name, $date);
}
function __construct($name, $birthdate)
{
$this->name = $name;
$this->birthdate = $birthdate;
}
Run Code Online (Sandbox Code Playgroud)
如果这是不可能的,那么什么是好的选择呢?
我仍然是Python的新手.我听到有人说使用is
,不是==
因为"这不是C".但我有一些代码x is 5
,它没有按预期工作.
因此,遵循正确的Python/PEP样式,何时使用is
以及何时使用==
?
请注意这是关于作业问题,功课已完成并符合规范,但它很慢,所以我试图减少我做的SQL调用次数.我还在学习SQL-fu.
我的表看起来像:
Ticker |Name |Industry
A |Agilent Technologies |Information Technology
AA |Alcoa Inc |Materials
AAPL |Apple Inc. |Information Technology
Run Code Online (Sandbox Code Playgroud)
现在我在循环中运行这样的SQL语句.每次迭代我都会改变哪个行业.
SELECT ticker FROM company WHERE industry = 'Information Technology'
Run Code Online (Sandbox Code Playgroud)
但这意味着我必须为每个行业调用数据库,以获得与之相关的代码.我想要的是进行一次返回多个结果的数据库调用.因此,第一个结果将是第一个行业的所有代码清单,第二个结果将是第二个行业的所有代码.
我尝试过这个SQL,但它只给了我一个行业的代码,而不是一个完整的列表
SELECT ticker, industry FROM company GROUP BY industry
Run Code Online (Sandbox Code Playgroud) 自从我完成C++以来已经有一段时间了,所以我在这里遇到了一些麻烦.我allQueue
在主文件中声明的行上收到此错误.我显然已经删除了许多我认为不需要的代码,如果你需要更多信息请告诉我.
用.编译
g++ mainFile.cpp MyClass.cpp extraObjectFile.o -o mainFile
Run Code Online (Sandbox Code Playgroud)
产生:
error: expected constructor, destructor, or type conversion before ‘<’ token
Run Code Online (Sandbox Code Playgroud)
主文件
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <iostream>
#include "MyClass.h"
vector<MyClass> allQueue;
int main()
{
allQueue.push_back(new MyClass(100));
}
Run Code Online (Sandbox Code Playgroud)
MyClass.cpp
#include "MyClass.h"
MyClass::MyClass(int start_priority)
{
priority = start_priority;
}
int MyClass::getPriority()
{
return priority;
}
Run Code Online (Sandbox Code Playgroud)
MyClass.h
class MyClass
{
int priority;
public:
MyClass(int);
int getPriority();
};
Run Code Online (Sandbox Code Playgroud)