我正在尝试为工程项目实现一些形状并将其抽象出来以用于一些常见功能,以便我可以使用通用程序.
我正在尝试做的是有一个调用的接口,cShape并拥有cRectangle并cCircle实现cShape
我的代码如下:
cShape 接口
Option Explicit
Public Function getArea()
End Function
Public Function getInertiaX()
End Function
Public Function getInertiaY()
End Function
Public Function toString()
End Function
Run Code Online (Sandbox Code Playgroud)
cRectangle 类
Option Explicit
Implements cShape
Public myLength As Double ''going to treat length as d
Public myWidth As Double ''going to treat width as b
Public Function getArea()
getArea = myLength * myWidth
End Function
Public Function getInertiaX()
getInertiaX = (myWidth) * (myLength ^ 3) …Run Code Online (Sandbox Code Playgroud) 当我开始学习Haskell时,我被告知类型类与接口不同且功能更强大.
一年后,我广泛使用了接口和类型,我还没有看到它们如何不同的示例或解释.这不是一种自然而然的启示,或者我错过了一些明显的东西,或者实际上没有真正的区别.
搜索互联网并没有发现任何实质性内容.那么,你有答案吗?
在我的项目中,我发现了一个在C#中看起来完全有效的奇怪情况,因为我没有编译错误.
简化示例如下所示:
using System;
using System.Collections.Generic;
namespace Test
{
interface IFoo
{
void FooMethod();
}
class A
{
public void FooMethod()
{
Console.WriteLine("implementation");
}
}
class B : A, IFoo
{
}
class Program
{
static void Main(string[] args)
{
IFoo foo = new B();
foo.FooMethod();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这样的代码编译.但是,请注意,A是不是IFoo和B未实现IFoo的方法.在我的情况下,偶然(重构后),A具有相同签名的方法.但是,为什么要A知道如何实现FooMethod的的IFoo接口?A甚至不知道IFoo存在.
对我来说,这样的设计是危险的.因为每次我实现一些接口时,我应检查此接口中的每个方法是否"干扰"基类方法.
如果这是"纯C#功能"?这叫什么?我错过了什么吗?
我正试图在我的代码中使用快速(使用正文解析器中间件)的请求正文进行接口,但它不起作用.有可能这样做吗?
这是我的界面:
export interface IToDoDto {
description: string;
status: boolean;
};
Run Code Online (Sandbox Code Playgroud)
这是我正在尝试进行演员的代码:
@Post()
addToDo(@Response() res, @Request() req) {
const toDo: IToDoDto = <IToDoDto> req.body;
this.toDoService.addToDo(toDo);
return res.status(HttpStatus.CREATED).end();
}
Run Code Online (Sandbox Code Playgroud)
最后,被调用的服务方法:
public addToDo(toDo: IToDoDto): void {
toDo.id = this.idCounter;
this.todos.push(toDo);
this.idCounter++;
}
Run Code Online (Sandbox Code Playgroud)
我可以通过任何论点,这将工作正常.我已经读过在TypeScript中不存在强制转换而是类型断言,所以它只会告诉编译器一个对象的类型为x,所以...我错了吗?这样做的方法是什么?谢谢.
我有这样的通用接口:
interface A<T> {
T getValue();
}
Run Code Online (Sandbox Code Playgroud)
此接口具有有限的实例,因此最好将它们实现为枚举值.问题是那些实例有不同类型的值,所以我尝试了以下方法,但它不编译:
public enum B implements A {
A1<String> {
@Override
public String getValue() {
return "value";
}
},
A2<Integer> {
@Override
public Integer getValue() {
return 0;
}
};
}
Run Code Online (Sandbox Code Playgroud)
对此有何想法?
我已经搜索了SO以及网络的其他部分以获得一个很好的答案,但我找不到一个我真正理解的内容.我将以不同的方式呈现这一点,希望答案也能帮助其他人.
据我所知,这两个概念具有相同的规则,但由于方法实现能力,抽象类更灵活.此外,我知道你可以实现多个接口,只扩展一个类,但我确信有比我提到的两个更多的差异.
请查看代码的两个片段,并举例说明我可以对我的每个示例做些什么,这些示例会让我想要或不想使用另一个.
abstract class Foo {
abstract public function getValue();
abstract public function setValue($value);
}
class myObj extends Foo {
function getValue() {
}
function setValue($value) {
}
}
Run Code Online (Sandbox Code Playgroud)
interface Foo {
public function getValue();
public function setValue($value);
}
class myObj implements Foo {
function getValue() {
}
function setValue($value) {
}
}
Run Code Online (Sandbox Code Playgroud) 我已经多次看到从类生成的Interface实例.为什么以这种方式使用接口?Interface实例仅在派生类的帮助下创建,我们只能通过此实例访问此接口成员.这有什么优势?我很困惑..
interface IPrint
{
void Print();
}
class Sample : IPrint
{
public void Print()
{
Console.WriteLine("Print...");
}
public void Sample()
{
Console.WriteLine("Sample...");
}
}
class Program
{
static void Main(string[] args)
{
IPrint print = new Sample();
print.Print();
}
}
Run Code Online (Sandbox Code Playgroud) 我在这个上画了一个空白,似乎找不到我写的任何先前的例子.我正在尝试用类实现通用接口.当我实现接口时,我认为某些东西不能正常工作,因为Visual Studio不断产生错误,说我并没有暗示通用接口中的所有方法.
这是我正在使用的存根:
public interface IOurTemplate<T, U>
{
IEnumerable<T> List<T>() where T : class;
T Get<T, U>(U id)
where T : class
where U : class;
}
Run Code Online (Sandbox Code Playgroud)
那我班上应该怎么样?
为什么没有Typescript警告我,我定义的函数与接口声明不匹配,但如果我尝试调用该函数,它会警告我.
interface IFormatter {
(data: string, toUpper : boolean): string;
};
//Compiler does not flag error here.
var upperCaseFormatter: IFormatter = function (data: string) {
return data.toUpperCase();
}
upperCaseFormatter("test"); //but does flag an error here.
Run Code Online (Sandbox Code Playgroud)