我有以下 4 个课程:
public abstract class Base
{
abstract public void DoIt();
}
public class TW: Base {}
public class RW: TW
{
public override void DoIt() {}
}
public class IW: TW
{
public override void DoIt() {}
}
Run Code Online (Sandbox Code Playgroud)
用法:
var transaction = new TW();
transaction.DoIt();
Run Code Online (Sandbox Code Playgroud)
我不清楚为什么会出现以下错误:
错误 CS0534 'TW' 没有实现继承的抽象成员 'Base.DoIt()'
如果所有“叶子”派生类(RW 和 IW)都覆盖它,为什么(TW)还需要覆盖它?我可能会遗漏一些东西,因为我记得这不是必须的。我不想在 TW 中覆盖它,因为我没有任何有效的实现可以在那里编写。
我期待有能力写出类似的东西:
public override abstract void DoIt();
Run Code Online (Sandbox Code Playgroud)
什么是正确的实现?
更新:
根据我得到的答案,由于某些限制,我必须按以下方式实施它:
public override void DoIt()
{
//Not getting to this code, it is overriden …Run Code Online (Sandbox Code Playgroud) 我有一个C#类,其方法如下:
public class MyType
{
public MyType Clone()
{
var clone = (MyType)MemberwiseClone();
// Do some other stuff here with the clone's properties
return clone;
}
}
Run Code Online (Sandbox Code Playgroud)
我有一堆其他类,我想实现Clone方法所以我想我可以创建一个抽象基类,我可以在其中一般定义Clone方法,所以我不必在每个类中放置具体的实现.
我认为这是可能的,但是我没有对泛型做太多工作,而且我过去(几个月前,因为沮丧而丢弃我的代码)的尝试也没有成功.
这可能吗?如果是这样,怎么办呢?
我读到一个抽象类仍然可以有一个表.但我对它在vtable中会有多少条目感到困惑.例如,如果我的抽象类是:
class Circle(){
virtual void draw() = 0;
}
Run Code Online (Sandbox Code Playgroud)
然后在其vtable中有多少条目?另外,我是否正确地说这个抽象类在其vtable中有1个条目?谢谢你的帮助.
class Circle(){
virtual double a{ return 0.0; }
virtual void draw() = 0;
}
Run Code Online (Sandbox Code Playgroud) 提出了类似的问题,但他们没有回答我的问题.我正在尝试创建Abstract类(使用一个纯虚函数).但编译器给出了这个错误:
src/library.cpp:11:24:错误:无效转换为抽象类类型'mgc :: Shapes'形状(长度,名称);
我有Abstract类的构造函数,因为它有成员变量和成员函数.我不知道为什么会出现这个错误..
这是我的头文件librar.h
class Shapes
{
public:
Shapes(double len = 0.0, std::string name = "");
virtual void printClass();
virtual double area() = 0;
protected:
double len_, width_;
std::string name_;
};
class Triangle : public Shapes
{
public:
Triangle(double lene =0, std::string namee= "" );
void printClass();
double area();
};
Run Code Online (Sandbox Code Playgroud)
这是我的实现文件library.cpp
mgc::Shapes::Shapes(double length, std::string name)
: len_(length),
width_(length),
name_(name)
{}
mgc::Triangle::Triangle(double length, std::string name)
{
Shapes(length, name);
}
void mgc::Shapes::printClass()
{
std::cout<<"Base class is called"<<std::endl;
}
void mgc::Triangle::printClass() …Run Code Online (Sandbox Code Playgroud) 所以我正在做一些关于制作一个小游戏原型的课程.我有这些简单的类(以及其他一些不相关的类):
abstract class Weapon {
int damage;
int cost;
}
abstract class RangedWeapon extends Weapon {
int range;
int rounds;
}
class ExtraRounds extends Item{
int cost = 20;
int uses = 1;
void use(GameState state){
if (state.currentCharacter.weapon instanceof RangedWeapon){
state.currentCharacter.weapon.rounds += 10;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是在尝试编译时,我得到了
Implementations.java:56: error: cannot find symbol
state.currentCharacter.weapon.rounds += 10;
^
symbol: variable rounds
location: variable weapon of type Weapon
Run Code Online (Sandbox Code Playgroud)
我想要的只是班级ExtraRounds来检查是否weapon是班级RangedWeapon并采取相应的行动,但我不知道哪里出了问题.任何帮助表示赞赏
我有一个没有抽象方法的abstract类......如何测试这个?我可以简单地将它导入测试类并照常开展业务吗?
例:
public abstract class SomeAbstractClass implements SomeOtherClass {
// Some variables defined here
private static final String dbUrl = System.getProperty("db.url");
// Some public methods
public String doSomethingToUrl(String url) {
url = url + "/takeMeSomewhereNice";
}
}
Run Code Online (Sandbox Code Playgroud)
说我在ARG通为db.url的localhost:8080,我想测试的doSomethingToUrl方法做输出新的字符串...它仍然会是这种格式?
public class TestUrl {
SomeAbstractClass sac = new SomeAbstractClass();
@Test
public void testUrlChange() throws Exception {
String testUrl = "localhost:8080";
assertThat("localhost:8080/takeMeSomewhereNice",
sac.doSomethingToUrl(testUrl));
}
}
Run Code Online (Sandbox Code Playgroud) 我正在研究一个C#项目来解析不同类型的文件.为了做到这一点,我创建了以下类结构:
interface FileType {}
class FileType1 : FileType {}
class FileType2 : FileType {}
abstract class FileProcessor {}
class Processor_FileType1 : FileProcessor {} // Will use FileType1 - type of storage class
class Processor_FileType2 : FileProcessor {} // Will use FileType2 - type of storage class
Run Code Online (Sandbox Code Playgroud)
所以,由于每个都FileProcessor使用不同的类型FileType,我希望在我的Base FileProcessor类中编写某种方法,以便能够从文件中获取值,其内容如下:
abstract class FileProcessor
{
protected List<T> getValuesFromFile<T>() where T:FileType
{
try
{
otherClass.doProcess<T>();
}
catch (Exception ex)
{
throw new Exception("Unable to retrieve the data from the …Run Code Online (Sandbox Code Playgroud) 我为2d游戏创建了一个抽象的形状类,但我在两个形状类中都出错.该错误与super()有关.可能还有其他错误.我还显示了我在代码中得到错误的位置.IS super()适合使用.
形状类
public abstract class Shape {
int Y;
int WIDTH;
int HEIGHT;
int DIAMETER;
public Shape(int Y, int WIDTH, int HEIGHT, int DIAMETER) {
this.Y = Y;
this.WIDTH = WIDTH;
this.HEIGHT = HEIGHT;
this.DIAMETER = DIAMETER;
}
public abstract void paint(Graphics g);
}
Run Code Online (Sandbox Code Playgroud)
球拍类
public class Racquet extends Shape {
int x = 0;
int xa = 0;
private Game game;
public Racquet(int Y, int WIDTH, int HEIGHT) {
super(Y, WIDTH, HEIGHT); // <- **Error Here**
}
public …Run Code Online (Sandbox Code Playgroud) 我知道创建抽象类背后的概念用法,即为其子类定义一个公共接口,其中一些实现留给各个子类.
我是否正确地假设技术上没有必要抽象类,因为你可以覆盖超类方法?是否创建了抽象类以使类的意图更清晰?
我的意思是:
// Using an abstract class
abstract class Car
{
int fuel;
int getFuel()
{
return this.fuel;
}
abstract String getColor();
}
class RedCar extends Car
{
String getColor()
{
return "red";
}
}
// Without an abstract class
class Car
{
int fuel;
int getFuel()
{
return this.fuel;
}
String getColor()
{
return "defaultColor";
}
class RedCar extends Car
{
String getColor()
{
return "red";
}
}
Run Code Online (Sandbox Code Playgroud) #include <vector>
class M {
public:
M(unsigned int);
unsigned int n;
};
M::M(unsigned int i) { n = i; }
class A {
protected:
char t;
public:
virtual ~A();
virtual std::vector<M> foo(unsigned int);
char getChar();
};
A::~A(){}
std::vector<M> A::foo(unsigned int u) {
std::vector<M> v;
return v;
}
char A::getChar() { return t; }
class B : public A {
public:
B();
std::vector<M> foo(unsigned int);
};
B::B() { t = 'b'; }
std::vector<M> B::foo(unsigned int c) {
std::vector<M> v;
for (unsigned …Run Code Online (Sandbox Code Playgroud) abstract-class ×10
java ×4
c# ×3
c++ ×3
generics ×2
inheritance ×2
c++03 ×1
c++11 ×1
class ×1
instanceof ×1
junit ×1
object ×1
oop ×1
pure-virtual ×1
shapes ×1
super ×1
testing ×1
virtual ×1
vptr ×1
vtable ×1