所以我有这个:
std::vector<EnemyInterface*> _activeEnemies;
Run Code Online (Sandbox Code Playgroud)
EnemyInterface看起来像这样:
#include "Ogre.h"
class EnemyInterface{
public:
virtual void update(const Ogre::Real deltaTime) = 0;
virtual void takeDamage(const int amountOfDamage, const int typeOfDamage) = 0;
virtual Ogre::Sphere getWorldBoundingSphere() const = 0;
virtual ~EnemyInterface(){}
};
Run Code Online (Sandbox Code Playgroud)
我创造了一个新的敌人:
// Spikey implements EnemyInterface
activeEnemies.push_back( (EnemyInterface*) &Spikey(_sceneManager, Ogre::Vector3(8,0,0)) );
Run Code Online (Sandbox Code Playgroud)
我想在每个敌人身上调用更新功能,但它崩溃了:
// update enemies
for (std::vector<EnemyInterface*>::iterator it=_activeEnemies.begin(); it!=_activeEnemies.end(); ++it){
(**it).update(timeSinceLastFrame); // Option 1: access violation reading location 0xcccccccc
(*it)->update(timeSinceLastFrame); // Option 2: access violation reading location0xcccccccc
}
Run Code Online (Sandbox Code Playgroud)
我可以在屏幕上看到敌人,但我无法访问它.任何帮助,将不胜感激.
Spikey.h看起来像这样:
#include "EnemyInterface.h"
class Spikey: virtual public …Run Code Online (Sandbox Code Playgroud) 我有几个关于接口的问题.
为什么我们不能将virtual关键字与Interfaces成员一起使用
为什么我们不能在接口的派生类中使用override关键字
假设
interface Iface
{
void Func();
}
class Program : Iface
{
static void Main(string[] args)
{
}
public void Func()
{
Console.WriteLine("In func");
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我需要在接口(即Func()定义中)对派生类中的成员函数使用public?如果我不使用public关键字,则会导致编译时错误
我们可以在Interface中使用静态成员吗?
我不确定这两个签名是否有任何真正的区别:
public static class MyCustomExtensions
{
public static bool IsFoo(this IComparable<T> value, T other)
where T : IComparable<T>
{
// ...
}
public static bool IsFoo(this T value, T other)
where T : IComparable<T>
{
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这些操作几乎完全相同,但我不太确定......我在这里俯瞰什么?
这个问题的背景可以从我之前的问题中找到.
上一个问题:http://tinyurl.com/chq4w7t
我有一个Comm带发送功能的界面:
public interface Comm
{
public int send(Socket socket, byte[] bytes);
}
Run Code Online (Sandbox Code Playgroud)
我具有各种类(Server,Client,Serial它实现了接口等)Comm.我可以将这些类对象作为参数传递给另一个类中的另一个发送函数,该类充当Comm对象和各种插件之间的管理器,这些插件可配置为使用这些Comm类之一作为通信介质.
(Server,Client,Serial,等等)可以被作为参数传递到下面的发送功能
public void Send(Comm com, Socket socket, byte[] message)
{
com.send(null, message);
}
Run Code Online (Sandbox Code Playgroud)
从我之前的问题我有一个getClasses函数,它返回一个Class[]并将String作为参数.这用于提供不同的配置选项.
我用Class.forName("Client");例如返回ClassClient 的对象.
现在最后我的问题如下:
如何转换Class为Comm类型?我做了以下尝试以获得一个想法:( cboxComm是一个用于测试我的代码的测试组合框.它包含Comm对象的类名)
// Some code I have no idea how …Run Code Online (Sandbox Code Playgroud) 您是否有充分的理由可以在Java类中拥有或不具有内部接口?(就像你会内心一样).我无法找到谷歌的确切答案,但看起来好像你不能像使用内部类一样嵌入界面.我的猜测是Java创建者没有看到使其成为可能的一个很好的理由,所以它不是,但也许有很好的理由?
例如我无法编译(这是一大类)
package buttonGetSourceObject;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ClassWithButton implements ActionListener, ButtonOwner{
private String objectName = "";
public ClassWithButton(String string) {
objectName = string;
JFrame f = new JFrame();
JPanel p = new JPanel();
MyJButton b = new MyJButton(this,"Press This Button for Name of Owner of Button");
b.addActionListener(this);
f.add(p);
p.add(b);
f.pack();
f.setVisible(true);
}
public static void main(String[] args){
ClassWithButton c = new ClassWithButton("object1name");
}
@Override
public void actionPerformed(ActionEvent arg0) {
((ButtonOwner)((MyJButton)arg0.getSource()).getOwner()).printInstanceName();
} …Run Code Online (Sandbox Code Playgroud) public interface RMIInterface extends Remote {
public byte[] geScreen() throws RemoteException;
public byte[] getProcessList() throws RemoteException;
public boolean execute(String command) throws RemoteException;
public boolean messageTo(String msg,String user) throws RemoteException;
public boolean saveImage(byte[] image,String user) throws RemoteException;
public byte[][] getimages(String user,String date)throws RemoteException;
}
Run Code Online (Sandbox Code Playgroud)
上面的RMI接口方法抛出RemoteException,如果我将这些方法包含在try中,并且捕获RemoteException就可以了.
我们可以使用变量和接口方法而无需使用关键字"Implements".
注意:接口和类在同一个包中.
提前致谢..!!!
我已经实现了iEquatable接口:
LineItem : IEquatable<LineItem>
Run Code Online (Sandbox Code Playgroud)
但现在我想Equals(...)通过单步调试我的方法.但即使在调试模式下,踩入也不会进入它(即F11),并且在方法中放置一个断点也不会让我进入它.我怎么调试呢?
不是它应该是相关的,但这是我的Equals方法:
public bool Equals(LineItem other)
{
List<bool> individuals = new List<bool>();
individuals.Add(DateTime.Equals(Expiry, other.Expiry));
individuals.Add(Code == other.Code);
individuals.Add(Type == other.Type);
individuals.Add(Class == other.Class);
Func<object, object, bool> Compare = delegate(object A, object B)
{
if (A == DBNull.Value || B == DBNull.Value)
return A == B;
else
return (double)A == (double)B;
};
individuals.Add(Compare(Strike, other.Strike));
individuals.Add(Compare(Future, other.Future));
individuals.Add(Compare(Premium, other.Premium));
individuals.Add(Compare(Volatility, other.Volatility));
individuals.Add(Compare(Volume, other.Volume));
individuals.Add(Compare(OpenInterest, other.OpenInterest));
individuals.Add(Compare(Delta, other.Delta));
return !individuals.Contains(false);
}
Run Code Online (Sandbox Code Playgroud)
编辑: 我现在从我的代码中的其他地方调用方法:
if(!fo.Future.Equals(li))...
Run Code Online (Sandbox Code Playgroud)
但是仍然不允许我调试它.
奇怪的是,我无法找到类似的问题,但这实际上是我想要的,找到派生类的所有父类.
我测试了一个代码,希望它对我有用:
void WriteInterfaces()
{
var derivedClass = new DerivedClass();
var type = derivedClass.GetType();
var interfaces = type.FindInterfaces((objectType, criteria) =>
objectType.Name == criteria.ToString(),"BaseClass");
foreach(var face in interfaces)
{
face.Name.Dump();
}
}
interface BaseInterface
{}
class BaseClass : BaseInterface {}
class BaseClass2 : BaseClass {}
class DerivedClass : BaseClass2{}
Run Code Online (Sandbox Code Playgroud)
基本上,这里我的主要目的是检查派生类是否以某种方式在其基础层次结构中的某处继承基类.
但是,此代码返回null并仅适用于接口.
我有以下类和接口
public interface IFoo {}
public class Foo : IFoo {}
public interface IWrapper<T> where T : IFoo {}
public class Wrapper<Foo> : IWrapper<Foo> {}
Run Code Online (Sandbox Code Playgroud)
我怎样才能投Wrapper<Foo>来IWrapper<IFoo>?使用Cast(InvalidCastException)时引发异常,因为使用as时为null。
谢谢您的帮助!
更新
这是一个更具体的示例:
public interface IUser {}
public class User : IUser {}
public interface IUserRepository<T> where T : IUser {}
public class UserRepository : IUserRepository<User> {}
Run Code Online (Sandbox Code Playgroud)
现在,我需要能够执行以下操作:
UserRepository up = new UserRepository();
IUserRepository<IUser> iup = up as IUserRepository<IUser>;
Run Code Online (Sandbox Code Playgroud)
我正在使用.net 4.5。希望这可以帮助。
interface ×10
c# ×5
java ×4
generics ×2
c#-3.0 ×1
c#-4.0 ×1
c++ ×1
casting ×1
debugging ×1
iequatable ×1
implements ×1
iterator ×1
oop ×1
reflection ×1
stdvector ×1