本帖最后由 赵崇友 于 2013-4-9 22:36 编辑
- class Bowl //Bowl类
- {
- Bowl(int marker)//带参数构造器
- {
- System.out.println("Bowl("+marker+")");
- }
- void f1(int marker)//成员方法
- {
- System.out.println("f1("+marker+")");
- }
- }
- //---------------------------------------
- class Table //Table类
- {
- static Bowl bowl1=new Bowl(1);//在table类中创建一个Bowl对象引用bowl1
- Table() //无参数构造器
- {
- System.out.println("Table()");
- bowl2.f1(1); //对象引用Bwol中的f1()方法
- }
- void f2(int marker)//成员方法
- {
- System.out.println("f2("+marker+")");
- }
- static Bowl bowl2=new Bowl(2);//创建Bowl对象引用bowl2
- }
- //--------------------------------
- class Cupboard //Cupboard类
- {
- Bowl bowl3=new Bowl(3);//创建一个Bowl对象引用bowl3
- static Bowl bowl4=new Bowl(4);//创建一个Bowl对象引用bowl4
- Cupboard() //构造器
- {
- System.out.println("Cupboard()");
- bowl4.f1(2);//对象调用f1()方法
- }
- void f3(int marker)//成员方法
- {
- System.out.println("f3("+marker+")");
- }
- static Bowl bowl5=new Bowl(5);//);//创建一个Bowl对象引用bowl5
- }
- //----------------------------------------
- public class Practice09
- {
- public static void main(String[] args)//主方法
- {
- System.out.println("Creating new Cupboard() in main");//输出语句
- new Cupboard();//new一个Cupboard对象
- System.out.println("Creating new Cupboard() in main");//输出语句
- new Cupboard();
- table.f2(1);//对象调用Table中f2()方法
- cupboard.f3(1);//对象调用Cupboard中的f3()方法
- }
- static Table table=new Table();//);//创建一个Table对象引用table
- static Cupboard cupboard=new Cupboard();//创建一个Cupboard对象引用cupboard
- }
- //以下是运行的结果:
- /*output:
- Bowl(1)
- Bowl(2)
- Table()
- f1(1)
- Bowl(4)
- Bowl(5)
- Bowl(3)
- Cupboard()
- f1(2)
- Creating new Cupboard() in m
- Bowl(3)
- Cupboard()
- f1(2)
- Creating new Cupboard() in m
- Bowl(3)
- Cupboard()
- f1(2)
- f2(1)
- f3(1)
- */
复制代码 |