我的问题是关于编程技术,或者可能是设计模式.假设从base派生的几个类包含一个纯虚方法,它应该进行一些交互.
例如,Rectangle,Ellipse,Triangle和Line-都来自衍生Shape.而这个抽象类Shape包含virtual bool Intersects(Shape* another) = 0.
看来,我需要做六个实现,对(顺便说一句,在这个特定的例子中是更好的解决方案吗?).
我目前不知道其他任何例子.
也许我在谈论一个众所周知的事情,我几乎肯定有一些名字描述了这项技术.但是,我甚至不知道输入什么来在互联网上找到它.
那么,你能告诉我如何实现这样的事情(我仍然想知道是否需要任何辅助方法,或者,也许,RTII是dynamic_cast吗?)或者指出一些关于它的来源(艺术,教程或其他)?
我有这门课
#import <Foundation/Foundation.h>
@interface SubscriptionArray : NSObject{
NSString *title;
NSString *source;
NSString *htmlUrl;
}
@property (nonatomic,retain) NSString *title;
@property (nonatomic,retain) NSString *source;
@property (nonatomic,retain) NSString *htmlUrl;
@end
Run Code Online (Sandbox Code Playgroud)
并且实现文件是这个:
#import "SubscriptionArray.h"
@implementation SubscriptionArray
@synthesize title,source,htmlUrl;
-(void)dealloc{
[title release];
[source release];
[htmlUrl release];
}
@end
Run Code Online (Sandbox Code Playgroud)
当我在这个例子中使用类时,我得到一个EXEC_BAD_ACCESS错误:
for (NSDictionary *element in subs){
SubscriptionArray *add;
add.title=[element objectForKey:@"title"]; //ERROR Happens at this line
add.source=[element objectForKey:@"htmlUrl"];
add.htmlUrl=[element objectForKey:@"id"];
[subscriptions addObject:add];
}
Run Code Online (Sandbox Code Playgroud)
有人能帮我吗?PS订阅是NSMutableArray
我有一个与使用C++实现图像插值(双三次和双线性方法)有关的问题.我主要担心的是速度.基于我对该问题的理解,为了使插值程序快速有效,可以采用以下策略:
使用Streaming SIMD Extensions(SSE)进行快速图像插值
使用多线程或GPU进行图像解释
快速图像插值算法
C++实现技巧
在这里,我对最后的策略更感兴趣.我设置了一个插值类:
/**
* This class is used to perform interpretaion for a certain poin in
* the image grid.
*/
class Sampling
{
public:
// samples[0] *-------------* samples[1]
// --------------
// --------------
// samples[2] *-------------*samples[3]
inline void sampling_linear(unsigned char *samples, unsigned char &res)
{
unsigned char res_temp[2];
sampling_linear_1D(samples,res_temp[0]);
sampling_linear_1D(samples+2,res_temp[1]);
sampling_linear_1D(res_temp,res);
}
private:
inline void sampling_linear_1D(unsigned char *samples, unsigned char &res)
{
}
}
Run Code Online (Sandbox Code Playgroud)
这里我只举一个双线性插值的例子.为了使程序运行得更快,采用内联函数.我的问题是这种实施方案是否有效.另外,在解释过程中,如果我给出了使用选择不同插值方法的选项.然后我有两个选择:
第一种方法意味着程序中的代码更多,而第二种方法可能导致效率低下.那么,我怎么能在这两种方案之间做出选择呢?谢谢!
首先,我很抱歉我的英语不好,然后我的问题;
我知道有很多这样的问题,在这里,但我找不到直接的答案.正在List<T>使用某种形式的"链表"机制来实现?或者它只是一个过度设计的阵列?
我对列表的性能问题很感兴趣,比如排序,插入和删除项目.例如,对于插入操作,"链表"只定义了一些新连接,但数组需要移动其值.列表怎么样?
我一直想知道为什么允许在接口中执行代码实现,当接口被假定为不包含代码实现时:
public interface someInterface{
String someString = "example";
}
Run Code Online (Sandbox Code Playgroud)
我可以使类实现此接口,而不会出现错误:
public class someClass implements someInterface
Run Code Online (Sandbox Code Playgroud)
怎么会?
我正在使用单独的编译进行家庭作业,我有一个关于访问我创建的类的数据成员的问题.当实现不带任何参数的类的成员函数并且我需要访问该类的数据成员时,我将如何在C++中执行此操作?我知道在Java中,有一个this关键字指的是调用该函数的对象.
我的头文件:
#ifndef _POLYNOMIAL_H
#define _POLYNOMIAL_H
#include <iostream>
#include <vector>
class Polynomial {
private:
std::vector<int> polynomial;
public:
// Default constructor
Polynomial();
// Parameterized constructor
Polynomial(std::vector<int> poly);
// Return the degree of of a polynomial.
int degree();
};
#endif
Run Code Online (Sandbox Code Playgroud)
我的实施文件:
#include "Polynomial.h"
Polynomial::Polynomial() {
// Some code
}
Polynomial::Polynomial(std::vector<int> poly) {
// Some code
}
int degree() {
// How would I access the data members of the object that calls this method?
// Example: polynomialOne.degree(), How would I …Run Code Online (Sandbox Code Playgroud) 问题状态是否有办法找出我的ruby实现是mri,yarv还是jruby等.
我想知道如何检查是否有一些错误是由于实现造成的.
编辑::
我想检查一下我的是Rubinus,MRI,YARV等.
我有多个类(简化用于解释目的):
public class A : BaseClass,
IHandleEvent<Event1>,
IHandleEvent<Event2>
{
}
public class B : BaseClass,
IHandleEvent<Event3>,
IHandleEvent<Event4>
{
}
public class C : BaseClass,
IHandleEvent<Event2>,
IHandleEvent<Event3>
{
}
Run Code Online (Sandbox Code Playgroud)
在我的"BaseClass"中,我有一个方法,我想检查Child-class是否实现IHandleEvent了特定事件.
public void MyMethod()
{
...
var event = ...;
...
// If this class doesn't implement an IHandleEvent of the given event, return
...
}
Run Code Online (Sandbox Code Playgroud)
从这个SO答案我知道如何检查对象是否实现了通用接口(implements IHandleEvent<>),如下所示:
if (this.GetType().GetInterfaces().Any(x =>
x.IsGenericType && x.GenericTypeDefinition() == typeof(IHandleEvent<>)))
{
... // Some log-text
return;
}
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何检查对象是否实现了SPECIFIC通用接口(implements IHandleEvent<Event1>).那么,如何在if中检查?
为什么接口不能实现这样的方法?
public interface ITargetableUnit {
//Returns whether the object of a class that implements this interface is targetable
bool unitCanBeTargeted(){
bool targetable = false;
if(this is Insect){
targetable = (this as Insect).isFasterThanLight();
}
else if(this is FighterJet){
targetable = !(this as FighterJet).Flying;
}
else if(this is Zombie){
targetable = !(this as Zombie).Invisible;
}
return targetable;
}
}
Run Code Online (Sandbox Code Playgroud)
Insect和Zombie都已经从基类Creature派生而来,而FighterJet派生自类Machine但是,并非所有的Creature都是可定位的,并且不使用ITargetableUnit inteface.
是否有任何解决方法可以解决我所面临的问题?
我有下面的代码,我使用类"B"继承类"A",而我希望从接口IMy实现F函数.但是编译器告诉我我正在隐藏接口方法"F".所以运行结果是"A".
我希望这个程序输出"B".我不希望使用隐式接口实现,因为我希望在main函数中使用正常的多态性.
如何更正我的代码?谢谢.
public interface IMy
{
void F();
}
public class A : IMy
{
public void F()
{
Console.WriteLine("A");
}
}
public class B : A
{
public void F()
{
Console.WriteLine("B");
}
}
class Program
{
static void Main(string[] args)
{
IMy my = new B();
my.F();
}
}
Run Code Online (Sandbox Code Playgroud) implementation ×10
c# ×4
interface ×4
c++ ×3
class ×2
generics ×1
hide ×1
inheritance ×1
interaction ×1
iphone ×1
java ×1
list ×1
objective-c ×1
oop ×1
opencv ×1
ruby ×1
stl ×1
types ×1