假设我已经在Visual Studio 2010中构建了一个现有的.NET程序集,在其他一些Winforms产品中使用它.它没有第三方依赖.
我想直接在MonoTouch解决方案中重用该库.
就目前而言,为了能够在我的MonoTouch UI项目中添加对我的库的引用,我必须创建一个新的csproj文件并创建指向其中所有源文件的链接.
这有效,但是维护负担.肯定有更好的办法?
提前致谢.
看看这段代码:
IDfSessionManager manager = DfcSessionManager.getSessionManager();
try {
IDfSession session = manager.getSession(DfsUtils.getCurrentRepository());
...
return somewhat; //May be without return statement
} finally {
if (session != null) {
manager.release(session);
}
}
Run Code Online (Sandbox Code Playgroud)
这种结构重复多次并围绕不同的代码。这可以是带有或不带有 return 语句的方法。我想为这个 try-finally 块制作一些可重用的东西。
我想出了这样的认识。
public abstract class ISafeExecute<T> {
private IDfSession session = null;
protected abstract T execute() throws DfException;
public T executeSafely() throws Exception {
IDfSessionManager manager = DfcSessionManager.getSessionManager();
try {
session = manager.getSession(DfsUtils.getCurrentRepository());
return execute();
} finally {
if (session != null) {
manager.release(session);
}
} …Run Code Online (Sandbox Code Playgroud) 我使用node_clone模块效果很好但我的项目中需要使用自定义模块中的node_clone函数.所以我输入以下代码:
module_load_include('inc', 'node_clone', 'clone.pages');
function mymodule_init(){
clone_node_save(118);
}
Run Code Online (Sandbox Code Playgroud)
该代码返回Fatal error: Call to undefined function clone_node_save().
我的模块按来源分类为标记为mine和contrib的目录.Node_save在contrib中,而myModule在我的中.
所以,我按照以下方式修改了代码:
module_load_include('inc', '../../contrib/node_clone', 'clone.pages');
Run Code Online (Sandbox Code Playgroud)
但我得到了同样的错误.
有人能突出我做错了吗?
我有一个具有一些功能的基本类型,包括特征实现:
use std::fmt;
use std::str::FromStr;
pub struct MyIdentifier {
value: String,
}
impl fmt::Display for MyIdentifier {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value)
}
}
impl FromStr for MyIdentifier {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(MyIdentifier {
value: s.to_string(),
})
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个简化的例子,真正的代码会更复杂。
我想介绍两种类型,它们具有与我描述的基本类型相同的字段和行为,例如MyUserIdentifier和MyGroupIdentifier。为避免在使用这些时出错,编译器应将它们视为不同的类型。
我不想复制我刚写的整个代码,我想重用它。对于面向对象的语言,我会使用继承。我将如何为 Rust 做到这一点?
我的公司使用mod_perl,axkit和apache的组合开发web应用程序.我们在unix操作系统中都有大量的Perl模块,javascripts等.
每当我需要编写一个新函数时,我会尝试进行一些代码重用,但事情就是所有的Perl模块和javascripts分散在各个文件夹中.
我讨厌以后才编写一些代码,发现已经有一个函数做了同样的事情,我当前的方法是尝试grep表名,看看是否有冗余函数,但除此之外,我几乎会只是放弃并编写一个新功能,因为我不想花太多时间搜索和找不到任何东西.
有一个更好的方法吗?或者甚至更好,我可以安装免费软件工具,它可以帮助我正确管理所有功能和模块,甚至允许开发人员记录评论等.
有这样的讨论在第C检测代码的重复使用++工具,我们有这样的事情在UNIX平台的Perl代码?
谢谢〜史蒂夫
我有一组实现特定接口的类,我有一组复选框.如果没有选中复选框,我想抛出一个错误.如果选择了至少一个或多个复选框,则应创建与该复选框关联的对象.
这就是我的表现.
interface U { ... }
class A implements U { ... }
class B implements U { ... }
class C implements U { ... }
class Main {
//....
//....
public void findSelectedCheckBoxesAndCreateObjects() {
if(!(checkboxA.isSelected() || checkboxB.isSelected() || checkboxC.isSelected()) {
System.out.println("No checkboxes selected");
return;
}
//if any selected, create associated object
if(checkboxA.isSelected()) new A(file);
if(checkboxB.isSelected()) new B(file);
if(checkboxC.isSelected()) new C(file);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我有3个问题.
|| checkboxD.isSelected()每次我有一个新类来检查它时,我都不能继续添加.if(checkboxD.isSelected()) new D(file);为每节课增加.它非常不优雅.我可以使用某种循环来删除冗余代码吗?
请给我你的建议.谢谢.
我有类似的公式来计算哈特利变换.唯一的区别是输入函数 - sin,cos,exp在以下代码行中:
Math.Exp((double)tau)
Math.Sin((double)tau)
Math.Cos((double)tau)
Run Code Online (Sandbox Code Playgroud)
如何在以下片段中逃避几乎相同的代码片段并缩短我的代码?
private void CountHartley(ref double [] arr, string function)
{
int N = arr.Length;
if (function == "exp")
{
for (int nu = 0, tau = 0; ((nu < N) && (tau < N)); nu++, tau++)
{
arr[nu] = 1 / (double)N *
Math.Exp((double)tau) *
(Math.Sin(2 * Math.PI * nu * tau / (double) N) +
Math.Cos(2 * Math.PI * nu * tau / (double) N));
}
}
else if (function == "sin")
{ …Run Code Online (Sandbox Code Playgroud) 我正在使用JSP和Spring框架.在页面中,我想在相应的select元素旁边有五个按钮.我的问题是如何避免重复代码呢?
以下是我对一个按钮选择组合的代码:
<tr>
<td>
<sf:label path="inputFile">Select a file:</sf:label>
</td>
<td>
<sf:select path="inputFile" cssStyle="width: 250px;" onchange="enableLocal(this.selectedIndex,'matButton')">
<sf:option value="Upload a Local File" />
<sf:option value=" --- Available already: --- " disabled="true" />
<sf:options items="${flowData.availableInputs}" />
</sf:select>
</td>
<td>
<input id="matButton" type="file" name="inputFile"/>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,名称inputFile和matButton将被视为参数,即其他按钮将具有不同的名称.
我想到的是类似于具有两个参数的函数,它们将生成上述代码.可能吗?
我需要打印定期报告和批量报告.我正在使用podofo库.我计划为每个报告使用单独的类,但每个类将需要下面的一些常用函数(现在在另一个项目的一个类中).
int CPdfBatchReport::CalculateMaxRowsInEmptyPage(void)
{
int rows = MaxPageHeight / PDF_TABLE_STANDARD_ROW_HEIGHT;
// because the first will always be the column header in every page, we substrct 1 to account for that
rows = rows - 1;
return rows;
}
// Calculates the max rows in current page. The current page is determined
// by the current x, y position
int CPdfBatchReport::CalculateMaxRowsInCurrentPage(void)
{
float AvailablePageHeight = pPage->GetPageSize().GetHeight() - PDF_BOTTOM_MARGIN - y;
int rows = AvailablePageHeight / PDF_TABLE_STANDARD_ROW_HEIGHT;
// because the first will …Run Code Online (Sandbox Code Playgroud) 为什么这个实现:
T& T::operator+=( const T& ) {
// ... implementation ...
return *this;
}
T operator+( const T& lhs, const T& rhs ) {
T temp( lhs );
return temp += rhs;
}
Run Code Online (Sandbox Code Playgroud)
比这更频繁地传播:
T& T::operator+=( const T& rhs ) {
*this = *this + rhs;
return *this;
}
T operator+( const T& lhs, const T& rhs ) {
// ... implementation ...
return some_result;
}
Run Code Online (Sandbox Code Playgroud)
是否有任何理由,或者只是一个随机的巧合,我看到人们在我阅读的文献中多次这样实现它,而从来没有反过来?
code-reuse ×10
c++ ×2
java ×2
architecture ×1
c# ×1
coding-style ×1
csproj ×1
drupal ×1
drupal-7 ×1
include-path ×1
javascript ×1
jcheckbox ×1
jsp ×1
oop ×1
perl ×1
rust ×1
swing ×1
try-catch ×1
types ×1
xamarin.ios ×1