对于我在PHP中进行的新项目,我创建了一个SQLMethods类来连接数据库并执行查询.今晚是我实际测试它的第一个晚上(我在大约一个星期前写了它并忘了它)并发生意外错误:当它调用我的ExecuteQuery()函数时,它不会使用数据库我在构造函数中选择.
构造函数:
public function SQLMethods() {
$SQLConnection = mysql_connect($SQLDBAddress, $SQLUserName, $SQLPassword);
if (!$SQLConnection) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($SQLDB, $SQLConnection);
}
Run Code Online (Sandbox Code Playgroud)
有问题的功能:
public function ExecuteQuery($Query) {
mysql_query($Query, $SQLConnection) or die('Could not perform query: ' . mysql_error());
}
Run Code Online (Sandbox Code Playgroud)
有谁看到问题可能是什么?构造函数完成后连接是否关闭?
更新:所以这里的每个人都告诉我,我只需要重新开始我如何设计课程(顺便说一句,谢谢大家的优秀答案!).接下来,我开始对战略模式进行广泛阅读.我想创建从抽象基类继承的行为类(或策略类).然后,Candidate类将具有不同抽象基类/类的属性作为Type行为或策略.也许是这样的:
public abstract class SalaryStrategy {
public abstract decimal Salary { get; set; }
public abstract decimal Min { get; set; }
public abstract decimal Mid { get; set; }
public decimal CompaRatio {
get {
if (this.Mid == 0) { return 0; }
else { return this.Salary / this.Mid; }
}
}
}
public class InternalCurrentSalaryStrategy {
public override decimal Salary { get; set; }
public override decimal Min {
get { return this.Salary * …Run Code Online (Sandbox Code Playgroud) c# inheritance code-reuse design-patterns multiple-inheritance
我想坚持不要重复自己的原则,但有时当我将PHP与HTML和CSS一起编写时,如果我在不同的情况下重复使用相同的代码,我的代码很快会有很多 - 如果 - 然后 - 否则代码不易维护.
如果使用模板引擎Smarty,这可能是一个更大的问题,因为大多数代码编辑器都不匹配{if} {else} {/ if}所以程序员需要在视觉上寻找匹配的标签,并且不容易当有3或4级嵌套{if} {else} {/ if}时.
在这种情况下,有没有办法坚持DRY,但仍然有良好的可维护代码?
if ( $_GET['tab'] == 'newest' ) {
// Go through each question
foreach( array_reverse( $end_array, true ) as $tags_and_Qid['question_id'] => $titles_and_Qid['title'] )
{
// Grab the title for the first array
$title = $titles [ $tags_and_Qid['question_id'] ] ['title'];
// Grab the tags for the question from the second array
$tags = $end_array [ $tags_and_Qid['question_id'] ] ['tag'];
// Grab the username for the question from the second array
$username = $usernames [ $tags_and_Qid['question_id'] ] ['username'];
--- cut ----
}
} …Run Code Online (Sandbox Code Playgroud) 假设我在C++中有以下类层次结构:
class AbstractBaseClass
{
public:
// Note: Class is completely abstract! No data!
AbstractBaseClass() {}
virtual ~AbstractBaseClass() {}
virtual std::string definedInVirtual() = 0;
virtual std::string definedInDerived() = 0;
};
class Virtual : public virtual AbstractBaseClass
{
public:
Virtual() {}
virtual ~Virtual() {}
std::string definedInVirtual()
{
return "definedInVirtual";
}
};
class DerivedA : public Virtual
{
public:
DerivedA() {}
virtual ~DerivedA() {}
std::string definedInDerived()
{
return "definedInDerivedA";
}
};
class DerivedB : public Virtual
{
public:
DerivedB() {}
virtual ~DerivedB() {} …Run Code Online (Sandbox Code Playgroud) 这里有类似的帖子.我只想澄清一些事情.我在测试项目中实现了这一点.我创建了两个页面page1.aspx和page.aspx是除名称之外的彼此的副本.我对它们进行了配置,以便它们使用相同的后端编码文件.
page1.axpx - > page.aspx.cs page2.aspx - >(上面的代码)
我的问题是,
是避免额外代码和增强维护是一个好主意吗?
虽然每个人都在上面的测试项目中工作.在我的实际项目中,我得到了这个非常常见的错误
当前上下文asp中不存在名称"xxxx"
由于上述错误,代码将无法编译.如果我从浏览器运行应用程序,它确实有效(或似乎是).我的问题是,为什么我没有在一个应用程序中而不是另一个应用程序中收到此错误.此问题现已修复.但是想知道为什么我首先得到这个错误,然后在这里和那里玩它,错误就消失了.
我相信这确实是微软的错误.怎么样?我在这里解释一下.
Page1.aspx.cs实际上是使用Page.aspx页面而不是page1.aspx控件.intellisence只显示Page1.aspx控件(如果你将控件添加到page2,它不会显示在intellesense中,如果你将它添加到Page1,它将被显示).由于两个页面上的控件完全相同(包括名称和ID),因此它们奇迹般地起作用.有时编译器不喜欢它并且会给你错误(没有明显的原因).微软应该更优雅地解决这些问题,而不是把它扔给我们.
所以它解决了我,但任何人都可以解释这种神秘的行为.
假设我有从EventArgs派生的这个类
public class myclass01:EventArgs
{
public string channel{get;set;}
public string generic_property{get;set;}
}
public class myclass02:EventArgs
{
public string extension{get;set;}
public string generic_property{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能使它不必"generic_property"一次又一次地定义派生的类EventArgs?有点像我将拥有一个具有所有通用属性的基类.
我一直听到人们说测试应该如何简单,可维护,直截了当,但是在单元测试中代码重用性会发生什么?
我们举个例子:
def test_some_1():
...some code
def test_some_2():
...code repeated from test_some_1
Run Code Online (Sandbox Code Playgroud)
将两个测试中重复的代码封装在一个包含必要断言的函数中是不是最好?
我和一些程序员争论过这个并且他们不同意,他们说测试应该是愚蠢的,代码重用性在这里并不好.这样做的原因是因为在django控制台中断言实际上失败了并不是很清楚,因为断言是在函数中,虽然我不同意,因为使用它与鼻子会给你测试的名称和追溯,虽然这些家伙再次不同意,说明可以在没有鼻子的情况下单独调用测试(因此你无法看到所有这些细节).
你们有什么感想?
我在我的XML文件中定义了一个大的搜索查询
<select id="searchItems" resultMap="myMap" parameterType="map">
SELECT
a lot of field
FROM multiple table with few joins
WHERE with few conditions
LIMIT x,y
</select>
Run Code Online (Sandbox Code Playgroud)
上面的查询使用limit来返回分页结果并避免在搜索中返回整个项目.但是对于一个要求,我还需要返回查询找到的项目总数.
我的问题是:我如何重用上面的查询而只选择count(*),显然没有LIMIT?有没有办法分隔每个查询部分并在<select>标记中重用它们?
我找到了这个老答案.我想知道解决方案是否仍然有效,或者是否有更新,更有效的方法.
让我们假设我有一个像下面这样的迭代器(细节并不重要,只是它很大):
class inorder_iterator : public std::iterator<std::forward_iterator_tag, token>
{
friend syntax_tree;
node* current_node;
std::stack<node*> prev_nodes;
//std::stack<node*> visited_nodes;
std::map<node*, bool> visited;
public:
inorder_iterator();
inorder_iterator& operator++();
inorder_iterator operator++(int);
token& operator*();
const token& operator*() const;
token* operator->();
const token* operator->() const;
friend bool operator==(const inorder_iterator lhs, const inorder_iterator rhs);
friend bool operator!=(const inorder_iterator lhs, const inorder_iterator rhs);
private:
inorder_iterator(node* current);
node* find_leftmost_node(node* from);
};
Run Code Online (Sandbox Code Playgroud)
成员函数声明的实现具有合理的大小,但我想重用当前的迭代器来减少代码重复.
想到的第一个想法就是在node类型上进行模板化,所以我可以通过const node使它成为const迭代器,但它听起来很腥
template <typename Node>
//replace every occurrence …Run Code Online (Sandbox Code Playgroud) code-reuse ×10
php ×3
.net ×2
c# ×2
c++ ×2
asp.net ×1
c++14 ×1
class ×1
count ×1
django ×1
django-nose ×1
dry ×1
duplicates ×1
inheritance ×1
mybatis ×1
mysql ×1
python ×1
scope ×1
sql ×1
unit-testing ×1