如何在继承两个接口的类中重写两个不同接口中具有相同名称和签名的函数。是否可以 ?
注:这是我被问到的一个面试问题。
例子:
interface example1
{
int add(int a, int b);
}
interface example2
{
int add(int a, int b);
}
public class cls : example1, example2
{
public int add(int a, int b)
{
int c = a + b;
return c;
}
}
Run Code Online (Sandbox Code Playgroud) 我在 C# 中遇到了一种情况,当我需要多重继承以避免在实现类中重复代码时,但这在 C# 中是被禁止的。
我应该怎么办?
我有以下接口:
public interface ICar
{
void Move();
void GetService();
}
public interface ISuperCar : ICar
{
void MoveFast();
void HaveARace();
}
Run Code Online (Sandbox Code Playgroud)
和抽象类:
public abstract class Car : ICar
{
public void Move()
{
Console.WriteLine("Car moves");
}
public abstract void GetService();
}
public abstract class SuperCar : Car, ISuperCar
{
public void MoveFast()
{
Console.WriteLine("SuperCar moves fastly");
}
public abstract void HaveARace();
}
Run Code Online (Sandbox Code Playgroud)
以及他们的实现:
public class ToyotaCar : Car
{
public override void GetService()
{ …Run Code Online (Sandbox Code Playgroud) #include<iostream>
using namespace std;
class A
{
int a;
int b;
public:
void eat()
{
cout<<"A::eat()"<<endl;
}
};
class B: public A
{
public:
void eat()
{
cout<<"B::eat()"<<endl;
}
};
class C: public A
{
public:
void eat()
{
cout<<"C::eat()"<<endl;
}
};
class D: public B, C
{
};
int foo(A *ptr)
{
ptr->eat();
}
main()
{
D obj;
foo(&(obj.B)); //error. How do i call with D's B part.
}
Run Code Online (Sandbox Code Playgroud)
上面的foo调用是编译时错误.我想用obj的B部分调用foo而不使用虚拟继承.我怎么做.
此外,在虚拟继承的情况下,为什么偏移信息需要存储在vtable中.这可以在编译时自己确定.在上面的例子中,如果我们用D的对象传递foo,在编译时我们只能计算D的A部分的偏移量.
在followint代码中,指针转换和多继承如何一起发挥作用?
class Foo {
public:
virtual void someFunc();
};
class Bar;
void someWork(Bar *bar) {
((Foo*) bar)->someFunc();
}
class Bar: public Zed, public Foo {
...
virtual void someFunc() { ... do something else ... }
}
Bar bar;
int main() {
someWork(&bar);
}
Run Code Online (Sandbox Code Playgroud)
我的理解有点不稳定.
一方面,有些工作对Bar一无所知,所以这不应该工作; 但另一方面,我已经向前宣布了Bar.
谢谢!
c++ virtual-functions multiple-inheritance forward-declaration
这是关于js中的oop的一些问题(下面的代码中的问题).
<html>
<script>
function A(){
a = 'a - private FROM A()';
this.a = 'a - public FROM A()';
this.get_a = function(){
return a;
}
}
function B(){
this.b = 'b - private FROM B()';
this.a = 'a - public FROM B() ';
}
C.prototype = new A();
C.prototype = new B();
C.prototype.constructor = C;
function C() {
A.call(this);
B.call(this);
}
var c = new C();
//I've read paper about oop in Javacscript but they never talk
//(the ones have …Run Code Online (Sandbox Code Playgroud) 我正在使用许多抽象类,它们只定义纯虚函数和虚拟(非纯)析构函数.
没有使用虚拟继承,是否仍然可以使用钻石继承结构?(我想确保即使某些程序员不知道他应该使用虚拟继承也没有问题.)什么是一个很好的资源,它提供了一个简短而完整的主题概述?
谢谢!
我已经进行了一段时间的编码,这可能是一个容易的错误,我只是忽略了睡眠不足,但问题发生在这段代码.
do
{
aWithIntAcct.enterAccountData();
aWithIntAcct.getSavInfo();
aWithIntAcct.getCheckingInfo();
checkAcct.push_back(aWithIntAcct);
cout << "Would you like to enter another Checking Account with interest? y or n ";
cin >> quitChar;
}while(quitChar != QUIT);
Run Code Online (Sandbox Code Playgroud)
它说我有'enterAccountData'的模糊访问(错误C2385)这与其他(错误C3861)标识符没有找到相矛盾.
我的类是继承的,所以我不知道为什么这个为我工作.有什么建议?
其余代码:
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
template <class T>
void repeatValue(T val, int times)
{
for(int x = 0; x < times; x++)
{
cout << "-";
}
};
template <class T>
void produceReport(int tableRows, string title, T acctType)
{
cout …Run Code Online (Sandbox Code Playgroud) 据说,Java语言只支持单继承.但是如何同时从Object和任何其他类继承?这不是一个多重继承.
其次,我们需要继承所有11个Object方法?我很难想象为什么我在I/O中需要它们,例如
最后,JDK 8将为我们提供接口中的默认方法实现,以及是否可能导致Java中的多重继承.
如果接口A提供方法a()具有默认实现并且接口B也提供带有另一个默认实现的a()方法并且我们的自定义类C实现两个接口并依赖于默认实现 - 那不是Diamond of Death吗?
我在C中遇到了经典的多重继承问题.
我创建了源文件Stack.c和Queue.c.它们都包含一个文件Node.c(包含分配和释放内存的函数).现在,我正在尝试在单个文件中实现另一个程序,我需要包含Stack.c和Queue.c.我试图#include这两个文件,但编译器抛出了一个冲突的类型错误.
这样做最正确的方法是什么?
提前致谢!!
我目前正在编写一个程序,根据不同的参数搜索歌曲.在我的系统中有两种类型的歌曲:歌词和乐器.因为我需要将它们都放在1个向量中,所以我有一个歌曲类和一个LyricsSong&InstrumentalSong子类.
所以我有一个Song.h文件:
#include <stdio.h>
#include <iostream>
#include <string>
class Song
{
public:
std::string title;
virtual void print();
virtual void printSong(std::string query);
};
Run Code Online (Sandbox Code Playgroud)
还有乐器和歌词子类,它们以这种方式定义:
class LyricsSong : public Song
class InstrumentalSong : public Song
Run Code Online (Sandbox Code Playgroud)
两者都包括Song.h,在这两个类中,类只在头文件中定义.
当我尝试运行另一个使用这两个子类的文件时,包括:
#include "LyricsSong.h"
#include "InstrumentalSong.h"
Run Code Online (Sandbox Code Playgroud)
(显然更多的cpp库),我得到以下编译错误:
In file included from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/InstrumentalSong.h:16:0,
from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/songsParser.cpp:26:
/cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/Song.h:6:7: error: redefinition of 'class Song'
class Song
^
In file included from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/LyricsSong.h:15:0,
from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/songsParser.cpp:25:
/cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/Song.h:6:7: error: previous definition of 'class Song'
class Song
^
Run Code Online (Sandbox Code Playgroud)
什么时候: …
c++ ×5
inheritance ×4
class ×3
c# ×2
include ×2
c ×1
interface ×1
java ×1
javascript ×1
oop ×1
overriding ×1
polymorphism ×1
syntax ×1
templates ×1