简体   繁体   English

java实现接口时遇到的问题

[英]Facing problem while implementing interface in java

I have studied that我研究过

"A class that implements an interface must implement all the methods declared in the interface" “实现接口的类必须实现接口中声明的所有方法”

I am doing the study of CharSequence from this link here CharSequence having 4 methods, according to the definition of the interface a class must implement all methods of the interface.我正在从这个链接这里研究 CharSequence 有 4 个方法的 CharSequence,根据接口的定义,一个类必须实现接口的所有方法。

I have created one class and implemented the CharSequence interface我创建了一个类并实现了 CharSequence 接口

but here I am not overriding the "toString()" method and working fine.但在这里我没有覆盖“toString()”方法并且工作正常。

I want to know that my code is not giving any error when I am not overriding the "toString()" but it is giving an error if I am not implementing the others method.我想知道当我没有覆盖“toString()”时,我的代码没有给出任何错误,但是如果我没有实现其他方法,它就会给出错误。

below code is working for me but I think it should give an error.下面的代码对我有用,但我认为它应该给出一个错误。

    import java.util.*;
import java.lang.*;
public class Charsequence {

    public static void main(String args[]){
        System.out.println("hello...");
    }

}
class Subsequence implements CharSequence{

public char charAt(int index){
    return '1';
}

public int length(){
    return 1;
}

public CharSequence subSequence(int start, int end){
    return "" ;
}

/*public String toString(){
    return "";
}*/


}

sorry for the bad English.抱歉英语不好。

Thank you:)谢谢:)

All classes in Java extend Object class. Java 中的所有类都扩展了Object类。
This is because the Object class implements toString() .这是因为 Object 类实现了toString()

All classes have a toString() method because they inherit Object#toString , so it is actually "implemented".所有类都有一个toString()方法,因为它们继承了Object#toString ,所以它实际上是“实现”的。 You should however override it and implement it properly as the CharSequence documentation specifies.但是,您应该覆盖它并按照 CharSequence 文档的指定正确实现它。

toString is implemented in java.lang.Object . toStringjava.lang.Object实现。 To be non-abstract, a method just needs an implementation.为了不抽象,一个方法只需要一个实现。 This even if the the method is implemented in a super class of the class that implements the interface.即使该方法是在实现接口的类的超类中实现的。

In some case you may get a synthetic method in the subclass if there are covariant return types.在某些情况下,如果存在协变返回类型,您可能会在子类中获得合成方法。

In java, every class by default extends Object class in java.在java中,默认情况下每个类都扩展了java中的Object类。 The Object class already has a default implementation for the toString() method. Object类已经有toString()方法的默认实现。 So every class created in java has default parent class ie Object class.所以在java中创建的每个类都有默认的父类,即Object类。 In your case, lets say your class name is Test, Test is using the default implementation of toString() method provided in Object class.在您的情况下,假设您的类名是 Test,Test 使用 Object 类中提供的 toString() 方法的默认实现。

During compilation, java generates .class file for each class.在编译过程中,java 为每个类生成 .class 文件。 If you see the code of Test.class file it will look like:如果您看到Test.class文件的代码,它将如下所示:

class Test extends Object implements CharSequence{

   // YOUR CODE

}

If you want to provide implementation of toString() , you can override it in your class.如果你想提供toString() ,你可以在你的类中覆盖它。

All the Java classes extend a predefined class called Object .所有 Java 类都扩展了一个名为Object的预定义类。 The Object class has a few methods defined for you, one of them being toString() . Object 类为您定义了一些方法,其中之一是toString() If we do not define a toString() in our own class, then the Object class's toString() is used.如果我们没有在自己的类中定义toString() ,则使用Object类的toString() However, if we are implementing an interface, we must define the methods declared in that interface.然而,如果我们正在实现一个接口,我们必须定义在该接口中声明的方法。 If we do not wish to define those methods in the implementing class, it must be declared as abstract .如果我们不想在实现类中定义这些方法,则必须将其声明为abstract

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM