标签: implements

实现Comparable以使用字符串进行字母排序

我希望一个对象可以比较(在这种情况下在TreeSet中使用它).

我的对象有一个名称字段,我希望按字母顺序排序.

我首先想到我可以使用字符串的unicode值并简单地做一个减法,但是然后AA会在Ab之后...例如...

这是我开始的方式:

public final class MyObject implements Comparable<MyObject> {

 private String name;

 public MyObject(String name) {
  this.name = name;
 }

 public String name() {
  return name;
 }

 @Override
 public int compareTo(MyObject otherObject) {
  return WHAT DO I PUT HERE ?;
 }
}
Run Code Online (Sandbox Code Playgroud)

感谢那些愿意帮助的人,祝你有个美好的一天!

java interface comparable implements alphabetical-sort

6
推荐指数
1
解决办法
2万
查看次数

Why can't I implement multiple interfaces?

I am creating a game where objects implement interfaces for animations. I have a parent interface for the animations. Here is a shortened version:

public interface Animates<S extends Animator> {
    S createAnimator(long animationTime);
}
Run Code Online (Sandbox Code Playgroud)

In addition, I have multiple interfaces that extend this interface. Two examples:

public interface AnimatesPaint extends Animates<PaintAnimator> {
    PaintAnimator createPaintAnimator(long animationTime);

    default PaintAnimator createAnimator(long animationTime) {
        return createPaintAnimator(animationTime);
    }

}
Run Code Online (Sandbox Code Playgroud)

and

public interface AnimatesPosition extends Animates<PositionAnimator> {
    PositionAnimator createPositionAnimator(long animationTime);

