我有很多代码,如下所示,我明确地实现了接口所需的一些事件.
public class IMicrowaveNotifier {
event EventHandler<EventArgs> DoorClosed;
event EventHandler<EventArgs> LightbulbOn;
// ...
}
public class Microwave : IMicrowaveNotifier {
private EventHandler<EventArgs> _doorClosed;
event EventHandler<EventArgs> IMicrowaveNotifier.DoorClosed {
add { lock (this) _doorClosed += value; }
remove { lock (this) _doorClosed -= value; }
}
private EventHandler<EventArgs> _lightbulbOn;
event EventHandler<EventArgs> IMicrowaveNotifier.LightbulbOn {
add { lock (this) _lightbulbOn += value; }
remove { lock (this) _lightbulbOn -= value; }
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
你可以看到很多这是样板.在Ruby中,我可以做这样的事情:
class Microwave
has_events :door_closed, :lightbulb_on, ...
end
Run Code Online (Sandbox Code Playgroud)
在C#中有没有类似的更短的方法来删除这个样板? …
我编写了一个解析命令行参数的程序或算法.首先,需要指定有效的参数及其属性(标志,分隔符等).
现在,我有一个辅助函数返回:: Tuple<Tuple<Argument, String>, String>
元组中的第一个元素(也是一个元组)包含一个"键值对",null
如果找不到对.第二个元素包含一个被标识为选项的字符串,而分隔符是" "
(意味着该值将是命令行中的下一个元素)
该函数的代码如下:
private Tuple<Tuple<Argument, String>, Argument> f(string e, List<Argument> valid) {
var p = valid.Find( x => { return e.StartsWith( x.value ); } );
if ( p == null )
return new Tuple<Tuple<Argument, String>, Argument>( null, null );
if ( p.flag )
return new Tuple<Tuple<Argument, String>, Argument>( new Tuple<Argument, String>( p, "" ), null );
if ( p.separator.Equals(" ") && p.value.Length == e.Length )
return new Tuple<Tuple<Argument, String>, Argument>( null, …
Run Code Online (Sandbox Code Playgroud) 我发现互联网上的一段代码有一个非常简单的目标,但它使用了一种丑陋的方法.据推测,作者使用switch case来确定先前定义的Enum的一些(非连续)值是否属于其范围.如果是,则函数返回true
,就是这样.否则,它会返回false
.
它实际上看起来像这样:
switch(value) {
case ONE:
case TWO:
/* many similar lines later */
case TWENTY:
case TWENTY_FIVE:
/* an afternoon later */
case ONE_HUNDRED:
return true;
default:
return false;
}
Run Code Online (Sandbox Code Playgroud)
由于编译器生成的跳转表,即使跳转表并不一定意味着从我收集的内容中进行即时查找,因此使用开关盒是合理的,因为它具有即时查找功能.即便如此,这会产生无数不必要的代码行.
我已经阅读了函数内联和使用函数指针数组,但我不知道如何在这种特定情况下使用它.
我如何避免用这样一个简单的案例写出许多行case X:
(没有双关语)?
html5 Boilerplate中的fav图标不起作用(不显示)
我已经在localhost和服务器上尝试了它,但在这两种情况下它都没有用.是什么让它发挥作用.?
我在许多烧瓶应用程序中都有一些错误处理调用.例如,我的404响应是使用@app.errorhandler
装饰器定义的:
@app.errorhandler(404)
def page_not_found(e):
return jsonify({'status': 'error',
'reason': '''There's no API call for %s''' % request.base_url,
'code': 404}), 404
Run Code Online (Sandbox Code Playgroud)
由于我有大量的样板代码,我想将它放在一个公共文件中,并从一个地方继承或导入我的烧瓶应用程序.
是否可以从其他模块继承或导入烧瓶样板代码?
我正在使用asp.net样板来创建新项目.
我已定义新服务如下:
public class Employee : Entity<int>
{
public string FName { get; set; }
public string LName { get; set; }
}
public interface IEmployeeAppService : IApplicationService
{
Employee AddEmployee(Employee emp);
List<Employee> GetAll();
}
public class EmployeeAppService : MyTestProjectAppServiceBase, IEmployeeAppService
{
private IRepository<Employee, int> _employeeRepository;
public EmployeeAppService(IRepository<Employee, int> repo)
{
_employeeRepository = repo;
}
public Employee AddEmployee(Employee emp)
{
return _employeeRepository.Insert(emp);
}
public List<Employee> GetAll()
{
return _employeeRepository.GetAllList();
}
}
Run Code Online (Sandbox Code Playgroud)
我想在HomeController中使用该服务:
public class HomeController : MyTestProjectControllerBase
{ …
Run Code Online (Sandbox Code Playgroud) 从开箱即用的 HTML5 Boilerplate 安装中,我使用以下代码
body
{
background-image:url('img/bg.png');
background-repeat:repeat-y;
}
Run Code Online (Sandbox Code Playgroud)
在 style.css 文件中,没有出现背景图片,它仍然保持空白。
完整的 css 在这里
/* HTML5 Boilerplate */
article, aside, details, figcaption, figure, footer, header, hgroup, nav, section { display: block; }
audio, canvas, video { display: inline-block; *display: inline; *zoom: 1; }
audio:not([controls]) { display: none; }
[hidden] { display: none; }
html { font-size: 100%; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
html, button, input, select, textarea { font-family: sans-serif; color: #222; }
body { margin: 0; font-size: 1em; …
Run Code Online (Sandbox Code Playgroud) 我正在使用haskell和gtk2hs启动GUI.我有一个笔记本小部件,我想用"F1,F2 ... F11"键切换页面.
我的工作代码是:
import Control.Monad.Trans (liftIO)
import Graphics.UI.Gtk
main = do
initGUI
builder <- builderNew
builderAddFromFile builder "M62.glade"
window <- builderGetObject builder castToWindow "window1"
notebook <- builderGetObject builder castToNotebook "notebook1"
window `on` keyPressEvent $ tryEvent $ do "F1" <- eventKeyName
liftIO $ notebookSetCurrentPage notebook 0
window `on` keyPressEvent $ tryEvent $ do "F2" <- eventKeyName
liftIO $ notebookSetCurrentPage notebook 1
window `on` keyPressEvent $ tryEvent $ do "F3" <- eventKeyName
liftIO $ notebookSetCurrentPage notebook 2
window `on` keyPressEvent $ …
Run Code Online (Sandbox Code Playgroud) 我正在阅读Google C++ Style Guide以尝试模仿良好的编码实践。当我通读关于如何评论我的代码的部分时,它要求我用license boilerplate开始每个文件。我所能找到的关于这个术语的所有信息都来自Mozilla以及我认为是他们的评论标准。许可证样板注释究竟是什么,它需要采用什么格式?
注意:明确地说,我并不是说 Google C++ 风格指南是遵循良好编码实践的完美指南。我只是用它来获得关于如何改进我的编码风格的新想法。
我正在写这样的样板:
this.lposItems = new LposItems[5];
final LposItems lposItem = new LposItems();
lposItem.setA("24341");
lposItem.setB("14");
final LposItems lposItem1 = new LposItems();
lposItem1.setA("62");
lposItem1.setB("49");
final LposItems lposItem2 = new LposItems();
lposItem2.setA("40");
lposItem2.setB("4");
final LposItems lposItem3 = new LposItems();
lposItem3.setA("62");
lposItem3.setB("63");
final LposItems lposItem4 = new LposItems();
lposItem4.setA("14");
lposItem4.setB("4");
final LposItems lposItem5 = new LposItems();
lposItem5.setA("15");
lposItem5.setB("4");
this.lposItems[0] = lposItem;
this.lposItems[1] = lposItem1;
this.lposItems[2] = lposItem2;
this.lposItems[3] = lposItem3;
this.lposItems[4] = lposItem4;
this.lposItems[5] = lposItem5;
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我任何可以减少这种情况的第三方库吗?(像现成的建造者或其他一些技术)
boilerplate ×10
c# ×2
c++ ×2
asp.net ×1
asp.net-mvc ×1
builder ×1
code-reuse ×1
coding-style ×1
comments ×1
css ×1
events ×1
favicon ×1
flask ×1
generics ×1
gtk2hs ×1
haskell ×1
html5 ×1
java ×1
jump-table ×1
python ×1
refactoring ×1