Java 接口is'中的方法是什么时候;两者';覆盖和实现?
下面的代码是我在笔记本电脑的netbeans中运行的代码,用于理解该接口Java 接口is'中的方法是什么时候;两者';覆盖和实现?,java,interface,Java,Interface,下面的代码是我在笔记本电脑的netbeans中运行的代码,用于理解该接口 public class CharSequenceDemo implements CharSequence { private String s; public CharSequenceDemo(String s) { //It would be much more efficient to just reverse the string //in the construc
public class CharSequenceDemo implements CharSequence {
private String s;
public CharSequenceDemo(String s) {
//It would be much more efficient to just reverse the string
//in the constructor. But a lot less fun!
this.s = s;
}
//If the string is backwards, the end is the beginning!
private int fromEnd(int i) {
return s.length() - 1 - i;
}
public char charAt(int i) {
if ((i < 0) || (i >= s.length())) {
throw new StringIndexOutOfBoundsException(i);
}
return s.charAt(fromEnd(i));
}
public int length() {
return s.length();
}
public CharSequence subSequence(int start, int end) {
if (start < 0) {
throw new StringIndexOutOfBoundsException(start);
}
if (end > s.length()) {
throw new StringIndexOutOfBoundsException(end);
}
if (start > end) {
throw new StringIndexOutOfBoundsException(start - end);
}
StringBuilder sub =
new StringBuilder(s.subSequence(fromEnd(end), fromEnd(start)));
return sub.reverse();
}
public String toString() {
StringBuilder s = new StringBuilder(this.s);
return s.reverse().toString();
}
//Random int from 0 to max. As random() generates values between 0 and 0.9999
private static int random(int max) {
return (int) Math.round(Math.random() * (max+1));
}
public static void main(String[] args) {
CharSequenceDemo s =
new CharSequenceDemo("Write a class that implements the CharSequence interface found in the java.lang package.");
//exercise charAt() and length()
for (int i = 0; i < s.length(); i++) {
System.out.print(s.charAt(i));
}
System.out.println("");
//exercise subSequence() and length();
int start = random(s.length() - 1);
int end = random(s.length() - 1 - start) + start;
System.out.println(s.subSequence(start, end));
//exercise toString();
System.out.println(s);
}
}
public类CharSequenceDemo实现CharSequence{
私有字符串;
公共字符序列演示(字符串s){
//只需反转字符串,效率会更高
//在构造器中。但是没有那么有趣!
这个.s=s;
}
//如果字符串向后,则结束即开始!
私有整数fromEnd(整数i){
返回s.length()-1-i;
}
公共字符(int i){
如果((i<0)|(i>=s.length()){
抛出新StringIndexOutOfBoundsException(i);
}
返回s.charAt(从结束(i));
}
公共整数长度(){
返回s.length();
}
公共字符序列子序列(int开始,int结束){
如果(开始<0){
抛出新StringIndexOutOfBoundsException(开始);
}
如果(结束>s.长度()){
抛出新StringIndexOutOfBoundsException(结束);
}
如果(开始>结束){
抛出新StringIndexOutOfBoundsException(开始-结束);
}
StringBuilder子模块=
新的StringBuilder(s.子序列(fromEnd(end)、fromEnd(start));
返回sub.reverse();
}
公共字符串toString(){
StringBuilder s=新的StringBuilder(this.s);
返回s.reverse().toString();
}
//Random int从0到最大值。As Random()生成0到0.9999之间的值
私有静态int-random(int-max){
return(int)Math.round(Math.random()*(max+1));
}
公共静态void main(字符串[]args){
字符序列=
新的CharSequenceDemo(“编写一个实现java.lang包中的CharSequence接口的类”);
//练习charAt()和length()
对于(int i=0;i
对于方法toString netbeans,表示-->方法已重写并实现,但对于其他方法,则表示已重写。重写和实现方法之间有什么区别?他们不是一回事吗 实现-您必须从实现的接口定义每个方法 扩展-您可以使用在父类中编写的方法
实现来自您的接口
重写来自对象(每个java对象都隐式扩展对象)实现-您必须从实现的接口定义每个方法 扩展-您可以使用在父类中编写的方法
实现来自您的接口
重写来自对象(每个java对象隐式扩展对象)在java 5.0中,@Override只能放在重写在超类中定义的方法的方法上,而不能放在接口中声明的方法上。在Java6+中,可以将@Override放在实现和重写方法上 在Java 6+中,您可以在不实现
interface A {
void method();
}
interface B extends A {
@Override
void method();
}
abstract class C implements A {
@Override
public abstract void method();
}
class D extends C {
@Override
public void method() {
}
}
在Java 5.0中,@Override只能放在重写超类中定义的方法的方法上,而不能放在接口中声明的方法上。在Java6+中,可以将@Override放在实现和重写方法上 在Java 6+中,您可以在不实现
interface A {
void method();
}
interface B extends A {
@Override
void method();
}
abstract class C implements A {
@Override
public abstract void method();
}
class D extends C {
@Override
public void method() {
}
}
具有toString()
方法,当您的类实现CharSequence时,您的类将被迫实现toString()(从而实现),然而,由于所有类的扩展java.lang.Object
都声明了toString()
您的类将从对象类继承它(因此被重写)
你的情况就是这样
- toString()-->从CharSequence实现
- toString()-->从java.lang.Object重写
toString()
方法,当类实现CharSequence时,您的类将被迫实现toString()(从而实现),然而,当所有类的extendjava.lang.Object
声明了toString()
时,您的类将从对象类继承它(因此被重写)
你的情况就是这样
- toString()-->从CharSequence实现
- toString()-->从java.lang.Object重写
toString方法表示重写,因为接口重写了所有Java对象所拥有的toString方法,并且您也在CharSequenceDemo中实现了它。重写时,您修改了现有的行为。实现时,您尊重现有行为。重写时,您修改现有行为。当您实现时,您尊重现有的行为。因此,它们都“指向”被覆盖的toString。我从哪里可以接受我得到了正确的答案?所以,它们都指向被覆盖的toString。我从哪里可以接受我得到了正确的答案?