    @Override
    default PositionAnimator createAnimator(long animationTime) { …
Run Code Online (Sandbox Code Playgroud)

java interface implements

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

VB6中的接口限制

我正在尝试在VB 6中实现(即实现接口)一个类,但我收到此错误:"编译错误:实现的接口错误:接口包含数据字段".所以我想知道你可以实现哪个类的限制?或者,如果有人知道我为什么会收到这个特定的错误,那也会有所帮助.

如果我需要更清楚,请告诉我.多谢你们.

戴夫

vb6 interface implements

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

覆盖PHP中的方法?

在其他面向对象的语言如Java,我们可以覆盖功能,可以使用关键字/注解喜欢implements@override等等。

有没有办法在PHP中这样做?我的意思是,例如:

class myClass {
    public static function reImplmentThis() { //this method should be overriden by user
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望用户实现自己的myClass::reImplementThis()方法。

我怎样才能在 PHP 中做到这一点?如果可能,我可以将其设为可选吗?

我的意思是,如果用户没有实现该方法,我可以指定一个默认方法还是我可以确定该方法未定义(我可以使用 来做到这一点method_exists)?

php oop overriding implements

5
推荐指数
0
解决办法
5060
查看次数

为什么接口有用?(面向对象编程)

我已经了解了实现和接口的基础知识。我不明白什么时候使用接口。拥有一个接口需要什么条件?

例子:

/// Interface demo
Interface IDemo
{
    // Function prototype
    public void Show();
}

// First class using the interface
Class MyClass1 : IDemo
{
public void Show()
{
    // Function body comes here
    Response.Write("I'm in MyClass");
}
}

// Second class using the interface
Class MyClass2 : IDemo
{
public void Show()
{
    // Function body comes here
    Response.Write("I'm in MyClass2");
    Response.Write("So, what?");
}
}
Run Code Online (Sandbox Code Playgroud)

这两个类具有相同的函数名称和不同的主体。这也可以在没有接口的情况下实现。提供方法参考的目的是什么?当我扩展超类时,至少我获得了超类的属性和方法。

请给我一个清晰的解释和一个现实世界的场景,以便我更好地理解。

oop interface implements

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

必须扩展A类并实现某些接口的成员变量

我需要在类中有一个变量,它是类"ClassA"的实例,但它也实现了接口"InterfaceI".

显然,这可以很容易地为一个或另一个完成:

private ClassA mVariable;
private InterfaceI mVaraible;
Run Code Online (Sandbox Code Playgroud)

但是如何强制对象既扩展了ClassA又实现了InterfaceB?就像是:

private <? extends ClassA & InterfaceI> mVaraible;
Run Code Online (Sandbox Code Playgroud)

是我需要的,但我不知道语法或甚至可能.我还需要一个get和set方法,但这可以用泛型来完成(我想?)

明显的解决方案是

public class ClassB extends ClassA implements InterfaceI{
    //whatever here
}
Run Code Online (Sandbox Code Playgroud)

但是,InterfaceI和ClassA都是外部库的一部分,这个库有扩展ClassA和InterfaceI的类,我不能编辑它们以使它们扩展ClassB,因此这个解决方案不起作用.

java extends implements

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

java动作监听器:实现vs匿名类

我正在尝试自学Java并且有一个我到目前为止无法回答的问题.在我的一些在线阅读中,我发现了两种使用动作监听器的方法,它们似乎做同样的事情.但我试图找出一个优于另一个的优势/劣势.

使用这样的匿名类是否更好:

public MyClass() {
...
    myButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            //doSomething
        }
    });
...
}
Run Code Online (Sandbox Code Playgroud)

或者最好是在类的开头实现,如下所示:

public MyClass() implements ActionListener {
...
    myButton.addActionListener(this);

    public void actionPerformed(ActionEvent e) {
        //doSomething
    }
...
}
Run Code Online (Sandbox Code Playgroud)

java event-handling anonymous-class actionlistener implements

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

循环继承和接口 - 当B类实现A接口时,A类不能实现B类接口

我有:

public class A implements BListener {
    public interface AListener {}
}

public class B implements AListener {
    public interface BListener {}
}
Run Code Online (Sandbox Code Playgroud)

所以,如果我理解正确,循环继承发生的原因是:

编译器转到A并说"嘿,A实现BListener,让我们去找BListener!"

然后当它试图找到BListener时,它最终会到达B,它说:

"嘿,BListener,A需要在B里面!但是等待!B需要AListener!让我们去找AListener吧!"

然后它到达A,重复.我做对了吗?

顺便说一句,这个编译错误发生在Android开发上.

java inheritance android interface implements

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

TypeScript类实现具有私有函数的类

我正在探索在TypeScript中实现类的可能性.

因此,我编写了以下代码Playground链接:

class A {
    private f() { console.log("f"); }
    public g() { console.log("G"); }
}

class B implements A {
    public g() { console.log("g"); }
}
Run Code Online (Sandbox Code Playgroud)

我得到了错误:Class 'B' incorrectly implements class 'A' --- property 'f' is missing in type 'B'加上我实际意味着的建议extends.

所以我试着创建一个私有字段f(public没有工作,因为它检测到它们有不同的访问修饰符)Playground链接

现在我得到了错误:Class 'B' incorrectly implements class 'A'. Types have separate declarations of a private property 'f'; 这让我很困惑:

  • 为什么私有成员甚至很重要 - 如果我使用不同的数据结构实现相同的算法,我是否必须为了类型检查而声明一些名称相同的东西?
  • 为什么在实现f私有函数时会出现错误?

我不会在实践中这样做,但我很好奇TS为何如此工作.

谢谢!

implements typescript

5
推荐指数
2
解决办法
2212
查看次数

为什么 Typescript 允许实现抽象类?

我会阅读有关打字稿的内容classabstract class并且确实发现了一些非常有趣的东西。

Typescript allows us to implement classes.

我什至创建了一个stackblitz项目来测试它。

无论如何,我已经在这里找到了一般差异,以及它在此处的打字稿中的行为是什么。

我知道它可以用来为测试创建模拟。

为什么打字稿允许的话,如果在其他非常流行的语言(JavaC#,...),这是“禁止”?此功能是否还有其他重要应用?

extends interface class implements typescript

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