标签: interface

接口常量与类常量变量

我想知道最有可能使用什么,是interface Constants还是class Constants

我有一个interface类似这样的代码:

public interface IConstantsVariableSupport {
     public String URL = "www.sample-me.com";
     public Integer LIMIT = 50;
}
Run Code Online (Sandbox Code Playgroud)

interface将支持所有class需要其属性常量的内容。我认为我可以在其中class包含常量的情况下做到这一点,如下所示:

public class ConstantsVariableSupport {
     public final static String URL = "www.sample-me.com";
     public final static Integer LIMIT = 50;
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,当我用他们所拥有的支持其他类时,我会考虑什么方法constants。它是使用interface还是a class?或者,如果可能的话,您能否建议一种最佳实践来处理常量值,以便于在整个程序中使用。仅供参考,我知道如何使用这两种方法,我只是想知道如何正确执行。

java interface class constants

-1
推荐指数
1
解决办法
8397
查看次数

Golang:对接口切片进行排序

我实现了一个界面几何图形,并有一部分 [] 几何图形,

anything := []geometry{
        rect{width: 3, height: 4, name: "rect"},
        circle{radius: 5, name: "circle"},
}
Run Code Online (Sandbox Code Playgroud)

现在我想按名称对切片进行排序,更改切片中元素的顺序。我可以使用 getName 函数来获取名称。排序后,我希望它像切片一样

{circle{radius: 5, name: "circle"},rect{width: 3, height: 4, name: "rect"},}
Run Code Online (Sandbox Code Playgroud)

这是代码

package main

import (
    "fmt"
    "math"
    "sort"
)

type geometry interface {
    area() float64
    perim() float64
    getName() string
}

type rect struct {
    width, height float64
    name          string
}

type circle struct {
    radius float64
    name   string
}

func (r rect) area() float64 {
    return r.width * r.height
}
func …
Run Code Online (Sandbox Code Playgroud)

sorting interface go slice

-1
推荐指数
1
解决办法
2453
查看次数

是否可以在不使用界面的情况下使其工作?

为什么添加 type 时以下内容有效,Pet []interface{Name()}但添加 type 时无效Pet []string?是否可以在使用界面的情况下使其工作?

package main

import "fmt"

type Pet []string // cannot use Cat("Puss") (type Cat) as type string in array or slice literal
// type Pet []interface{Name()} // prt Fluffy

type Cat string

func (c Cat) Name() {
    fmt.Println(c)
}

func main() {
    p := Pet{Cat("Whiskers"), Cat("Fluffy")} 
    p1 := p[1]
    p1.Name() 
}

./oo3.go:15:14: cannot use Cat("Whiskers") (type Cat) as type string in array or slice literal
./oo3.go:15:31: …
Run Code Online (Sandbox Code Playgroud)

types interface go

-1
推荐指数
1
解决办法
109
查看次数

为什么当接口不能有构造函数时枚举有构造函数?

众所周知,接口不需要构造函数,因为接口的所有数据成员都是公共的、静态的和最终的。同样,枚举也将其所有常量设为 public static 和 final,那么它为什么需要/有一个构造函数?

java enums constructor interface

-1
推荐指数
1
解决办法
64
查看次数

从另一种接口类型转换接口

我有一个通用函数,用于将目标设置为从地图中提取的值。目前,我对该函数支持的所有类型都有一个很大的难看的 switch 语句,但想知道是否有任何方法可以根据另一个接口的底层类型转换接口。

两个接口的类型不直接相关,这增加了复杂性。目标接口是另一个接口的引用(例如,如果目标是 *int,则另一个接口应转换为 int)。不要担心不遵循此模式的示例。

以下是我正在使用的功能:

