我知道它可以用Java完成,因为我过去曾广泛使用过这种技术.Java中的一个例子如下所示.(附加问题.这种技术被称为什么?如果没有名称,很难找到这样的例子.)
public abstract class Example {
public abstract void doStuff();
}
public class StartHere{
public static void main(string[] args){
Example x = new Example(){
public void doStuff(){
System.out.println("Did stuff");
}
};
x.doStuff();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我的主要问题是,这也可以在C#中完成,如果是这样,怎么样?
以下代码编译了广泛的gcc和clang版本 - 在编译并使用gcc 5.3.1运行时,它会打印出来
一个()
然后以纯虚拟调用错误中止.
#include <stdio.h>
class A
{
public:
A() {
printf("A()\n");
}
virtual void b() const = 0;
};
int main()
{
const A& a{};
a.b();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我意识到绑定对临时的引用并不理想(虽然我认为这种情况被某种生命周期扩展所覆盖) - 但它也适用于尝试调用一个带有const引用的方法,如:
Foo({});
Run Code Online (Sandbox Code Playgroud)
为方便起见,这里是一个用clang 3.2 编译的例子:Compiler Explorer
我想了解为什么编译器允许以下代码进行编译
#include <iostream>
struct A
{
A()
{
std::cout << "A::A\n";
}
virtual void f() const = 0;
};
void g(const A& a)
{
a.f();
}
int main()
{
g({});
}
Run Code Online (Sandbox Code Playgroud)
它甚至A::A在运行时输出.
如果我更换g({})与g(A())它显然不会编译.它抱怨它A是抽象的,无法实例化.Clang和GCC都在没有任何警告的情况下编译此罚款.运行时,两个版本都打印pure virtual method called并终止.
我的dll中有一个抽象类.
class IBase {
protected:
virtual ~IBase() = 0;
public:
virtual void f() = 0;
};
Run Code Online (Sandbox Code Playgroud)
我想进入IBase加载DLL的exe文件.第一种方法是创建以下功能
IBase * CreateInterface();
Run Code Online (Sandbox Code Playgroud)
并添加虚拟函数Release()在IBase.
第二种方法是创建另一个功能
boost::shared_ptr<IBase> CreateInterface();
Run Code Online (Sandbox Code Playgroud)
并且不需要任何Release()功能.
问题.
1)在第二种情况下,在dll(而不是在exe文件中)中调用析构函数和内存释放是否正确?
2)如果使用不同的编译器(或不同的设置)编译exe-file和dll ,第二种情况是否能正常工作.
这是我的示例抽象单例类:
public abstract class A {
protected static A instance;
public static A getInstance() {
return instance;
}
//...rest of my abstract methods...
}
Run Code Online (Sandbox Code Playgroud)
以下是具体实施:
public class B extends A {
private B() { }
static {
instance = new B();
}
//...implementations of my abstract methods...
}
Run Code Online (Sandbox Code Playgroud)
不幸的是我无法在B类中获取静态代码来执行,因此实例变量永远不会被设置.我试过这个:
Class c = B.class;
A.getInstance() - returns null;
Run Code Online (Sandbox Code Playgroud)
还有这个
ClassLoader.getSystemClassLoader().loadClass("B");
A.getInstance() - return null;
Run Code Online (Sandbox Code Playgroud)
在eclipse调试器中运行这两个,静态代码永远不会被执行.我可以找到执行静态代码的唯一方法是将B的构造函数的可访问性更改为public,并调用它.
我在Ubuntu 32bit上使用sun-java6-jre来运行这些测试.
可以与方法本地内部类一起使用的合法修饰符之一是abstract.
例如:
public class Outer {
public void method(){
abstract class Inner{
}
}
}
Run Code Online (Sandbox Code Playgroud)
你有实际使用它的情况吗?
你必须知道SCJP考试.
最近我在一些代码中遇到了一个奇怪的模式.我们知道所有事情都有时间和地点,特别是在涉及ABC和接口问题时,但这对我来说似乎是多余的.
// This describes a person....
public interface IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int BasePay { get; set; }
public string Address { get; set; }
}
// And so does this, but it also uses the interface....
public abstract class Person : IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int BasePay { get; set; }
public string …Run Code Online (Sandbox Code Playgroud) 我正在编写一个抽象类,因为我想提供一些常用的方法,需要一些过于具体的方法,并允许一些方法"扩展".遇到编译错误后,我想知道是否有人能解释extern,abstract和partial关键字之间的差异.他们的意思是什么,我应该在何时/何地使用它们?
这里有一个名为的界面IFish.我想用一个抽象的class(WalkingFishCommon)派生它,它提供了一个不完整的实现,因此派生的类WalkingFishCommon不必实现该CanWalk属性:
interface IFish
{
bool Swim();
bool CanWalk { get; }
}
abstract class WalkingFishCommon : IFish
{
bool IFish.CanWalk { get { return true; } }
// (1) Error: must declare a body, because it is not marked
// abstract, extern, or partial
// bool IFish.Swim();
// (2) Error: the modifier 'abstract' is not valid for this item
// abstract bool IFish.Swim();
// (3): If no declaration is provided, compiler says …Run Code Online (Sandbox Code Playgroud) 我目前正在为大学班写一个光线跟踪器.为了从文件加载场景,我写了一个sdfloader来读取sdf文件并为其创建场景.
如果我现在想编译加载程序,我会收到以下错误:
rc/sdf_loader.cpp: In member function 'void SDFloader::add_shape(std::istringstream&)':
src/sdf_loader.cpp:95:58: error: invalid new-expression of abstract class type 'box'
&scene_.materials[mat]));
^
Run Code Online (Sandbox Code Playgroud)
我试图找到一个解决方案,但失败了.
sdf_loader类如下所示:
class SDFloader {
public:
SDFloader();
~SDFloader();
Scene const& scene() const;
void read(std::string file);
private:
void add_material(std::istringstream&);
void add_shape(std::istringstream&);
void add_light(std::istringstream&);
void add_camera(std::istringstream&);
void apply_transformation(std::istringstream&);
private:
Scene scene_;
};
Run Code Online (Sandbox Code Playgroud)
在我的sdf加载器的实现中,我编写了方法read():
void SDFloader::add_shape(std::istringstream& iss) {
std::string name;
iss >> name;
if(name == "box") {
double x1,y1,z1,x2,y2,z2;
std::string mat;
iss >> name >> x1 >> y1 >> z1 >> x2 >> y2 …Run Code Online (Sandbox Code Playgroud) abstract-class ×10
c# ×4
c++ ×4
java ×2
abstract ×1
boost ×1
c++11 ×1
clang ×1
classloader ×1
dll ×1
gcc ×1
inheritance ×1
interface ×1
oop ×1
pure-virtual ×1
shared-ptr ×1
singleton ×1