我有一个实现3个接口的抽象类.
public abstract class ServiceBaseCore<Entity, EntityKey> : MarshalByRefObject, IComponentService, IEntityProvider<Entity, EntityKey>
where Entity : IEntityId<EntityKey>, new()
where EntityKey : IEntityKey, new()
{
// Provided functionality/body to some methods of interface
}
Run Code Online (Sandbox Code Playgroud)
问题:我得到的错误是我的抽象类没有为接口函数提供实现(定义/正文),其中我读到的是"如果一个类是抽象的,那么就不需要为所有/任何函数提供主体界面实施".
注意:代码是由codeSmith生成的,即使它显示错误.
请告诉我我错在哪里以及我错过了什么.
谢谢
我真的很困惑,而且我已经阅读了很多关于这个主题的问题,而且我无法明确指出一个接口可以做抽象类无法做到的事情.
接口可以做什么,抽象类不能做?
我问我的Java类的情况下,但它是否适用于其他语言随意删除Java标签(可能是C#?).
编辑:我知道抽象类可以做界面不能做的事情,但是如果抽象类可以完成界面可以做的所有事情,那么界面的重点是什么?"实现多个接口"是什么意思?
我可以创建一个像下面这样的抽象类吗?
abstract class A{
private final String foo;
private final String too;
public A(final String foo, final String too) {
this.foo= foo;
this.too= too;
}
public String getfoo(){
return foo;
}
public String gettoo(){
return too;
}
}
Run Code Online (Sandbox Code Playgroud) 所以我有abstract class Worker,它有抽象功能computePay.
两个孩子的类此类的是HourlyWorker和FixedWorker,现在我已经提供的实现computePay在这两个classes.Now我的代码说
Worker[] w=new worker[2];
w[0]=new HourlyWorker;
w[1]=new FixedWorker;
Run Code Online (Sandbox Code Playgroud)
现在,当我说我w[0].computePay如何确定调用哪个computePay时,我知道将调用子类中的那个,但是哪一个?
即如果我的两个子类具有不同的computePay函数实现,下面的代码会给我想要的结果吗?
w[0].computePay //Prints the pay as per the implementation in HourlyWorker;
w[1].computePay //Prints the pay as per the implementation in FixedWorker;
Run Code Online (Sandbox Code Playgroud)
我也听说过instance of运营商/关键字,但我不知道它会在这里有用吗?
我喜欢这样的场景
这是抽象类
abstract class A
{
public void add()
{
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
这是扩展到抽象类之上的类
class B extends A
{
@Override
public void add()
{
//do something else
}
}
Run Code Online (Sandbox Code Playgroud)
这是我想要调用add方法的类
class C
{
A a = new B();
// Calls B's add method
a.add();
// Call A's add method ???
}
Run Code Online (Sandbox Code Playgroud)
如何调用A的添加方法???
我有一个SomeTaskManager抽象方法的类runATask.我想runATask通过反射执行方法,这是我的代码:我错过了什么?
SomeTaskManager pm= (SomeTaskManager)context.getSomeTaskManager();
Class c = Class.forName( pm.getClass().getName() );
Method[] allMethods = c.getDeclaredMethods();
for (Method m : allMethods) {
if (!m.getName().equals("runATask")) {
continue;
}
m.invoke( c ,new Object[] { someParam, null, 1});
break;
}
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误
java.lang.IllegalArgumentException: object is not an instance of the class
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at de.vogella.android.downloadmanager.DownloadManagerActivity.riflesso(DownloadManagerActivity.java:250)
at de.vogella.android.downloadmanager.DownloadManagerActivity.onCreate(DownloadManagerActivity.java:68)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
at android.app.ActivityThread.access$1500(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at …Run Code Online (Sandbox Code Playgroud) 我有一个名为的抽象类PersonAbstract.它有一个名为的抽象属性Age
abstract public int Age { get; set; }
Run Code Online (Sandbox Code Playgroud)
两个类调用Customer并Employee扩展PersonAbstract.
Customer并Employee使用Linq-to-SQL创建,因此它们具有自动生成的代码.
这两个的自动生成代码:
public int Age
{
get
{
return this._age;
}
set
{
this._age = value;
}
}
Run Code Online (Sandbox Code Playgroud)
我向部分类添加功能Employee并Customer使用它.
public partial class Employee: PersonAbstract
{
.....
Run Code Online (Sandbox Code Playgroud)
如何更改此结构,以便继承的类(Employee和Customer)使其person属性覆盖抽象类中的person属性而不触及自动生成的代码?
编辑:
问题是我希望两个类共享一些父类的代码.所以我一直在尝试在实现具有Age属性的接口和两个子类之间添加带有共享代码的抽象类.但该代码需要访问该Age属性.因此抽象类必须使用Age属性的抽象实例来实现接口,子类重写它.如果这不可能,我怎样才能达到预期的效果呢?
我创建了一个使用scala的项目,该scala具有一些抽象类定义,例如:
abstract class A(n: String) {
require(n!= null)
val name = n
var inputs: List[Input[_]]
var outputs: List[Output[_]]
}
abstract class B(n: String) extends A(n){
def act
def isValid : Boolean
def getCs : List[C[_]]
}
abstract class C[Type]{
var value: Type
}
Run Code Online (Sandbox Code Playgroud)
现在我想在Java中使用该项目.我在一个新项目中导入了scala lib,并在构建路径中添加了scala项目.我正在使用eclipse.
但是,当我尝试这样做时:
public class CoolClass extends B {
public CoolClass(){ }
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我在Java中使用它时遇到了一些问题,这让我有些疑惑.让我列举一下:
getCs所以它提供的接口List<C>而不是List<C<?>>?我想定义一个抽象基类,然后传递一个这种类型的数组(显然是一个派生类的实例)作为一个函数参数,但是编译器对我大吼大叫.有任何想法吗?
例如("Testable"是抽象的,"Vecteur"是具体的):
void Testeur::commencerTest(Testable testables[], int nTestables, string titre) {
cout << "\n" << titre << "\n";
for (int i=0; i < nTestables; i++) {
testables[i].afficher();
}
}
// in main function:
Vecteur v1 = Vecteur(1,2,3);
Vecteur v2 = Vecteur(4,5,6);
Vecteur vecteurs[] = { v1, v2 };
int nVecteurs = 2;
this->commencerTest(vecteurs, nVecteurs, "Some text");
Run Code Online (Sandbox Code Playgroud)
编译器invalid abstract type ‘std::Testable’ for ‘testables’在上面代码的第一行说.
如何将抽象类型数组作为函数参数传递?
c++ polymorphism inheritance abstract-class parameter-passing
以下代码使用UIGestureRecognizer:
UIGestureRecognizer *gestureRecog = [[UIGestureRecognizer alloc]
initWithTarget:self
action:@selector(handletap:)];
[self.view addGestureRecognizer:gestureRecog];
Run Code Online (Sandbox Code Playgroud)
实际上可以编译和运行.我以为抽象类无法实例化?
abstract-class ×10
java ×6
inheritance ×3
abstract ×2
c# ×2
interface ×2
polymorphism ×2
android ×1
c++ ×1
extends ×1
ios ×1
iphone ×1
objective-c ×1
reflection ×1
scala ×1