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

© cyc523633157 中级黑马   /  2014-8-3 23:38  /  1278 人查看  /  15 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 cyc523633157 于 2014-8-4 22:10 编辑
  1. package com;

  2. import com.cer.Outer.Inner;

  3. class Outer{
  4.         private static String info = "hello world!!!";
  5.         static class Inner{//使用static第一内部类为外部类
  6.                 public void print(){
  7.                         System.out.println(info);
  8.                 }
  9.         }
  10. }
  11. public class InnerClassDemo03 {
  12.         public static void main(String[] args) {
  13.                  new Outer().Inner().print();//访问内部类
  14.         }

  15. }
复制代码
为什么这样访问内部类,Inner()会不行?会报错?

评分

参与人数 1技术分 +1 收起 理由
格子、 + 1 淡定

查看全部评分

15 个回复

倒序浏览
本人初学知识有限,没怎么看懂。
使用static我只记得一个原则
非静态的可以调用静态的,
静态的只能够调用静态的。
我用了很多次,没有错过。要不你检查下是不是static用错了地方。
希望对你有帮助。
回复 使用道具 举报
zhuohong_xiao 发表于 2014-8-3 23:46
本人初学知识有限,没怎么看懂。
使用static我只记得一个原则
非静态的可以调用静态的,

还有一个就是静态的不能调用非静态的。
回复 使用道具 举报
new Outer.Inner() 这样应该可以
回复 使用道具 举报
非静态的可以调用静态的,
静态的只能够调用静态的。
回复 使用道具 举报
我编译了一下也不能找出原因,
回复 使用道具 举报
new Outer.Inner().print()应该可以吧。
回复 使用道具 举报
本帖最后由 ysdolls 于 2014-8-4 09:35 编辑
  1. package com.ys.doll;

  2. class Outer{
  3.     private static String info = "hello world!!!";
  4.     static class Inner{//使用static第一内部类为外部类
  5.             public void print(){
  6.                     System.out.println(info);
  7.             }
  8.     }
  9. }
  10. public class InnerClassDemo03 {
  11.     public static void main(String[] args) {
  12.                     new Outer.Inner().print();
  13. //            Outer.Inner oi = new Outer.Inner();
  14. //            oi.print();
  15.     }

  16. }
复制代码

内部类 new的方式不对吧   多了一个括号

评分

参与人数 1技术分 +1 收起 理由
格子、 + 1 很给力!

查看全部评分

回复 使用道具 举报
class Outer{
        private static String info = "hello world!!!";
        static class Inner{//使用static第一内部类为外部类
                public void print(){
                        System.out.println(info);
                }
                }
}
public class InnerClassDemo03 {
        public static void main(String[] args) {
               Outer.Inner in=new  Outer.Inner();
               in.print();
        }

}
——————————————————————————————————————————
class Outer{
        private static String info = "hello world!!!";
        static class Inner{//使用static第一内部类为外部类
                public static void print(){
                        System.out.println(info);
                }
                }
}
public class InnerClassDemo03 {
        public static void main(String[] args) {
              new Outer.Inner().print();;
        }

}
因为你内部累的方法不是静态的,而主函数的方法是静态的,静态只能调用静态,直接new  外部类.内部类().print();外部类只相当于this.表示这个外部类下的内部类

评分

参与人数 1技术分 +1 收起 理由
格子、 + 1 赞一个!

查看全部评分

回复 使用道具 举报
刘沙 发表于 2014-8-4 08:22
new Outer.Inner() 这样应该可以

没错,谢谢:)
回复 使用道具 举报
江苏孙浩 发表于 2014-8-4 08:52
new Outer.Inner().print()应该可以吧。

没错,谢谢:)
回复 使用道具 举报
ysdolls 发表于 2014-8-4 09:33
内部类 new的方式不对吧   多了一个括号

你答的太对了。,。。
回复 使用道具 举报
张涛的狂怒 发表于 2014-8-4 10:14
class Outer{
        private static String info = "hello world!!!";
        static class Inner{//使 ...

你答的太对了。,。。
回复 使用道具 举报
静态内部类创建实例的方式是new Outer.Inner()的方式,
回复 使用道具 举报
(new Outer().Inner()).print();这样写,或者是(new Outer.Inner()).print()
回复 使用道具 举报
路过~~~~~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马