public class Date {
    int year,month,day;
    public Date(int year,int month,int day) {
        this.year=year;
        this.month=month;
        this.day=day;
    }
    public  void bubbleSort(Date a[]) {
        for(int i=a.length-1;i<=1;i--) {
            for(int j=0;j<i;j++){
                if(compare(a[j],a[j+1])==1){
                    Date temp;
                    temp=a[j] ;
                    a[j] =a[j+1] ;
                    a[j+1] =temp;
                }
            }
    }
        
  
}
    public   int compare(Date a,Date b)
    {
        return  a.year>b.year?1
                :a.year<b.year?-1
                :a.month>b.month?1
                :a.month<b.month?-1
                :a.day>b.day?1
                :a.day<b.day?-1:0;
                
    }
}如上,有一个bubbleSort方法
接下来我要调用bubbleSort
public class TestSort {
    public  static void main(String args[]){
    Date[]day=new Date[5] ;
    day[0]=new Date(2004,5,7);
    day[1] =new Date(2004,5,8);
    day[2] =new Date(2005,2,3);
    day[3] =new Date(2008,4,6);
    day[4] =new Date(2005,1,1);
 *   bubbleSort(day);
    
    
    }}
结果打*的那一行出错了
说是:
找不到符号
符号:方法bubbleSort(Date[])
location:TestSort{5}问:
不是已经定义了bubbleSort 了吗,怎么不好调用啊!?
要怎么改正呢?
在此先谢谢各位了!

解决方案 »

  1.   

    因为bubbleSort并不是TestSort类的方法,而是Date类的方法,所以在TestSort类中不能直接调用,必须通过某个Date类实例来调用,例如:new Date(1,1,1).bubbleSort(day);当然,如果这样写,new Date(1,1,1)是一个没有任何含义的Date实例,程序可读性不好,更好一点的做法可能是把bubbleSort和compare方法都定义为静态static方法,就可以在外部这样调用:
    Date.bubbleSort(day);
      

  2.   

    给你说下你程序的几个问题。  首先  Date 类应该只保存数据, 他里面的两个方法不属于 Date类的职责 所以你该重新定义类 HandleDate  用来封装对 Date 进行操作   , 调用非静态方法,需要得到对该对象的引用 我给你改一下。  package com.meran.normalTest;public class HandleDate {
      public void bubbleSort(Date a[]) {
      for(int i=a.length-1;i<=1;i--) {
      for(int j=0;j<i;j++){
      if(compare(a[j],a[j+1])==1){
      Date temp;
      temp=a[j] ;
      a[j] =a[j+1] ;
      a[j+1] =temp;
      }
      }
      }
        
       
    }
      public int compare(Date a,Date b)
      {
      return a.year>b.year?1
      :a.year<b.year?-1
      :a.month>b.month?1
      :a.month<b.month?-1
      :a.day>b.day?1
      :a.day<b.day?-1:0;
        
      }}package com.meran.normalTest; public class Date {
      int year,month,day;
      public Date(int year,int month,int day) {
      this.year=year;
      this.month=month;
      this.day=day;
      } }
    package com.meran.normalTest;public class TestSort {
      public static void main(String args[]){
      Date[]day=new Date[5] ;
      day[0]=new Date(2004,5,7);
      day[1] =new Date(2004,5,8);
      day[2] =new Date(2005,2,3);
      day[3] =new Date(2008,4,6);
      day[4] =new Date(2005,1,1);
      HandleDate hd=new HandleDate();
      
      hd.bubbleSort(day);
        
        
      } }
      

  3.   

    估计楼主还是C语言之类的刚转过来,没有面向对象的思想。
    出现这个问题就是,你声明的bubbleSort是非static类型,必须要new一个Date对象的实例,比如是d,然后通过这个实例来调用d.bubbleSort()才可以,不能像C语言那样直接在main里面像调用库函数一样。如果想要不new对象调用,给你贴一下代码,方法都改成static
    public class Date {
    int year, month, day; public Date(int year, int month, int day) {
    this.year = year;
    this.month = month;
    this.day = day;
    } public static void bubbleSort(Date a[]) {
    for (int i = a.length - 1; i <= 1; i--) {
    for (int j = 0; j < i; j++) {
    if (compare(a[j], a[j + 1]) == 1) {
    Date temp;
    temp = a[j];
    a[j] = a[j + 1];
    a[j + 1] = temp;
    }
    }
    } } public static int compare(Date a, Date b) {
    return a.year > b.year ? 1 : a.year < b.year ? -1
    : a.month > b.month ? 1 : a.month < b.month ? -1
    : a.day > b.day ? 1 : a.day < b.day ? -1 : 0; }
    }
    public class TestSort {
    public static void main(String args[]){
      Date[]day=new Date[5] ;
      day[0]=new Date(2004,5,7);
      day[1] =new Date(2004,5,8);
      day[2] =new Date(2005,2,3);
      day[3] =new Date(2008,4,6);
      day[4] =new Date(2005,1,1);
      Date.bubbleSort(day);
        
        
      }}