泛型类可以继承吗 C#关于泛型接口继承写法问题疑惑
泛型类可以继承吗
看我的例子
class Father
T t
Father(T t){
this.t=t
}
public void fatherPrint(){
System.out.println("fathers " t)
}
}
class Son
Son(T t) {
super(t)
}
public void sonPrint(){
System.out.println("sons " t)
}
}
public class Test {
public static void main(String[] args){
Father
f.fatherPrint()
Son
s.sonPrint()
}
}
C#关于泛型接口继承写法问题疑惑
T只是一个占位符 它代表一个类 且只在他所处的范围中有效
以上代码 T只在interface中有意义
在class Base中 是没有T这个东西的 此处你使用的是U 你就要用U 你可以把U 换成T的
java中泛型继承的问题
public interface TestQuery extends BasicQuery{ } public class TestEntity extends BasicEntity { } 业务需要这么定义吧。
c 泛型 模板继承问题
。。。。。。。
VS2012,给你重新写一个。
#include
#include
#include
enum class Type {
ADD,
SUB
}
template
class Operator {
public:
void setX(const Op & x) {
_x = x
}
void setY(const Op & y) {
_y = y
}
virtual Op result() = 0
virtual ~Operator() {}
public:
Op _x
Op _y
}
template
class Add : public Operator
typedef Operator
public:
Op result() override {
return Base::_x Base::_y
}
}
template
class Sub : public Operator
typedef Operator
public:
Op result() override {
return Base::_x - Base::_y
}
}
class Factory {
public:
template
static Operator
switch(type) {
case Type::ADD:
return new Add
case Type::SUB:
return new Sub
default:
throw std::invalid_argument("Factory::create")
}
}
}
int main() {
Operator
op->setX(1)
op->setY(2)
std::cout << "add = " << op->result() << std::endl
Operator
op2->setX(2)
op2->setY(1)
std::cout << "sub = " << op2->result() << std::endl
system("pause")
}