黑马程序员技术交流社区

标题: 使用static定义内部类 [打印本页]

作者: cyc523633157    时间: 2014-8-3 23:38
标题: 使用static定义内部类
本帖最后由 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()会不行?会报错?

作者: zhuohong_xiao    时间: 2014-8-3 23:46
本人初学知识有限,没怎么看懂。
使用static我只记得一个原则
非静态的可以调用静态的,
静态的只能够调用静态的。
我用了很多次,没有错过。要不你检查下是不是static用错了地方。
希望对你有帮助。
作者: cyc523633157    时间: 2014-8-4 07:39
zhuohong_xiao 发表于 2014-8-3 23:46
本人初学知识有限,没怎么看懂。
使用static我只记得一个原则
非静态的可以调用静态的,

还有一个就是静态的不能调用非静态的。
作者: 刘沙    时间: 2014-8-4 08:22
new Outer.Inner() 这样应该可以
作者: xbz1986    时间: 2014-8-4 08:28
非静态的可以调用静态的,
静态的只能够调用静态的。
作者: 250669200@qq.co    时间: 2014-8-4 08:51
我编译了一下也不能找出原因,
作者: 江苏孙浩    时间: 2014-8-4 08:52
new Outer.Inner().print()应该可以吧。
作者: ysdolls    时间: 2014-8-4 09:33
本帖最后由 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的方式不对吧   多了一个括号

作者: 张涛的狂怒    时间: 2014-8-4 10:14
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.表示这个外部类下的内部类
作者: cyc523633157    时间: 2014-8-4 12:41
刘沙 发表于 2014-8-4 08:22
new Outer.Inner() 这样应该可以

没错,谢谢:)
作者: cyc523633157    时间: 2014-8-4 12:42
江苏孙浩 发表于 2014-8-4 08:52
new Outer.Inner().print()应该可以吧。

没错,谢谢:)
作者: cyc523633157    时间: 2014-8-4 12:44
ysdolls 发表于 2014-8-4 09:33
内部类 new的方式不对吧   多了一个括号

你答的太对了。,。。
作者: cyc523633157    时间: 2014-8-4 12:45
张涛的狂怒 发表于 2014-8-4 10:14
class Outer{
        private static String info = "hello world!!!";
        static class Inner{//使 ...

你答的太对了。,。。
作者: liubea    时间: 2014-8-4 13:55
静态内部类创建实例的方式是new Outer.Inner()的方式,
作者: 郑阳阳    时间: 2014-8-4 14:20
(new Outer().Inner()).print();这样写,或者是(new Outer.Inner()).print()
作者: 渴望学习    时间: 2014-8-4 15:16
路过~~~~~




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2