A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王怡然 黑马帝   /  2012-4-9 18:17  /  2090 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class StudentMethod
  2. {
  3.         public static void scoreSpeak(double score)
  4.         {
  5.                 System.out.println("成绩:"+score);
  6.         }
  7.         public static void ageSpeak(int age)
  8.         {
  9.                 System.out.println("年龄:"+age);
  10.         }
  11. }

  12. class Student
  13. {
  14.         String name;
  15.         private int age;
  16.         private double score;
  17.         {
  18.                 System.out.println("学生");
  19.         }
  20.         Student(String name)
  21.         {
  22.                 System.out.println("姓名:"+name);
  23.         }
  24.         public void setChenj(double score)
  25.         {
  26.                 if (score>0 && score<100)
  27.                 {
  28.                         this.score = score;
  29.                         new StudentMethod.scoreSpeak();
  30.                 }
  31.                 else
  32.                         System.out.println("非法成绩;");
  33.         }
  34.         double getscore()
  35.         {
  36.                 return score;
  37.         }
  38.         /*
  39.         void chenjSpeak()
  40.         {
  41.                 System.out.println("成绩:"+chenj);
  42.         }
  43.         */
  44.         void setAge(int age)
  45.         {
  46.                 if (age>0 && age<120)
  47.                 {
  48.                         this.age = age;
  49.                         StudentMethod.ageSpeak();
  50.                 }
  51.                 else
  52.                         System.out.println("非法年龄");
  53.         }
  54.         int getAge()
  55.         {
  56.                 return age;
  57.         }
  58.         /*
  59.         void ageSpeak()
  60.         {
  61.                 System.out.println("年龄:"+this.age);
  62.         }
  63.         */
  64. }

  65. class StudentTest
  66. {
  67.         public static void main(String[] args)
  68.         {
  69.                 Student sd = new Student("张三");
  70.                 sd.setAge(20);
  71.                 sd.setChenj(80);
  72.         }
  73. }
复制代码

6 个回复

倒序浏览
返回值类型写错了 应该和传入的一样··不应该是void
回复 使用道具 举报
李南江 发表于 2012-4-9 18:21
返回值类型写错了 应该和传入的一样··不应该是void

哪个位置?
回复 使用道具 举报
class StudentMethod
{
        public static void scoreSpeak(double score)
        {
                System.out.println("成绩:"+score);
        }
        public static void ageSpeak(int age)
        {
                System.out.println("年龄:"+age);
        }
}

class Student
{
        String name;
        private int age;
        private double score;
        {
                System.out.println("学生");
        }
        Student(String name)
        {
                System.out.println("姓名:"+name);
        }
        public void setChenj(double score)
        {
                if (score>0 && score<100)
                {
                        this.score = score;
                        new StudentMethod.scoreSpeak();//这里应该是 new StudentMethod().scoreSpeak();                }
                else
                        System.out.println("非法成绩;");
        }
        double getscore()
        {
                return score;
        }
        /*
        void chenjSpeak()
        {
                System.out.println("成绩:"+chenj);
        }
        */
        void setAge(int age)
        {
                if (age>0 && age<120)
                {
                        this.age = age;
                        StudentMethod.ageSpeak();//这里必须传入一个参数age,StudentMethod.ageSpeak(age);                }
                else
                        System.out.println("非法年龄");
        }
        int getAge()
        {
                return age;
        }
        /*
        void ageSpeak()
        {
                System.out.println("年龄:"+this.age);
        }
        */
}

class StudentTest
{
        public static void main(String[] args)
        {
                Student sd = new Student("张三");
                sd.setAge(20);
                sd.setChenj(80);
        }
}
回复 使用道具 举报
03.        public static void scoreSpeak(double score)

07         public static void ageSpeak(int age)

30.                        new StudentMethod.scoreSpeak();  应该是StudentMethod.scoreSpeak(score);

50.                        StudentMethod.ageSpeak();应该是  StudentMethod.ageSpeak(age);

你的代码没什么大问题,主要是调用的时候,忘了想你定义的函数中传值了

评分

参与人数 1黑马币 +2 收起 理由
王怡然 + 2 感谢,原来只是个小问题.看来得要相信自己..

查看全部评分

回复 使用道具 举报
你定义的是静态方法,可以直接用

类.静态方法名()    调用
回复 使用道具 举报
public static void scoreSpeak(double score)
public static void ageSpeak(int age)
定义中都有参数
调用时夜需要传入参数
new StudentMethod.scoreSpeak();  ==>new StudentMethod().scoreSpeak(score);
StudentMethod.ageSpeak();==>StudentMethod.ageSpeak(age);
应该是这样吧。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马