2 u013232949 u013232949 于 2016.02.29 20:45 提问

not applicable for the arguments

新手学习接口的时候 写了个compareTo方法。
视频上未有错误。但是自己写的时候出错了。
网上查了好久 也不得要领 。 大概是理解了 ComparebleCircle 这个类不适用于Object类。
很想知道到底错在哪里 。按理说object类型是所有类的父类 传入 ComparebleCircle类型不应该出错啊。

错误原因:
The method compareTo(Object) in the type ComparebleCircle is not applicable for the arguments
(ComparebleCircle)

package testInterface;

public class TestCircle {
ComparebleCircle circle1 = new ComparebleCircle(2.1);
ComparebleCircle circle2 = new ComparebleCircle(2.2);
ComparebleCircle circle3 = new ComparebleCircle(2.3);

int i1 = circle1.compareTo(circle2);//此行报错

}
interface CompareObject{
//若返回值为0,代表相等,若为正数,代表当前对象大;负数代表当前对象小
public int compareTo(Object o);
}
class Circle{
protected double raduis;

protected Circle() {
    super();
}

protected Circle(double raduis) {
    super();
    this.raduis = raduis;
}

public double getRaduis() {
    return raduis;
}

public void setRaduis(double raduis) {
    this.raduis = raduis;
}

}
class ComparebleCircle extends Circle implements CompareObject{
public ComparebleCircle(double redius) {
super(redius);

}
@Override
public int compareTo(Object o) {
if(this == o){
return 0;
}else if(o instanceof ComparebleCircle){
ComparebleCircle c = (ComparebleCircle) o;
if(this.getRaduis()>c.getRaduis()){
return 1;
}else if(this.getRaduis()<c.getRaduis()){
return -1;
}else{
return 0;
}
}else{
return -2;
// throw new RuntimeException("传入的对象类型不匹配非ComparebleCircle类型");
}

}

}

1个回答

wojiushiwo945you
wojiushiwo945you   Ds   Rxr 2016.02.29 21:44
已采纳

测试了你的代码,没有问题的。我是这样测试的,第一个类定义修改下:

 public class TestCircle {
    public static void main(String[] args) {
        ComparebleCircle circle1 = new ComparebleCircle(2.1);
         ComparebleCircle circle2 = new ComparebleCircle(2.2);
         ComparebleCircle circle3 = new ComparebleCircle(2.3);
        int i1 = circle1.compareTo(circle2);//此行报错
        System.out.println(i1);
    }
}

输出的是-1,我的开发环境是Eclipse+jdk1.8

u013232949
u013232949 我这里确实依旧报错。估计还是因为开发环境的问题吧 。 很感谢你
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
JavaThe method
The method setAttribute String Object in the type ServletRequest is not applicable for the arguments String int
Java+javabean
The method setAttribute String Object in the type ServletRequest is not applicable for the arguments String int
解决windows下安装Apache+PHP出现LoadModule takes two
装apache时突然出现了LoadModule takes two arguments, a module name and the name of a shared object file to load it from 加入下面两行,apache就不能启动的问题 LoadModule php5_module modules/php5apache2.dll AddType application/x-httpd-php .php
Arguments and results
Arguments and results by James Noble
Handler Function input arguments
Function input arguments always precede output arguments
第一行代码源码
<?xml version="1.0" encoding="UTF-8"?> <projectDescription> <name>HelloWorld</name> <comment></comment> <projects> </projects> <buildSpec> <buildCommand> <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>com.android.ide.eclipse.adt.ApkBuilder</name> <arguments> </arguments> </buildCommand> </buildSpec> <natures> <nature>com.android.ide.eclipse.adt.AndroidNature</nature> <nature>org.eclipse.jdt.core.javanature</nature> </natures> </projectDescription>
GridView表头固定
r superTables = []; var superTable = function (tableId) { // add this instance to the supertables array var x = superTables.length; this.index = x; superTables[x] = this; // initialize the parameters this.sourceTable = document.getElementById(tableId); this.hasOptions = (arguments.length > 1) ? true : false; this.skin = (this.hasOptions && arguments[1].cssSkin) ? "_" + arguments[1].cssSkin : "_Default"; this.headerRows = (this.hasOptions && arguments[1].headerRows) ? parseInt(arguments[1].headerRows) : 1; this.fixedCols = (this.hasOptions && arguments[1].fixedCols) ? parseInt(arguments[1].fixedCols) : 0; this.columnWidths = (this.hasOptions && arguments[1].colWidths) ? arguments[1].colWidths : []; this.callbackFunc = (this.hasOptions && arguments[1].onFinish) ? arguments[1].onFinish : function () { return this; }; var i, j, k;
JS:arguments
JS:arguments
Arguments and Results
Arguments and Results 参数,资源
specification is applicable of gs-12-547
This specification is applicable to the termination characteristics of 0.8mm pitch BergStak® family of products, with 30 uin/ 0.76 um Palladium-Nickel plating, which provides electrical connections between parallel mounted boards.