方法1 通过语言技巧来实现 在Ca中设置个标志位flag,如果Ca()构造flag = 1,else flag = 0 在execA() { if (flag == 1) throw new Exception(); } 方法2 通过面向对象来实现 class Ca { } class Ca_child1: Ca { Ca_child1(){} }class Ca_child2: Ca { Ca_child2(int i){} void execA(int i){} } 使用: Ca ca1 = new Ca_child1(); (Ca_child1)ca1.execA();//编译器报错 Ca ca2 = new Ca_child2(20); (Ca_child2)ca2.execA();//正确
通过语言技巧来实现
在Ca中设置个标志位flag,如果Ca()构造flag = 1,else flag = 0
在execA()
{
if (flag == 1)
throw new Exception();
}
方法2
通过面向对象来实现
class Ca
{
}
class Ca_child1: Ca
{
Ca_child1(){}
}class Ca_child2: Ca
{
Ca_child2(int i){}
void execA(int i){}
}
使用:
Ca ca1 = new Ca_child1();
(Ca_child1)ca1.execA();//编译器报错
Ca ca2 = new Ca_child2(20);
(Ca_child2)ca2.execA();//正确