小弟初试java泛型遭遇编译错误,求前辈帮助指点,不胜感谢

xiaolongren1989 2014-03-07 11:37:03
小弟初次尝试java泛型遭遇困境无法解决,求大神前辈指点迷境,不胜感谢

public class Max <DataType extends Comparable < DataType > > {
public static DataType max(DataType a, DataType b) {
int cmp = a.compareTo(b);
if (cmp == -1) {
return b;
}
return a;
}

}

Eclipse爆出编译错误如下:
Multiple markers at this line
- Cannot make a static reference to the non-static type
DataType
- Cannot make a static reference to the non-static type
DataType
- Cannot make a static reference to the non-static type
DataType
...全文
296 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mergades 2014-03-07
  • 打赏
  • 举报
回复
Cannot make a static reference to the non-static type 非静态引用不能使用静态引用
haha_321 2014-03-07
  • 打赏
  • 举报
回复
Cannot make a static reference to the non-static type 这个意思应该很明确了,去掉max方法中的static
代码间的舞者 2014-03-07
  • 打赏
  • 举报
回复
引用 10 楼 morning_china 的回复:
使用方法泛型可以满足你的要求。 代码大致是: public class Max { public static <DataType extends Comparable<DataType>> DataType max(DataType a, DataType b) { int cmp = a.compareTo(b); if (cmp == -1) { return b; } return a; } }
+1
xiaolongren1989 2014-03-07
  • 打赏
  • 举报
回复
引用 10 楼 morning_china 的回复:
使用方法泛型可以满足你的要求。 代码大致是: public class Max { public static <DataType extends Comparable<DataType>> DataType max(DataType a, DataType b) { int cmp = a.compareTo(b); if (cmp == -1) { return b; } return a; } }
太好了,是我想寻找的答案;谢谢前辈
xiaolongren1989 2014-03-07
  • 打赏
  • 举报
回复
引用 9 楼 wangxf_8341 的回复:
泛型放在方法上

public class Max {
    public static <T  extends Comparable < T >> T  max(T a, T b) {
        int cmp = a.compareTo(b);
        if (cmp == -1) {
            return b;
        }
        return a;
    }
}

谢谢前辈指教,答案正是我想知道的
morning_china 2014-03-07
  • 打赏
  • 举报
回复
使用方法泛型可以满足你的要求。 代码大致是: public class Max { public static <DataType extends Comparable<DataType>> DataType max(DataType a, DataType b) { int cmp = a.compareTo(b); if (cmp == -1) { return b; } return a; } }
淡定的峰哥 2014-03-07
  • 打赏
  • 举报
回复
泛型放在方法上

public class Max {
    public static <T  extends Comparable < T >> T  max(T a, T b) {
        int cmp = a.compareTo(b);
        if (cmp == -1) {
            return b;
        }
        return a;
    }
}

xiaolongren1989 2014-03-07
  • 打赏
  • 举报
回复
引用 3 楼 aaaaaqiwang 的回复:
“静态方法中不能操作非静态变量和非静态方法”,记住这个原则就行了。
谢谢解答,我了解这个报错的改正方法;但是我想用使我写的这个泛型的max成为这种Math.max()这样来调用,怎么实现那?
xiaolongren1989 2014-03-07
  • 打赏
  • 举报
回复
引用 2 楼 fuli_mouren 的回复:
Cannot make a static reference to the non-static type 非静态引用不能使用静态引用
谢谢解答,我了解这个报错的改正方法;但是我想用使我写的这个泛型的max成为这种Math.max()这样来调用,怎么实现那?
xiaolongren1989 2014-03-07
  • 打赏
  • 举报
回复
引用 1 楼 haha_321 的回复:
Cannot make a static reference to the non-static type 这个意思应该很明确了,去掉max方法中的static
谢谢解答,我了解这个报错的改正方法;但是我想用使我写的这个泛型的max成为这种Math.max()这样来调用,怎么实现那?
波儸密 2014-03-07
  • 打赏
  • 举报
回复
public static DataType max(DataType a, DataType b) { 改为 public DataType max(DataType a, DataType b) {
tony4geek 2014-03-07
  • 打赏
  • 举报
回复
看错误信息。。
Kristen_Ge 2014-03-07
  • 打赏
  • 举报
回复
“静态方法中不能操作非静态变量和非静态方法”,记住这个原则就行了。

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