在下面的代码中,若要在子child类中对父类的addvalue方法进行重写,下面对于child类中的addvalue方法的声明哪个是正确的:( )。 class father { public int addvalue(int a, int b) { int s = a + b; return s; } } class child extends father { } A. public int addvalue(int i,int j) B. void addvalue(int a,int b) C. void addvalue(double i) D. int addvalue(int a)
时间: 2024-03-05 15:53:34 浏览: 26
A public int addvalue(int i,int j) 是正确的。
在子类中重写父类的方法,需要保持方法名、参数列表和返回类型相同。因此,在子类 `child` 中对 `addvalue` 方法进行重写,需要按以下方式声明:
```java
public int addvalue(int a, int b) {
// 子类中对父类方法进行重写
int s = a + b;
return s;
}
```
因此,选项 A 是正确的。
相关问题
如果在cpp代码中判断子类有没有重写父类的某个虚接口
在 C++ 代码中判断子类有没有重写父类的某个虚函数,可以使用 typeid 运算符和 dynamic_cast 运算符。
使用 typeid 运算符可以获取对象的类型信息,并且可以使用 typeid(类名) 获取类型信息。如果一个子类重写了父类的虚函数,那么对于子类的对象来说,使用 typeid 获取到的类型信息应该是子类的类型。
使用 dynamic_cast 运算符可以进行运行时的类型转换,并且如果该转换失败,会返回一个空指针。如果一个子类重写了父类的虚函数,那么对于子类的对象来说,使用 dynamic_cast 将该对象转换成父类指针时,返回的结果应该是一个非空指针。
示例代码如下:
```cpp
class Parent {
public:
virtual void func() {}
};
class Child : public Parent {
public:
virtual void func() override {}
};
int main() {
Child c;
Parent& p = c;
if (typeid(p) == typeid(Child)) {
std::cout << "Child has overridden the virtual function of Parent." << std::endl;
}
if (dynamic_cast<Child*>(&p) != nullptr) {
std::cout << "Child has overridden the virtual function of Parent." << std::endl;
}
return 0;
}
```
在这个例子中, 因为c为 Child 类型,转换成 Parent 引用 会输出 Child has overridden the virtual function of Parent.
在这两种方法中,dynamic_cast 通常
如果在cpp代码中判断子类重写父类的哪些虚接口,没有重写哪些虚接口
在 C++ 代码中,可以使用 dynamic_cast<> 来判断子类是否重写了父类的虚函数。如果该子类对象重写了该虚函数,dynamic_cast<> 将返回该子类对象的指针,否则返回 nullptr。
以下是一个示例:
```
class Parent {
public:
virtual void foo() { ... }
};
class Child : public Parent {
public:
virtual void foo() { ... }
};
int main() {
Parent* p = new Child();
Child* c = dynamic_cast<Child*>(p);
if (c != nullptr) {
std::cout << "The child class has overridden the foo() function of the parent class." << std::endl;
} else {
std::cout << "The child class has not overridden the foo() function of the parent class." << std::endl;
}
return 0;
}
```
在这个例子中,程序将输出“The child class has overridden the foo() function of the parent class.”,因为 Child 类重写了 Parent 类的 foo() 函数。
需要注意的是,当父类的虚函数没有在子类中重写时,dynamic_cast 并不能识别出来。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)