假设我在C++,并且我有这个伪接口(abstract class仅包含pure virtual methods):
class IErrorLog
{
public:
virtual bool closeLog() = 0;
};
class FileErrorLog : public IErrorLog
{
public:
FileErrorLog(){}
~FileErrorLog(){}
bool closeLog() {
std::cout << "Close FileErrorLog" << std::endl;
return true;
}
};
int main()
{
FileErrorLog fileErrorLog;
IErrorLog *log = &fileErrorLog;
log->closeLog();
}
Run Code Online (Sandbox Code Playgroud)
据我所知,closeLog使用 的动态类型IErrorLog称为对 的动态vtable类型进行去虚拟化FileErrorLog并选择所需的目标函数(closeLog()指针)。
由于 inC#并不是interfaces真正被认为是类,所以当我做这样的事情时:
interface IErrorLog {
void closeLog (); …Run Code Online (Sandbox Code Playgroud) 我是 Typescript 新手,我想为以下 JSON 定义接口:
{
"company":"abc inc",
"logoUrl":"someUrl",
"phone":"1234567890",
"branch":{
"nyc":{
"products":{
"asian":{
"somekey1":"someValue1",
"somekey2":"someValue2",
"somekeyN":"somevalueN"
},
"american":{
"somekey1":"someValue1",
"somekey2":"someValue2",
"somekeyN":"somevalueN"
}
}
},
"boston":{
"products":{
"asian":{
"somekey1":"somevalue1",
"somekey2":"somevalue2",
"somekeyN":"somevalueN"
},
"american":{
"somekey1":"somevalue1",
"somekey2":"somevalue2",
"somekeyN":"somevalueN"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我当前定义接口、对象的方式asian,并且american可以包含 n 个键值。我对定义它的语法感到困惑。有人可以指导我该怎么做吗?谢谢阅读。
interface Products {
asian: {};
american: {};
}
interface Configuration {
company: string;
phone: string;
logoUrl: string
branch: {
nyc: {
products: Products;
};
boston: {
products: Products;
};
};
}
Run Code Online (Sandbox Code Playgroud) abstract class CAR
fuelUp () { // implemented }
/ \
interface SPORTER interface TRUCK
driveFast (); moveLoad ();
Run Code Online (Sandbox Code Playgroud)
在Java中有没有一种方法可以让我得到一个类ESTATE
从多个类扩展是不可能的,并且使 CAR 成为一个接口并不能为我提供 CAR 的实现。
我有一个方法,我想重新创建更通用的方法。
public Task<bool> DoSomething<T>(T t) where T : Type, IAnyInterface
Run Code Online (Sandbox Code Playgroud)
如您所见,我想要一个Type作为参数,它必须实现IAnyInterface
但如果我调用该方法,
DoSomething(typeof(ObjectThatImplementsIAnyInterface));
Run Code Online (Sandbox Code Playgroud)
我收到错误:
类型“System.Type”不能用作泛型类型或方法“DoSomething(...)”中的类型参数“T” 没有从“System.Type”到“IAnyInterface”的隐式转换
那么我怎样才能让该方法接受该类型呢?
我想使用接口来模拟函数,并且我能够模拟第一个函数callsomething\n在 的大力帮助下icza,现在\xe2\x80\x99s 有点棘手。我想测试函数vl1\n使用模拟函数 function1,它是如何完成的。
https://play.golang.org/p/w367IOjADFV
\n\n包主
\n\nimport (\n "fmt"\n "time"\n "testing"\n)\n\ntype vInterface interface {\n function1() bool\n}\n\ntype mStruct struct {\n info string\n time time.Time\n}\n\nfunc (s *mStruct) function1() bool {\n return true\n}\n\nfunc callSomething(si vInterface) bool {\n return si.function1()\n}\n\nfunc (s *mStruct) vl1() bool {\n return callSomething(s)\n}\n\nvar currentVt1 mStruct\n\nfunc main() {\n vl1 := currentVt1.vl1()\n\n fmt.Println(vl1)\n}\n\n//\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94TESTS\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\n\n// This test is working as expected (as suggested by icza) for the function "callSomething"\n// here we use mock …Run Code Online (Sandbox Code Playgroud) 我正在尝试从 Go Lang 中的深层嵌套 json 数据中解析并获取选定的数据。我在浏览结构和访问数据时遇到问题。数据太深太复杂,无法用 Go 中的先验已知结构进行解析。这是文件的 URL: - https://www.data.gouv.fr/api/1/datasets/?format=csv&page=0&page_size=20
我使用地图接口并使用 json 字符串进行了一些解析:
resultdata := map[string]interface {}
json.Unmarshal([]byte(inputbytestring), &resultdata) //Inputstring is the string containing the JSON data of the above URL
Run Code Online (Sandbox Code Playgroud)
问题:
在 Kotlin 中,可以在接口中声明扩展函数,如下所示:
interface Evaluator {
fun Double.evaluateY1(): Double
fun Double.evaluateY2(): Double
}
class EvaluatorImpl : Evaluator {
override fun Double.evaluateY1(): Double {
return this + 2.0
}
override fun Double.evaluateY2(): Double {
return this + 3.0
}
}
Run Code Online (Sandbox Code Playgroud)
有了接收器和接口实例,我如何调用这样的扩展函数?
我想出了一个涉及with范围函数的技巧,但我更喜欢缩进较少的东西。
fun usageExample(evaluator: Evaluator, x: Double) {
with(evaluator) {
println("Y1 = ${x.evaluateY1()}. Y2 = ${x.evaluateY2()}")
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个打字稿函数,它接受选项对象作为参数。我有一个定义选项的界面。我还有一个常量,其中包含每个选项的默认值。它看起来像这样:
interface Options {
viewportHeight: number
viewportWidth: number
// ...
}
const defaults: Options = {
viewportHeight: 600,
viewportWidth: 600,
// ...
}
function createGenerator(options: Options) {
options = { ...defaults, ...options }
return function generate() {
// Simplfied
return {
value: Math.min(options.viewportWidth, options.viewportHeight),
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题如下:如果我保留Options接口所需的所有属性,那么我无法执行类似createGenerator({ viewportWidth: 50 })仅设置我需要的选项并默认保留其余选项的操作
如果我确实将它们设为可选,那么当我尝试使用它们时,所有选项options.viewportWidth的类型都会number | undefined导致错误。Argument of type 'number | undefined' is not assignable to parameter of type 'number'
我可以定义两个接口,例如Options和FilledOptions …
我有界面:
public Interface Inter1
{
....
Task<(ITestBulk testBulk, bool resultBulk)> GetBulk(int par1, int par2, string par3);
}
Run Code Online (Sandbox Code Playgroud)
然后得到了一些方法:
class MyClass{
internal bool m_resultBulk;
internal async void NewBulk()
{
....
(ITestBulk, m_resultBulk) = await service.GetBulk(1, 2, "ggg");
}
}
Run Code Online (Sandbox Code Playgroud)
但m_resultBulk找不到。我做错了什么?
我有以下代码来测试 golang 接口值何时为 false。
package main
import (
"fmt"
)
func Foo(x interface{}) {
fmt.Println("33, x == nil = ", x == nil)
}
func main() {
var x *int = nil
fmt.Println("11, x == nil = ", x == nil)
var a interface{}
fmt.Println(22, &a, &a == nil, a == nil) // 0xc0000935c0 false true
Foo(x)
}
Run Code Online (Sandbox Code Playgroud)
结果是
11, x == nil = true
22 0xc00010a040 false true
33, x == nil = false
Run Code Online (Sandbox Code Playgroud)
我不明白输出。特别是,我不明白为什么&a不是 nil,因为 a …
interface ×10
c# ×3
go ×3
json ×2
typescript ×2
async-await ×1
c++ ×1
dictionary ×1
generics ×1
inheritance ×1
java ×1
kotlin ×1
oop ×1
polymorphism ×1
task ×1
tuples ×1
vtable ×1