    if iface, isSet := config[name]; isSet {
        if convert != nil {
            var err error
            if iface, err = convert(iface); err != nil {
                return err
            }
        }

        var isType bool
        switch target := target.(type) {
        case *string:
            *target, isType = iface.(string)
        case *bool:
            *target, isType = iface.(bool)
        case *int:
            *target, isType = iface.(int)
        case **int:
            *target, isType = iface.(*int)
        case *[]string:
            *target, isType = iface.([]string)
        case *strslice.StrSlice:
            *target, isType = iface.([]string)
        case …
Run Code Online (Sandbox Code Playgroud)

casting type-inference interface go

-1
推荐指数
1
解决办法
54
查看次数

将指针作为接口类型传递给函数

我是 Go 的新手,下面的行为让我感到困惑:

package main

type Contractor struct{}

func (Contractor) doSomething() {}

type Puller interface {
    doSomething()
}

func process(p Puller) {
    //some code
}

func main() {
    t := Contractor{}
    process(&t) //why this line of code doesn't generate error
}
Run Code Online (Sandbox Code Playgroud)

在 Go 中一些类型和指向这个时候的指针是否符合接口?所以在我的例子中t&t都是Puller

pointers interface go

-1
推荐指数
1
解决办法
46
查看次数

C#中如何继承两个或多个具有相同方法名的接口,并且派生类应该进一步继承到另一个类中

我们如何在类中实现两个或多个具有相同方法名称的接口,并且派生类应该进一步继承到具有相同方法的新类中。

using System;  

interface A  
{  
    void Hello();  
}  

interface B  
{  
    void Hello();  
}  

class Test : A, B  
{  
    void A.Hello()  
    {  
        Console.WriteLine("Test Hello-A");  
    } 

    void B.Hello()  
    {  
        Console.WriteLine("Test Hello-B");  
    }  
}  

class Demo : Test {
   //what will be the code to override Hello method
}

public class MainClass  
{  
    public static void Main()  
    {  
        //How can we access the Hello method of Test & Demo class  
    }  
}
Run Code Online (Sandbox Code Playgroud)

c# inheritance overriding interface

-1
推荐指数
1
解决办法
64
查看次数

是否可以通过此接口中的其他方法实现一个接口中的方法?

例如,一个名为 Computable 的接口,它有两个方法:Sqrt()Abs()

如果我想添加一个名为Curve()计算 sqrt 和 abs 总和的方法。

type Computable interace { 
    Sqrt() number  // method 1
    Abs() number   // method 2
    Curve() number // method should call Sqrt() + Abs()
}
Run Code Online (Sandbox Code Playgroud)

显然,不需要为所有接口实现重写第三个方法。在 C++/Java 中,很容易解析它。但谁能告诉如何实现它?

interface go

-1
推荐指数
1
解决办法
65
查看次数

C++ 中是否有与 Java 的 Collection 接口类似的东西?

我正在尝试用 C++ 创建一个双向链表,并且我正在尝试正确执行它。因此,我认为我应该遵守任何“官方”C++ 指南推荐的任何接口结构,例如接口。在Java中,有这样的接口,可以免费使用,并且标准化抽象数据类型(例如https://docs.oracle.com/javase/8/docs/api/java/util/List.html) 。

我的问题:C++中有类似的东西吗?Startpageing 找到了我这个链接:

https://en.cppreference.com/w/cpp/container/list

但它似乎没有使用任何继承。

c++ java inheritance interface

-1
推荐指数
1
解决办法
192
查看次数

我认为我误解了 Go 多态性/接口/结构

我不明白为什么下面的代码不能编译。

我很困惑为什么 Go 说HistoryReader没有正确实现IReaderHistoryBook实现IBook. 当尝试将 a 添加到 s 的切片时,为什么Read(book Ibook)和不能同时接受?Read(book HistoryBook)HistoryReaderIReader

package main

type IReader interface {
   Read(book IBook)
}

// HistoryReader implements IReader
type HistoryReader struct{}

func (r *HistoryReader) Read(book HistoryBook) {
   // ...
}

type IBook interface{}

// HistoryBook implements IBook
type HistoryBook struct{}

func main() {
   var readerSlice []IReader

   _ = append(readerSlice, &HistoryReader{})
}
Run Code Online (Sandbox Code Playgroud)
package main

type IReader interface {
   Read(book IBook)
}

// …
Run Code Online (Sandbox Code Playgroud)

polymorphism struct interface go

-1
推荐指数
1
解决办法
47
查看次数