标签: class-hierarchy

实现接口的类和该接口之间是否有任何关系?

考虑这个类层次结构:

  • Book extends Goods
  • Book implements Taxable

我们知道,子类与其超类(is-a)之间存在关系.

问:有没有像"是一个"之间的任何关系BookTaxable

GOOD Answers,但是你说"is-a"也是Book和之间的关系Taxable,但是 "is-a"是之间的关系,而接口不是类!

java interface class-hierarchy

4
推荐指数
2
解决办法
5136
查看次数

儿童班的单元测试

假设我们有这个超级简单的类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# unit-testing class-hierarchy

4
推荐指数
1
解决办法
1816
查看次数

关于模板化类型和类层次结构的c ++函数重载决策

可能重复:
在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,有什么方法可以解决上述问题吗?

c++ templates overloading resolution class-hierarchy

4
推荐指数
1
解决办法
916
查看次数

在CakePHP中定义模型类的层次结构

默认情况下,CakePHP有一个AppModel类,应用程序的每个模型都继承自它.在模型之间共享逻辑的常见设计模式是创建行为并$actAs为该行为配置模型.

但是,如果我想引入这样的模型类层次结构呢?:

AppModel
  |__ Vehicle
        |__ Car
        |__ Bike
        |__ Helicopter
Run Code Online (Sandbox Code Playgroud)

我试图创建一个Vehicle继承自的类,AppModel然后每个子类都继承自Vehicle.但CakePHP告诉我它无法找到这个类Vehicle.

我怎么能创建这个以及CakePHP目录树中应该创建的位置Vehicle

谢谢!

inheritance cakephp model class-hierarchy

4
推荐指数
1
解决办法
866
查看次数

4
推荐指数
1
解决办法
3520
查看次数

c#用子类类型的参数实现接口方法

我有这个所需的类层次结构:

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)

我该怎么办?

c# class-hierarchy

4
推荐指数
2
解决办法
3551
查看次数

在Python中正确使用super - 我应该明确引用类名吗?

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)

双方BarBaz返回相同的结果whee().我习惯了语法Bar.有什么理由我不应该使用语法Baz吗?

python class-hierarchy python-2.7

4
推荐指数
1
解决办法
56
查看次数

如何使C ++更喜欢匹配父类重载而不是模板?

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)

这将产生一个链接错误,因为构造函数footemplate<typename T>Foo::Foo(const T &)来代替Foo::Foo(Parent&)

如果c类型ParentChild,则使用非模板构造函数,并且链接没有问题。

我可以解决这个问题

Foo foo ((Parent&) c);
Run Code Online (Sandbox Code Playgroud)

但是我不想那样做。

为什么C ++更喜欢使用模板而不是隐式转换cParent&

我可以将类更改为偏向于铸造而不是模板,因此不需要解决方法吗?

c++ templates casting class-hierarchy

4
推荐指数
1
解决办法
86
查看次数

案例类层次结构的lift-json序列化

我有一个如下的层次结构:

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中有一些我不知道的东西吗?

serialization scala lift class-hierarchy

3
推荐指数
2
解决办法
1616
查看次数

强制所有类在多级继承层次结构中实现/覆盖"纯虚拟"方法

在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::foousing D::foo;.否则它必须覆盖foo强制性.

在C++中有这种效果的实用方法吗?

c++ overriding virtual-functions class-hierarchy language-lawyer

3
推荐指数
1
解决办法
1万
查看次数