标签: interface

使用 Java 接口的简单报警系统

好的,我的任务是创建基本的警报实现接口。

这是我到目前为止所得到的。

这是我的主要方法。

import java.util.Scanner;
import java.time.LocalTime;
 
public class Monday extends Weekday{
 
    private String time;
     
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        Monday m = new Monday();
         
        String alarmer;
        System.out.print("Enter time for alarm in this format (HH:MM): " );
        alarmer = reader.nextLine();
         
        m.setAlarm(alarmer);
        m.showAlarm();
    }
 
    @Override
    public void setAlarm(String time) {
        this.time = time;
        LocalTime alarm = LocalTime.parse(time);
        LocalTime now = LocalTime.now();
         
        if (alarm.isBefore(now)) {
            System.out.println("I'll wake you up later!");
        }
         
        else {
            System.out.println("Alarm is set …
Run Code Online (Sandbox Code Playgroud)

java interface localtime

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

Java Predicate Interface - 该方法如何工作 - 尽管它没有在我的类中实现

虽然我还没有实现 Predicate 接口中的方法 - test - ,但为什么在我的类 - inte 中没有任何主体的情况下执行该方法?

package newone; 
import java.util.function.Predicate;

public class inte {
    public static void main(String[] args) {
        Predicate <Integer> p = i -> (i<19);
        System.out.println(  p.test(22));
       }


@FunctionalInterface
public interface Predicate<T> {
    boolean test(T var1);
}
Run Code Online (Sandbox Code Playgroud)

java interface predicate

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

如何模拟 net.Interface

我正在尝试在 Go 中模拟 net.Interface,我使用 net.Interfaces() 并且我想要一个固定的回报。但 net.Interface 不是一个接口,所以我不能用 gomock 来模拟它。

\n

也许我的测试方式是错误的。

\n

这是我要测试的方法:

\n
const InterfaceWlan = "wlan0"\nconst InterfaceEthernet = "eth0"\n\nvar netInterfaces = net.Interfaces\n\nfunc GetIpAddress() (net.IP, error) {\n    // On r\xc3\xa9cup\xc3\xa8re la liste des interfaces\n    ifaces, err := netInterfaces()\n    if err != nil {\n        return nil, err\n    }\n\n    // On parcours la liste des interfaces\n    for _, i := range ifaces {\n        // Seul l\'interface Wlan0 ou Eth0 nous int\xc3\xa9resse\n        if i.Name == InterfaceWlan || i.Name == InterfaceEthernet {\n …
Run Code Online (Sandbox Code Playgroud)

interface mocking go gomock

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

在构造函数中使用多个接口作为参数的依赖注入

我是 Core/Azure Functions 的新手,并尝试使用 DI 容器添加依赖项。

我想将类/接口依赖项添加到 DI 容器:

s.AddSingleton<ITicketService>(new TicketService());
s.AddSingleton<IFoodService>(new FoodService());
s.AddSingleton<IFinancialsService>(new FinancialsService());
Run Code Online (Sandbox Code Playgroud)

我该如何处理上述三个类/接口,它们依赖于存储库类。

s.AddSingleton<ITicketService>(new TicketService(ITicketRepository));
s.AddSingleton<IFoodService>(new FoodService(IFoodRepository));
s.AddSingleton<IFinancialsService>(
    new FinancialsService(ITicketRepository,IFoodRepository));
Run Code Online (Sandbox Code Playgroud)

以下是这些类和接口的相关代码:

public interface ITicketService { ... }
public interface IFoodService { ... }
public interface IFinancialsService { ... }

public class TicketService : ITicketService
{
    private readonly ITicketRepository _ticketRepo;

    public TicketService(ITicketRepository ticketRepo) => _ticketRepo = ticketRepo;
}

public class FoodService : IFoodService 
{
    private readonly IFoodRepository _foodRepo;

    public FoodService(IFoodRepository foodRepo) => _foodRepo = foodRepo;
}

public class …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection interface asp.net-core azure-functions

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

这个 C# 接口语法叫什么以及它的含义是什么

我正在开发一组相当复杂的接口,它允许定义具有特定结构的对象。通过接口和泛型,它还允许我定义每个元素中可用的接口组合。该结构映射了我们背景中的某些内容,因此目标是在代码中重新创建结构,以便更好地理解代码实际操作的内容。

结果如下:

static void Main(string[] args)
{

  IComplexDefinition1 CD1 = null;
  
  CD1.Access.Level1.ElementB.SomeStructrueMethod();
  CD1.Access.Level1.ElementC.ActorAMethod1();
  CD1.Access.Level1.ElementC.ActorBMethod2();
  CD1.Access.Level1.ElementC.ActorCMethod1();

  CD1.Access.Level2.ElementA.SomeOperationPoolMethod();
  CD1.Access.Level2.ElementC.ActorAMethod2();
  CD1.Access.Level2.ElementC.ActorBMethod1();
  CD1.Access.Level2.ElementC.ActorCMethod2();

}
Run Code Online (Sandbox Code Playgroud)

现在,在实现具体类时,我在 Visual Studio 中单击了“自动实现”,它出现了一行我到目前为止还没有看到的代码(并且没想到会发生这种情况)。因此,我正在寻找它的工作原理以及我可以在哪里阅读/研究更多相关信息的见解。

具体实现的相关部分是:

public class ComplexDefinition : IComplexDefinition1,....
{

  protected IActorComposition _actors;
  protected SomeStructure _structure;
  protected SomeOperationPool _operationPool;

  public IElementsBC<IActorCompositionLevel2, SomeStructure> Level1 => this;
  public IElementsAC<IActorCompositionLevel1, SomeOperationPool> Level2 => this;
  
  public SomeStructure ElementB => _structure;
  public SomeOperationPool ElementA => _operationPool;

  public IActorCompositionLevel2 ElementC => _actors;
  IActorCompositionLevel1 IElementC<IActorCompositionLevel1>.ElementC => _actors;

}
Run Code Online (Sandbox Code Playgroud)

尤其是这两行让我感到困扰(尤其是最后一行):

public IActorCompositionLevel2 ElementC => _actors;
IActorCompositionLevel1 IElementC<IActorCompositionLevel1>.ElementC …
Run Code Online (Sandbox Code Playgroud)

c# interface explicit-interface

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

您可以更改 Java 异常的错误消息类型吗?

我正在编写一个运行时间非常慢的面向内部的工具。由于此运行时,我尝试验证输入是否有错误,并在实际运行该工具之前将包含每个丢失或失败元素的详细列表的 JSON 对象发送给客户端。

我想知道是否有一种方法可以修改 java Exception 的错误消息返回类型,以便我可以在错误中抛出此 HashMap。

包含错误的哈希图看起来像这样:

Error: {
   Message: "Your X is configured improperly, please fill out the required fields before running this tool", 
   formXErrors: {
     Message: "The following fields are missing from formX", 
     Error: {
       field1Name: "field 1", 
       field2Name: "field 2",
       ... 
    },
    formYErrors: {
     Message: "The following fields are missing from formY", 
     Error: {
       field1Name: "field 1", 
       field2Name: "field 2",
       ... 
     }
  } 
}
Run Code Online (Sandbox Code Playgroud)

然后,如果发现错误,我想像这样抛出它(将 HashMap 传递到消息字段而不是字符串):

if (errorHashMap != null) {
  throw new ToolXException(errorHashMap); …
Run Code Online (Sandbox Code Playgroud)

java interface exception

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

我应该如何显示接口和类之间的关系?

我有 Java Spring Web 应用程序和 UML。我有类PersonalClient(@Entity),并且有使用该实体 PersonalClientRepository(CRUD)与数据库交互的接口存储库。现在,在我的 UML 上它看起来像这样(绿色箭头):

在此输入图像描述

但我认为,这是错误的。有没有正确的方法来显示这种关系?

java uml interface class repository

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

如何实现 F# 接口并让成员返回该接口的实例?

假设我在 F# 中有以下接口:

type InterfaceA =
 abstract Magic : InterfaceA -> InterfaceA
Run Code Online (Sandbox Code Playgroud)

我怎样才能实现这样的接口?当我尝试这样做时:

type MyTypeA = {x:int} with
 interface InterfaceA with
  member self.Magic another = 
   {x=self.x+another.x}
Run Code Online (Sandbox Code Playgroud)

我收到错误: This expression was expected to have type 'InterfaceA' but here has type 'MyTypeA'

f# interface interface-implementation

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

Java:调用与接口字段冲突的抽象类的非静态字段

如何显示抽象类 Confused("ConfusedValue") 中“name”非静态字段的值?我尝试了3种方法,但都有错误。我只能显示 Confuslable 的名称值(“ConfusableValue”)

interface Confusable {
    String name = "ConfusableValue";

    String confuse();
}

abstract class Confused {
    String name = "ConfusedValue";

    abstract Object confuse();
}

public class Test extends Confused implements Confusable {
    public static void main(String[] args) {
        Test a = new Test();
        // --- OK
        System.out.println("Confusable.name: " + Confusable.name);
        // --- Errors
        System.out.println("name: " + name); // Error : The field name is ambiguous
        System.out.println("Confused.name: " + Confused.name); // Error : Cannot make a static reference …
Run Code Online (Sandbox Code Playgroud)

java inheritance interface abstract non-static

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

检查类型是否直接实现接口。并非来自继承

从类型 A、B、C、ANotState、BNotState 列表中,我想获取标有接口的 A、B、C。但是,如果确实从 ANotState 或 BNotState 获取接口,我只能得到从 C 继承的 isState(在 BNotState 情况下从 ANotState 继承)。

有没有办法知道接口是否是直接实现的?

public class Program
{
    public static void Main()
    {
        List<Type> types = new List<Type>{typeof(A), typeof(B), typeof(C), typeof(ANotState), typeof(BNotState)};
        
        //Now remove from list types that are not implementing isState interface directly which means ANotState and BNotState
    }
}

public interface isState{}


public class A : isState
{
    
}

public abstract class B : A, isState
{

}

public abstract class C : B, isState
{ …
Run Code Online (Sandbox Code Playgroud)

c# inheritance interface

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