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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Jeffery 中级黑马   /  2015-8-23 00:25  /  1073 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

interface Inter { void show(); }
        class Outer { //补齐代码 }
        class OuterDemo {
            public static void main(String[] args) {
                      Outer.method().show();
                  }
        }
要求在控制台输出”HelloWorld”

10 个回复

倒序浏览
  1. class Outer {
  2.         public static Outer method()
  3.         {
  4.                 return new Outer();
  5.         }
  6.         public void show()
  7.         {
  8.                 System.out.println("Hello World");
  9.         }
  10. }
复制代码

思路是这样的,method();本身是方法,而且可以通过类名直接调用,所以他一定是静态方法。而使用了method()方法后还能调用另一个方法,说明这个方法肯定返回的是一个对象,而在这个题中只能返回本类对象了
返回本类对象后再通过本类对象调用本类中的非静态方法。

评分

参与人数 1黑马币 +2 收起 理由
洋葱头头 + 2

查看全部评分

回复 使用道具 举报 1 0
  1. package ccc;
  2. interface Inter{public void show();}
  3. class Outer{
  4.         Inter method(){
  5.                 return new Inter(){
  6.                         public void show(){
  7.                                 System.out.println("hellow world");
  8.                         }
  9.                 };
  10.         }
  11. }
  12. public class OuterDemo {
  13.         public static void main(String[] args) {
  14.                 new Outer().method().show();
  15.         }
  16. }
复制代码

主函数跟你那个不一样,你那样写我也不懂。

评分

参与人数 1黑马币 +1 收起 理由
洋葱头头 + 1 很给力!

查看全部评分

回复 使用道具 举报
//要求在控制台输出”HelloWorld”

interface Inter
{
        void show();
}
class Outer
{ //补齐代码
    public  static   Inter      method()
        {
          Inter   in=new   Inter(){
                     public  void  show()
                     {
                         System.out.println("HelloWorld");                             
                     }
                  };                              
              return   in;                 
        }
}
class OuterDemo
{
  public static void main(String[] args)
   {
       Outer.method().show();
   }
}

评分

参与人数 1黑马币 +1 收起 理由
洋葱头头 + 1 很给力!

查看全部评分

回复 使用道具 举报
学习了,路过看看!
回复 使用道具 举报
  1. public class tex {
  2.         public static void main(String[] args) {
  3.                 Outer.method().show();
  4.         }

  5. }
  6. interface Inter { void show(); }
  7. //补齐代码
  8. class Outer{
  9.         public static Inter method(){
  10.                 return new Inter(){
  11.                 public void show(){
  12.                                 System.out.println("helloworld");
  13.                         }
  14.                 };
  15.         }
  16. }
复制代码




回复 使用道具 举报
fmi110 高级黑马 2015-8-23 12:24:55
7#
sven556677 发表于 2015-8-23 08:33
主函数跟你那个不一样,你那样写我也不懂。

类名调用静态函数,说明需要把show()声明为 static
回复 使用道具 举报
sven556677 发表于 2015-8-23 08:33
主函数跟你那个不一样,你那样写我也不懂。

谢谢大神了
回复 使用道具 举报
黄蒙 发表于 2015-8-23 08:03
思路是这样的,method();本身是方法,而且可以通过类名直接调用,所以他一定是静态方法。而使用了method() ...

学习了,谢谢
回复 使用道具 举报
蛋糕的爱意 发表于 2015-8-23 08:55
//要求在控制台输出”HelloWorld”

interface Inter

谢谢拉,长见识了
回复 使用道具 举报
interface Inter {
    void show();
    // public abstract
}

class Outer {
    // 补齐代码
    public static Inter method() {
        // 子类对象 -- 子类匿名对象
        return new Inter() {
            public void show() {
                System.out.println("HelloWorld");
            }
        };
    }
}

class OuterDemo {
    public static void main(String[] args) {
        Outer.method().show();
    /*
        1:Outer.method()可以看出method()应该是Outer中的一个静态方法。
        2:Outer.method().show()可以看出method()方法的返回值是一个对象。
            又由于接口Inter中有一个show()方法,所以我认为method()方法的返回值类型是一个接口。
    */
    }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马