考虑这个类层次结构:
Book extends GoodsBook implements Taxable我们知道,子类与其超类(is-a)之间存在关系.
问:有没有像"是一个"之间的任何关系Book和Taxable?
GOOD Answers,但是你说"is-a"也是Book和之间的关系Taxable,但是 "is-a"是类之间的关系,而接口不是类!
假设我们有这个超级简单的类hierchy:
public class SomeMath
{
public int Add(int x, int y)
{
return x + y;
}
}
public class MoreMath : SomeMath
{
public int Subtract(int x, int y)
{
return x - y;
}
}
Run Code Online (Sandbox Code Playgroud)
Add在为MoreMath类编写测试时,我应该为该方法编写测试吗?或者我在测试SomeMath课程时是否应该只关心这种方法?更一般地说:我应该测试一个类的所有方法,还是应该只测试"新"方法?
我可以想出双方的一些理由.例如,在测试所有方法时,您最终会不止一次地测试相同的东西,这不是很好,而且可能变得乏味.但是,如果你不测试所有方法,改变SomeMath可能会破坏用法MoreMath?这也是一件坏事.我想这也可能取决于案例.就像它扩展了一个类,我可以控制或不控制.但无论如何,我是一个全新的测试新手,所以我很想知道人们比我想的更聪明:-)
可能重复:
在C++中选择重载模板函数时的优先级
模板化功能使我可以方便地操作各种类型:
template<typename T> void destroy(T* obj) {
delete obj;
}
Run Code Online (Sandbox Code Playgroud)
但是在某些时候我想对类层次结构做一些专门化:
class Base { virtual void doSomething(); };
class Derived : public Base {};
void destroy(Base* obj) {
obj->doSomething();
delete obj;
}
Run Code Online (Sandbox Code Playgroud)
如果我传递确切类型Base*,则调用了预期的专用函数,但是重载决策的规则似乎更喜欢通用的模板化版本而不是执行静态向上转换,如果我传递Derived*给void detroy().
当然,我可以为所有可能的派生类型创建所有重载函数,但它的可维护性较差.
我正在使用Visual C++ 2008,有什么方法可以解决上述问题吗?
默认情况下,CakePHP有一个AppModel类,应用程序的每个模型都继承自它.在模型之间共享逻辑的常见设计模式是创建行为并$actAs为该行为配置模型.
但是,如果我想引入这样的模型类层次结构呢?:
AppModel
|__ Vehicle
|__ Car
|__ Bike
|__ Helicopter
Run Code Online (Sandbox Code Playgroud)
我试图创建一个Vehicle继承自的类,AppModel然后每个子类都继承自Vehicle.但CakePHP告诉我它无法找到这个类Vehicle.
我怎么能创建这个以及CakePHP目录树中应该创建的位置Vehicle?
谢谢!
我有这个所需的类层次结构:
interface IClass
{
string print(IClass item);
}
class MyClass : IClass
{
// invalid interface implementation
// parameter type should be IClass not MyClass
string print(MyClass item)
{ return item.ToString(); }
}
Run Code Online (Sandbox Code Playgroud)
我尝试通过使用泛型类型解决接口实现问题,但没有成功:
interface IClass
{
string print<T>(T item) where T : IClass;
}
class MyClass : IClass
{
string print<T>(T item) where T : MyClass
{ return item.ToString(); }
}
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
class Foo(object):
def whee(self):
return 77
class Bar(Foo):
def whee(self):
return super(Bar, self).whee() + 1
class Baz(Foo):
def whee(self):
return super(self.__class__, self).whee() + 1
Run Code Online (Sandbox Code Playgroud)
双方Bar并Baz返回相同的结果whee().我习惯了语法Bar.有什么理由我不应该使用语法Baz吗?
class Parent {};
class Child : public Parent {};
class Foo
{
public:
Foo (Parent &) {};
template <typename T>
Foo (const T &);
};
int main ()
{
Child c;
Foo foo (c);
}
Run Code Online (Sandbox Code Playgroud)
这将产生一个链接错误,因为构造函数foo选template<typename T>Foo::Foo(const T &)来代替Foo::Foo(Parent&)。
如果c类型Parent为Child,则使用非模板构造函数,并且链接没有问题。
我可以解决这个问题
Foo foo ((Parent&) c);
Run Code Online (Sandbox Code Playgroud)
但是我不想那样做。
为什么C ++更喜欢使用模板而不是隐式转换c为Parent&?
我可以将类更改为偏向于铸造而不是模板,因此不需要解决方法吗?
我有一个如下的层次结构:
case class A(val a: Long, val b: String)
case class B(val c: String) extends A(a=3, b="a string")
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用lift-json序列化它以下:
val obj = B(c="another string")
val cameraJson = net.liftweb.json.Serialization.write(obj)
Run Code Online (Sandbox Code Playgroud)
但我所看到的是它只序列化了B类中的属性而不是A中的属性.
我也尝试过:
compact(render(decompose(obj)))
Run Code Online (Sandbox Code Playgroud)
结果相同
是什么赋予了?Scala中有一些我不知道的东西吗?
在C++中,为什么纯 virtual法强制要求其强制压倒一切的只有它的直接孩子(创建对象),而不是到孙子女等等?
struct B {
virtual void foo () = 0;
};
struct D : B {
virtual void foo () { ... };
};
struct DD : D {
// ok! ... if 'B::foo' is not overridden; it will use 'D::foo' implicitly
};
Run Code Online (Sandbox Code Playgroud)
离开这个功能我没有看到什么大不了的.
例如,在语言设计的角度来看,只有当它有一些明确的语句时才struct DD允许使用D::foo它using D::foo;.否则它必须覆盖foo强制性.
在C++中有这种效果的实用方法吗?
c++ overriding virtual-functions class-hierarchy language-lawyer
class-hierarchy ×10
c++ ×3
c# ×2
templates ×2
cakephp ×1
casting ×1
inheritance ×1
interface ×1
java ×1
lift ×1
model ×1
overloading ×1
overriding ×1
poster ×1
python ×1
python-2.7 ×1
qt ×1
qt4 ×1
resolution ×1
scala ×1
unit-testing ×1