视图很容易与MC分开,
但是如何分离M和C?差异对我来说似乎有些模糊.
我正在使用PHP.
我经常需要实现一些接口,例如IEnumerable<T>在我的代码中.
每次,当自动实现时,我会遇到以下情况:
public IEnumerator<T> GetEnumerator() {
// Code here...
}
public IEnumerator GetEnumerator1() {
// Code here...
}
Run Code Online (Sandbox Code Playgroud)
虽然我必须实现两个GetEnumerator()方法,但它们不可能具有相同的名称,即使我们知道它们以某种方式执行相同操作.编译器不能将它们视为另一个的重载,因为只有返回类型不同.
这样做时,我设法将GetEnumerator1()访问者设置为private.这样,编译器就不会抱怨没有实现接口成员,而只是NotImplementedException在方法体内抛出一个.
但是,我想知道这是一个好的做法,还是我将以不同的方式进行,也许是方法别名或类似的东西.
实现接口时最好的方法是什么,例如
IEnumerable<T>需要实现两个具有相同名称的不同方法?
编辑#1
VB.NET在实现接口时的反应是否与C#不同,因为在VB.NET中它是明确实现的,因此强制执行
GetEnumerator1().这是代码:
Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
// Code here...
End Function
Public Function GetEnumerator1() As System.Collections.Generic.IEnumerator Implements System.Collections.Generic.IEnumerable.GetEnumerator
// Code here...
End Function
Run Code Online (Sandbox Code Playgroud)
两种GetEnumerator()方法都是显式实现的,编译将拒绝它们具有相同的名称.为什么?
我目前正在尝试使用http://sudzc.com/中的一些生成代码. 这段代码并不完全适合我的Web服务,因此我尝试将类别添加到一些生成的类中,并使用原始类替换它们的实现来自"objc/runtime.h"的method_exchangeImplementations.(我可以直接修改生成的代码,但我想避免它).
这是我在MyAppAppDelegate中执行的代码 - applicationDidFinishLaunching方法
Class theClass = [CBMayaIPhoneUser class];
Method originalMethod = class_getClassMethod(theClass, @selector(initWithNode:));
Method categoryMethod = class_getClassMethod(theClass, @selector(initWithAllStringNode:));
method_exchangeImplementations(originalMethod, categoryMethod);
theClass = [Soap class];
originalMethod = class_getClassMethod(theClass, @selector(getNodeValue:withName:));
categoryMethod = class_getClassMethod(theClass, @selector(getHrefNodeValue:withName:));
method_exchangeImplementations(originalMethod, categoryMethod);
theClass = [SoapRequest class];
originalMethod = class_getClassMethod(theClass, @selector(send));
categoryMethod = class_getClassMethod(theClass, @selector(sendIgnoringCertificate));
method_exchangeImplementations(originalMethod, categoryMethod);
originalMethod = class_getClassMethod(theClass, @selector(connectionDidFinishLoading:));
categoryMethod = class_getClassMethod(theClass, @selector(connectionDidFinishLoadingAndSentBody:));
method_exchangeImplementations(originalMethod, categoryMethod);
Run Code Online (Sandbox Code Playgroud)
正如我的问题所述,几乎所有这些class_getClassMethod都返回nil ...我使用了调试器,所以我知道'theClass'是正确设置的.找到的唯一方法是Soap类,它们都是类级(+)方法.但是从网上的各种例子中我得出的结论是,它也适用于其他人......
以下是MyAppAppDelegate.m的包含:
#import "MyAppAppDelegate.h"
#import "RootViewController.h"
#import "MyGlobalVariables.h"
#import "MyWebServiceExample.h"
#import "Soap+Href.h"
#import "SoapRequest+Certificate.h"
#import "CBMayaIPhoneUser+AllString.h"
#import …Run Code Online (Sandbox Code Playgroud) implementation exchange-server objective-c-runtime ios-simulator
在C/C++项目中,大多数文件可以是任何类型.h或.c/ .cpp.除了它们的标题差异,如标题和实现文件; 是否有任何功能差异?
换句话说:如果一个工作C/C++项目也能起到什么作用,如果我们改变了所有的文件.c或.cpp延期?
[注意:我们也可以#include保护.c/.cpp文件.如果将它们视为标题,我们可以跳过它们的编译.]
编辑:辩论不适用于此,因为我没有任何可靠的用例.而是我想知道,如果允许给予.h,.hxx,.i扩展只是一个设施或规则.例如,我看到的一个功能差异是.cxx文件可以拥有可爱的目标文件.
我正在通过实现一些数据结构来练习我对ADT的了解,即使大多数已经存在.使用Stacks,我读过很多书和其他文档,当你尝试添加一个元素但堆栈已满时,会讨论堆栈抛出错误.在java实现(或任何其他)中,我应该专门跟踪最大堆栈大小(来自构造函数),检查是否达到该大小,如果是,则抛出溢出异常?或者不是很重要?
采用以下简单示例:
interface IVehicle {
}
class Car : IVehicle {
}
Run Code Online (Sandbox Code Playgroud)
现在我应该能够做到以下几点:
IVehicle vehicle = new Car();
Run Code Online (Sandbox Code Playgroud)
事实上,如果我如上所述创建这些基类,我不会收到任何编译错误(代码运行.)
但是在我的项目中,无论我在做什么,基本上都是这样的(据我所知),我得到以下错误:
Cannot implicitly convert type 'Namespace.Path.Car' to 'IVehicle'. An explicit conversion exists (are you missing a cast?)
Run Code Online (Sandbox Code Playgroud)
有时(如在不同的代码位置,不是不同的编译),显式转换有效,有时它会编译,但会创建运行时异常(无效转换).为什么基本示例没有问题,而更详细的类(但仍然是直接实现接口的单个类)具有所有这些问题?
在某些情况下,它实际上会出现这个编译时错误:
Cannot implicitly convert type 'Namespace.Path.IVehicle' to 'IVehicle'. An explicit conversion exists (are you missing a cast?)
Run Code Online (Sandbox Code Playgroud)
我想我错过了这个隐式转换工作所需的一些重要条件,但是我没有发现什么区别导致简单的例子工作,更详细的类失败如此奇怪.我唯一注意到的是编译时错误只包括第一种类型的命名空间(如上面两个错误所示),但我不记得这是否正常.
为了消除任何环境原因,我使用以下代码创建了一个测试用例:
ISimpleInterface simple = new SimpleImplementation();
IComplexInterface complex = new ComplexImplementation();
Run Code Online (Sandbox Code Playgroud)
上面导致第二行的编译时错误(表明它不能进行隐式转换).
可能是一个普遍的问题,但是有一个接口(Service)及其实现(ServiceImpl)被认为是一种不好的做法,但是ServiceImpl包含未包含在接口中的私有实用程序方法?
我一直在玩一个着名的背包问题.这一次 - 然而 - 我决定在F#中实现它,因为我正在学习语言并且发现它特别有趣.
我已经成功实现了算法的主要部分,但我也需要回溯.不幸的是,我在网上找到的所有信息都使用了Haskell,我不知道(还有)).
作为占位符,我使用可变变量实现了回溯(没有详细说明,"组合(i,k)"返回i项的部分解和k的容量):
let output =
List.rev
[
let i = ref N
let k = ref K
// analize items starting with the last one
for item in Array.rev items do
match !k - item.Size with
// if size of i-th item is greater than current capacity...
| x when x < 0 -> i := !i - 1
// ... this means we haven't taken this item
yield 0
// otherwise we've got two …Run Code Online (Sandbox Code Playgroud) 我看到二进制搜索树表示为(例如):
1 5 7 10 40 50
我试图了解相同的序列化或反序列化在这里.博客文章让我疯狂的那些-1他们正在调用NULL指针的标记.他们将树代表为:
20 8 4 -1 -1 12 10 -1 -1 14 -1 -1 -1
混乱
什么是-1秒?
我的最终目标是使用C#存储和读取某种文件的二进制搜索树,但这种混乱让我失望.
假设我有一个Collection<string>并且我正在使用GetEnumerator()该返回IEnumerator<string>,我在哪里可以找到IEnumerator此集合的实现?
在MS 网站上,他们只是说:
返回循环访问Collection的枚举器.
但实现的类在哪里IEnumerator?
implementation ×10
c# ×3
interface ×3
java ×2
.net ×1
.net-2.0 ×1
algorithm ×1
backtracking ×1
c ×1
c++ ×1
f# ×1
file ×1
header-files ×1
ienumerable ×1
ienumerator ×1
nodes ×1
php ×1
stack ×1