标签: interface

Java接口:编译器错误

我正在玩java接口,试图实现堆栈.
但是,只要我添加构造函数,我就遇到了编译器错误:

./DefaultStack.java:5: error: <identifier> expected
DefaultStack<T> () {
                  ^
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

public class DefaultStack<T> implements Stack<T> {
    DefaultStack<T> () {

    }
}
Run Code Online (Sandbox Code Playgroud)

这可能是一个非常明显的错误,但我对Java有点新意.

java compiler-construction constructor interface

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

排序可比较的接口阵列

我已经为可比较数组,插入,合并和选择编写了排序方法,我通过改变我之前从排序int数组中获得的代码来完成此操作,而我只是将事物从int更改为Comparable.但是,当我为int数组做这个时,我非常清楚如何实际使用该方法,例如这是我对int的选择排序:

 public void selectionSort(int[] list){
    for (int i=0;i<list.length;i++){
        for (int si=i;si<list.length;si++){
            if (list[si]<list[i]){
                int temp=list[i];
                list[i]=list[si];
                list[si]=temp;
            }
        }
    }        
}
Run Code Online (Sandbox Code Playgroud)

这是最终使用此方法的代码:

public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    int numItems,searchNum,location;
    Sorts sort=new Sorts();        
    int[]test;        
    System.out.print("Enter the number of elements: ");
    numItems=in.nextInt();
    test=new int[numItems];

    for (int i=0;i<test.length;i++){
        test[i]=(int)(100*Math.random());
    }

    System.out.println("Unsorted: ");
    displayArray(test);
    sort.selectionSort(test);
    System.out.println("Sorted: ");        
    displayArray(test);    
Run Code Online (Sandbox Code Playgroud)

一切正常,但对于我的可比选择排序,我有这个代码:

public static void selectionSort(Comparable[] list){   
for (int i=0;i<list.length;i++){
        for (int si=i;si<list.length;si++){
            if (list[si].compareTo(list[i])<0){
                Comparable temp=list[i];
                list[i]=list[si];
                list[si]=temp;
            }
        }
       }
    } …
Run Code Online (Sandbox Code Playgroud)

java arrays sorting interface comparable

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

与params的接口

我在3个不同的类中使用相同的接口,

但是,这三个类需要有不同的参数来处理,并且所有参数都必须命名

例如我有这个界面

public interface ITest<T>{

 public T test1();
 public T test2();
 public T test3();
}
Run Code Online (Sandbox Code Playgroud)

我有3个班级

A,B,C实现具有不同T类型参数的所有ITest

但是,我需要一个类来拥有这个方法:

test1(String a, String b);
Run Code Online (Sandbox Code Playgroud)

B级有这个方法:

test1();
Run Code Online (Sandbox Code Playgroud)

C类有这个方法:

test1(boolean b);
Run Code Online (Sandbox Code Playgroud)

这可能使用相同的界面吗?或者我需要3个不同的接口用于这些类?

注意:我可以像这样编写我的界面:(假设括号中的所有类型都相同)

public interface ITest<T,S>{
 public T test1(S...params);
 public T test2();
 public T test3();

}
Run Code Online (Sandbox Code Playgroud)

然而,这意味着1:所有参数必须相同,2在不需要任何参数的方法中,仍有参数

java interface

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

强制<T>是否会与Guava的Optional <T>一起成为有用的补充?

我正在查看Guava的Optional及其理由,我想知道在表示不能为null的值时,类似的类是否快速失败会有所帮助.我找不到任何关于这个想法的讨论,所以我想我会问这里.

我的第一次尝试将尝试保持Guava使用的风格,一个Mandatory<T>暴露静态工厂of(T t).NullPointerException如果使用null参数调用,则抛出此方法.

我特别感兴趣的是在空处理方面确定接口方法的语义.我认为是否接受空参数是一个应该在接口中可以指定的设计决策,以便可以相应地设计客户端代码并避免重复前置条件检查逻辑.因此,使用此类的接口可能具有类似的方法

fire(Mandatory<Employee> employee);

并且客户可以打电话

fire(Mandatory.of(unfortunateEmployee));

我怀疑强制类型很容易找到使用方面等在调用之前挂钩进一步检查,如果这样的标记方法参数不应该为空是绝对重要的.

我也考虑过基于注释的方法,fire(@NotNull Employee employee)但是我看到的实现需要额外的验证器连线.

所以,问题......这个想法在任何地方都存在吗?如果没有,我是否错过了一些明显破坏它的东西?或者更好的想法来实现这一目标?

java null interface guava notnull

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

在Java中使用带有接口的增强型for循环?

我试图更好地理解Java接口,并在一些非常基本的代码中遇到以下问题.

以下创建两个实现相同接口的类.然后我创建两个ArrayLists来保存这两个类的对象.然后,我想创建一个增强型for循环,它遍历每个列表并执行最初在界面中定义的方法.

我认为我可以使用循环,而不是采用特定的类类型,因为它的参数可以使用接口类型,这将允许我使用任何实现该接口的类,但似乎我犯了错误.

我将如何创建一个for循环,它只允许对实现接口的类进行操作?

interface Valueing{
    double getValue();
}

class Coin implements Valueing
{
    private double coinVal = 0.0;
    Coin(double initVal){
        coinVal = initVal;
    }
    public double getValue(){
        return this.coinVal;
    }
}

class Note implements Valueing
{
    private int noteVal = 0;
    Note(int initVal){
        noteVal = initVal;
    }
    public double getValue(){
        return (double)noteVal;
    }
}

public class IFaceBasics{
    public static void main(String[] args){
        ArrayList<Coin> myChange = new ArrayList<Coin>();
        myChange.add(new Coin(0.01));
        double totalChange = sumValues(myChange);

        ArrayList<Note> myNotes = new ArrayList<Note>();
        myNotes.add(new …
Run Code Online (Sandbox Code Playgroud)

java foreach interface

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

如果接口不能有构造函数,这里会发生什么?

需要一些帮助,如果界面不能有构造函数,这里会发生什么?

interface A{
     String toString(); 
}

public class B{
    public static void main(String[] args) {
        System.out.println(new A() {
           public String toString() { 
              return "what happens here!!"; 
           }
     });
    }
}
Run Code Online (Sandbox Code Playgroud)

java interface

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

如何创建传递对象的方法实现接口?

我已经定义了这样的界面:

public interface InterfaceA {
      String getMyString();
}
Run Code Online (Sandbox Code Playgroud)

我有两个实现接口的类:

public class MyClassA implements InterfaceA {
     @Override
     public String getMyString(){
          return "A";         
     }
}

public class MyClassB implements InterfaceA {
     @Override
     public String getMyString(){
          return "B";         
     }
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是创建一个方法,接受一个实现InterfaceA为参数的对象并调用它getMyString method.这是一些伪代码,说明了我要完成的任务:

public String getStringTest(T implements InterfaceA){
     return T.getMyString;         
}
Run Code Online (Sandbox Code Playgroud)

java interface

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

Java中的接口

在C#中,通常有这样的方法:

public IPerson GetPerson()
{
  // do stuff
  return new Person(..);
}
Run Code Online (Sandbox Code Playgroud)

其中," IPerson"是用于通过一个接口Person,SpecialPerson等等.在换句话说,尽管上面的方法返回Person,一个策略模式可以被实现为使得一个SpecialPerson被代替返回的Person,条件是它们都使用IPerson的接口.

这种事情在Java中是否可行?

c# java interface

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

标记界面混乱

标记界面是指没有方法的界面.那么为什么我们将Runnable接口称为标记接口,即使它有run()方法.还有一件事在java中有多少标记接口?

java interface

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

实现的类是否可以在其父接口中声明未声明的方法?

我在Eclipse中尝试过,它显示了编译错误。但是,在联机IDE 编译器中尝试相同的操作不会显示任何错误。这就是为什么混乱。

interface Iclass{
  void print();
  void hey();
}

class sdlfkajl implements Iclass {
  public void print(){
    System.out.println("Impl class");
  }
  public void  hey(){
    System.out.println("Hey!");
  }
  public void extra(){
    System.out.println("Should I be here?");
  }
}
Run Code Online (Sandbox Code Playgroud)

显示的错误是此类不能具有未在接口中声明的方法。建议工具提示说我也应该在接口中声明此方法。

java interface

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