abstract class B extends A implements C {
// why I have to add implementation here for add() function
}
class A{
void add(){
}
}
interface C{
void add();
}
Run Code Online (Sandbox Code Playgroud)
我期望我不需要add()在抽象类中实现该方法B。为什么会发生这种情况?
我将如何在C++中执行以下操作(以下代码是C#):
class Base
{
public virtual void Foo()
{
// do stuff...
}
}
class C : Base
{
public override void Foo()
{
base.Foo(); // <=== how do you do this line?
}
}
Run Code Online (Sandbox Code Playgroud) 我想在C#中使用Java样式多态.可能吗?
这是一个不编译的例子
using System;
namespace HelloWorld
{
public class Program
{
public static void Main (string[] args)
{
Triangle triangle = new Triangle(2);
Square square = new Square(3);
printID(square);
}
public void printID(Shape s){
Console.WriteLine ("id is " + s.id);
}
}
public class Shape{
public int id;
}
public class Triangle: Shape{
float b;
float height;
float area(){
return b*height/2;
}
public Triangle(int k){
id=k;
}
}
public class Square: Shape{
float side;
float area(){
return side*side;
}
public …Run Code Online (Sandbox Code Playgroud) 在C#中,我有一个带有公共成员的父类.我想派生父类,然后派生公共成员的类,以便创建和访问新方法,如下所示......
public class Animal { }
public class Sheep : Animal {
public void makeALamb() { }
}
public class Farm
{
public Animal myAnimal;
}
public class SheepFarm : Farm {
public void SheepFarm() {
this.myAnimal = new Sheep();
this.myAnimal.makeALamb();
}
}
Run Code Online (Sandbox Code Playgroud)
此代码无法编译."Animal不包含makeALamb()的定义".但我想做的是多态的本质,不是吗?我错过了什么?我非常期待找到答案.
提前致谢!
请帮助我找到以下两个问题的答案,均在java面试中问到:
在这种情况下,将使用new关键字在JVM堆栈中分配内存(堆中没有任何内容)。
在这种情况下,方法重载将基于返回类型,方法名称和参数相同(我回答说在Java中是不可能的)
根据我的知识和从Google的发现,两者都无法完成,我的答案是:
New将始终在堆中分配内存,并且可以通过堆栈中的引用进行引用
重载取决于编译时间,如果不遵循以下情况,它将给编译器错误
但是他没有被说服。