编译错误和正确的语法

时间:2014-12-09 18:58:50

标签: java

这是我的代码;显然我缺少主力。请帮忙

class A {

    A get(){return this;}

}


class B1 extends A{
    B1 get(){return this;}
    void message(){System.out.println("welcome to covariant return type");}

    public static void main(String args[]){
        new B1().get().message();
    }
}  

1 个答案:

答案 0 :(得分:1)

A类

public class A
{
    public A get()
    {
        return this;
    }
}

B1级

public class B1 extends A
{
    public B1 get()
    {
        return this;
    }

    public void message()
    {
        System.out.println("welcome to covariant return type");
    }
}

主要方法

public static void main(String[] args)
{
     B1 b1 = new B1();
     B1 b2 = b1.get();

     b2.message();
}

如果你这样写出来,对你和其他人来说,阅读起来会更清楚一些。 main方法应该包含在一个类中,但我把它分开,这样你就可以看到代码的每个组件。

相关问题