`
sunsy
  • 浏览: 37649 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

Class/InnerClass

阅读更多
昨天工作中提到了多个class写在一个文件的方式,写了个例子,记录一下这种情况下各类的调用方式。
 
package test;
public class MultiClassesTest {
 
 public static class InnerClass {
  @Override
  public String toString() {
   return super.toString() + " and my own toString result is \"xixi\"";
  }
  
 }
 
 public class InnerClass2 {
  @Override
  public String toString() {
   return super.toString() + " and my own toString result is \"qiqi\"";
  }
  
 }
 
 public static void main(String[] args) {
  TheOtherClass to = new TheOtherClass();
  System.out.println(to.toString());
  MultiClassesTest mt = new MultiClassesTest();
  System.out.println(mt.toString());
  InnerClass ic = new InnerClass();
  System.out.println(ic.toString());
  InnerClass2 ic2 = new MultiClassesTest().new InnerClass2();
  System.out.println(ic2.toString());
 }
 @Override
 public String toString() {
  return super.toString() + " and my own toString result is \"hehe\"";
 }
}
class TheOtherClass {
 public static void main(String[] args) {
  MultiClassesTest mt = new MultiClassesTest();
  System.out.println(mt.toString());
  TheOtherClass to = new TheOtherClass();
  System.out.println(to.toString());
  MultiClassesTest.InnerClass ic = new MultiClassesTest.InnerClass();
  System.out.println(ic.toString());
  MultiClassesTest.InnerClass2 ic2 = new MultiClassesTest().new InnerClass2();
  System.out.println(ic2.toString());
 }
 @Override
 public String toString() {
  return super.toString() + " and my own toString result is \"haha\"";
 }
}
编译及运行结果:
E:\TestProject>javac test/MultiClassesTest.java
E:\TestProject>java test.MultiClassesTest
test.TheOtherClass@61de33 and my own toString result is "haha"
test.MultiClassesTest@14318bb and my own toString result is "hehe"
test.MultiClassesTest$InnerClass@ca0b6 and my own toString result is "xixi"
test.MultiClassesTest$InnerClass2@10b30a7 and my own toString result is "qiqi"
E:\TestProject>java test.TheOtherClass
test.MultiClassesTest@61de33 and my own toString result is "hehe"
test.TheOtherClass@14318bb and my own toString result is "haha"
test.MultiClassesTest$InnerClass@ca0b6 and my own toString result is "xixi"
test.MultiClassesTest$InnerClass2@10b30a7 and my own toString result is "qiqi"
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics