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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 尘埃落定 中级黑马   /  2014-7-23 14:15  /  8968 人查看  /  12 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 尘埃落定 于 2014-7-24 10:11 编辑

如下代码错误提示,什么问题?如何解决?
  1. /*重写object类中的equals方法示例。
  2. * */
  3. public class EqualsTest {
  4.         Demo_2 d1 = new Demo_2(5);
  5.         Demo_2 d2 = new Demo_2(6);
  6.         
  7.         boolean b = d1.equals(d2);
  8.         System.out.println(b);
  9.         /*错误提示:Multiple markers at this line
  10.         - Syntax error on token "b", VariableDeclaratorId expected after this
  11.          token*/
  12.         
  13.         Person_2 p = new Person_2();
  14.         boolean b1 = d1.equals(p);//
  15.         System.out.println(b1);
  16.         /*错误提示:Multiple markers at this line
  17.         - Syntax error on token(s), misplaced construct(s)
  18.         - Syntax error on token "b1", VariableDeclaratorId expected after this
  19.          token
复制代码


12 个回复

倒序浏览
主函数呢?
回复 使用道具 举报
关键的,你没截图
回复 使用道具 举报
public boolean equals(Object obj) {
        if (this == obj)
                return true;
        if (obj == null)
                return false;
        if(obj instanceof Person){
                Person p=(Person)obj;
                return this.name.equals(p.name)&&this.age==p.age;
                                }
        return false;
}

//你的代码不全,要不你截图,你看看这个事复写equals
回复 使用道具 举报
完整代码呢?
回复 使用道具 举报
shijianws 来自手机 中级黑马 2014-7-23 19:01:15
地板
没写在方法里,现在你的变量都是成员,但是system就提示编译不通过了
用主方法封装一下,马虎了吧
回复 使用道具 举报 1 0
class Demo_2{
        private int num;
        Demo_2(int num){
                this.num = num;
        }
       
        public boolean equals(Object obj){//覆盖父类方法
                if (!(obj instanceof Demo_2)){//判断类型是否一致
                        return false;
                }
                Demo_2 d = (Demo_2)obj;//向下转型。
                return this.num==d.num;
        }
}
class Person_2{
       
}

补上缺少的代码,不好意思,不知道怎么没粘贴完整
回复 使用道具 举报
以下为完整的代码;

  1. /*重写object类中的equals方法示例。
  2. * */
  3. public class EqualsTest2 {
  4.         Demo d1 = new Demo(5);
  5.         Demo d2 = new Demo(6);
  6.        
  7.         //System.out.println(d1.toString());
  8.         System.out.println(d1.equals(d2));
  9.         /*错误提示:Multiple markers at this line
  10.         - Syntax error on token "b", VariableDeclaratorId expected after this
  11.          token*/
  12.        

  13.         /*
  14.         Person p = new Person();
  15.         boolean b1 = d1.equals(p);//
  16.         System.out.println(b1);

  17.         */
  18.         /*错误提示:Multiple markers at this line
  19.         - Syntax error on token(s), misplaced construct(s)
  20.         - Syntax error on token "b1", VariableDeclaratorId expected after this
  21.          token
  22.           */
  23.        
  24. }

  25. class Demo{
  26.         private int num;
  27.         Demo(int num){
  28.                 this.num = num;
  29.         }
  30.        
  31.         public boolean equals(Object obj){//覆盖父类方法
  32.                 /*
  33.                 if (!(obj instanceof Demo)){//判断类型是否一致
  34.                         return false;
  35.                 }
  36.                 Demo d = (Demo)obj;//向下转型。
  37.                 */
  38.                 return this.num==d.num;
  39.         }
  40. }
  41. class Person{
  42.        
  43. }
复制代码




回复 使用道具 举报

楼主,没有主函数哦:P
回复 使用道具 举报
。。。你这写的是什么,没有主函数。。没法执行啊。。。public修饰了那个类了,还可能有主函数那个类出现吗?
回复 使用道具 举报 1 0
是不是在和包含这段代码的文件  同一个包下面的另外一个class文件,只有这种可能了。,你去找一下,再发,不然也不会报这种错误
回复 使用道具 举报
关度飞 发表于 2014-7-23 23:52
楼主,没有主函数哦

忘记主函数了{:3_60:}
我快受不了我自己了......
回复 使用道具 举报
JVM(Java虚拟机)都找不到程序的入口,也就是main函数
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马