🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: cn.bbs.comp.program
1 messages
1 total messages Started by ���ܵ�Сʨ Sat, 18 Apr 1998 00:00
���������Ƕ��
#1264
Author: ���ܵ�Сʨ
Date: Sat, 18 Apr 1998 00:00
178 lines
12984 bytes
JDK1.1���﷨:��Ƕ��(1)

 Java���ϰ汾��ֻ�ṩ�˶�����,���DZ����ǰ�ij�Ա��Ȼ������,
�°��Java1.1�������Ա������Ƕ��(inner class),���ǿ�������
����ij�Ա,���߾ֲ��ش�����һ���������,��������ش�����һ��
���ʽ�С�

    ���������Ƕ��ĸ���

    ��Ƕ�����Ҫ������:

    1.�������ϸ�,��Ƕ�����������������������Ч��������
���ڰ��ڽṹ���ࡣ
    2.��Ƕ��Ĵ������ʹ�ð�Χ�������еļ���,�����ࡢ��Χ
���ʵ���Ա,�Լ���Χ��ľֲ�������
    ��Ƕ�����ɻ�����ı�̺Ϳ�ṹ���϶�����,���ֳ����
�����ȱ�Beta��������á����ô�����Ƕ��Ŀ�ṹ,Java����Ա��
�Ը����׵ؽ�������ϵ����,��Ϊ��ʱ����Ա�����Ϊ��ӽ�������
��Ҫ���ƵĶ���,���ҿ���ֱ������������Ҫ�����֡����Ŷ�����
λ�õ����Ƶ����,Java������������ø�����,����ͬPascal��
Scheme��Щ��ͳ�Ŀ�ṹ������һ��
    ����,����Ա���Խ��ඨ��Ϊ������ľ�̬(static)��Ա����Ϊ
��̬���Ա�������Ϊ���Ա���඼����Ϊ�����ࡣ��������Ƕ���
�������:������ֻ��ֱ���������Լ���ʵ������������ַ�ʽʵ��
��Ƕ�׵�������ʹ������Ϊ�߼�����ص�һ��μ��������ṩһ����
�ư����֯�ṹ,������Щ�μ��๲���˽�г�Ա��ȫ����дȨ�ޡ�
    ��Ƕ���Ƕ�׶������ɱ�����ʵ��,���Ҳ���Ҫ��Java�������
�κθı䡣���Dz����ƻ����ִ�Java������Դ���뼶�Ͷ����Ƽ��ϵ�
�����ԡ�
    ������Щ�µ�Ƕ���๹�춼��ת���ɲ�ʹ����Ƕ���Java1.0��
�������Խ��͡���Java 1.1����������Java������ֽڴ���ʱ,��Щ
�ֽڴ������������(�����)��Դ���뵽Դ�����ת�����,�Ӷ�
ʹ��ͬ��Java1.1�����������Ķ����ƴ�����Լ��ݡ���Щ�ֽڴ�
��Ҳ�������һ�������Ա�־�Ա��������Java1.1������ָ��ÿ��
Ƕ����Ĵ��ڡ��⽫�������һ�����ۡ�

    �򵥵Ľӿ������
    �������������һ���ӿ��ࡣ���������ָ����һ�ִ����һ��
�������䱾��Ľ������������շ������á�Ϊ���ܽ�������AWT��J
avaBean�������¼�,�ӿ���ͨ��������Ҫ�ġ���Java1.1��,�ӿ�����
���ױ�����Ϊ��Ƕ��,������Ҫ�ӿڵ����С�
    ������һ�����������FixedStack,��ʵ��һ����ջ,���ҿ�����
�����¶�ջ�е�Ԫ�ؼ���:
    public  class  FixedStack  {
       Object  array[];
       int  top = 0;
       FixedStack ( int  fixedSizeLimit ){
    array = new  Object [fixedSizeLimit];
       }
       public  void  push ( Object  item ){
    array [top++] = item;
       }
       public  boolean  isEmpty( ) {
    return  top = 0;
       }
       // other  stack  methods  go  here...
       class  Enumerator  implements  java.util.Enumeration
        int  count = top;
        public  boolean  hasMoreElements( )  {
     return  count > 0;
        }
        public  Object  nextElement( )  {
        if (count == 0 )
     throw  new  NoSuchElementException( "Fix" );
       return  array[- - count];
        }
       }
       public  java.util.Enumeration  elements( )  {
        return  new  Enumerator( );
       }
      }
    �ӿ�java.util.Enumeration��������һϵ��ֵ��ͻ�������Fix
edStack����(���Ҳ�Ӧ��!)ֱ��ʵ��Enumeration�ӿ�,���������Ҫ
һ���ӿ�������Enumeration����ʽ������һϵ��Ԫ�ء���Ȼ,�����
������Ҫ�߱���д��ջ����Ԫ�ص�ij��Ȩ�ޡ�������Ա������ӿ�
��Ķ��������FixedStack��,��ýӿ���Ĵ������ֱ�����ö�ջ
�����ʵ�������
    ��Java������,��ķǾ�̬��Ա���Ի�������,�������Ƕ�������
��ǰʵ��this��صĺ��塣���,��FixedStack��ʵ�����array����
Ϊʵ��push������,Ҳ��������Ƕ��Fixe dStack.Enumerator��
������������á�����ʵ������嶼"֪��"�䵱ǰʵ��thisһ��
,�κ�һ����Enumerator�������Ƕ���еĴ���Ҳ��"֪��"���Χʵ
��,������array����ı�����ȡ�Եİ�Χ���ʵ��
    �������FixedStack�������ԭ��֮һ����FixedStack�IJ�����
�����Ƕ��Enumerato r֮�����һ��������������һϵ�е���ջ��
��ջ�����ڶ�nextElement�����ε���֮�䷢��ʱ,����ֵ���ܺ��ϴ�
�ļ���ֵ�޹�,�������ܷ��ش��ڵ�ǰջβ֮���"����ֵ"������Ա
�����η�ֹ���־����������������,���߽������������ʹ����
���ĵ��������Ƿ�ֹ���־����һ�ַ���:
    public  class  FixedStack  {
     ...
     synchronized  public  void  push ( Object  item ) {
       array[top++] = item;
     }
     class  Enumerator  implements  java.util.Enumeration
      ...
      public  Object  nextElement( )  {
        synchronized ( FixedStack.this ) {
    if ( count > top )   count = top;
    if ( count == 0 )
     throw  new  NoSuchElementException ("Fix");
    return  array[- - count];
        }
      }
     }
    ���б��ʽFixedStack.thisָ���ǰ�Χʵ��
    �ֲ������
    ��һ���౻�����һ�����еľֲ���ʱ,����Զ�дͬһ���е�
һ����ʽ�������õ�һ����ơ�������һ������:
    Enumeration  myEnumerate ( final  Object  array[]) {
     class  E  implements  Enumeration   {
      int  count = 0;
      public  boolean  hasMoreElements( )
        {  return  count < array.length;  }
      public  Object  nextElement( )
        {  return  array[count++];  }
     }
     return  new  E( );
    }
    ��������δ�����,Java�����������ͱ�������ѧ����õ���
ȷ���֡������ڷ���my Enumerate����֮��,array���ܱ���Ƕ������
����,������C����������"�뿪"�ˡ��෴,���ֵ�����ڰ�����E��
�����������ڵ��κ���Ҫ�ĵط��������á�
    ע������ģʽ(final)����������array����ľֲ�����ģʽ����
��Java1.1��һ�����ص㡣ʵ����,���һ�����еľֲ�������ֲ���
��Ҫ����һ����(����Ƕ��)������,��ô����뱻����Ϊ����ģʽ��
����DZ�ڵ�ͬ������,û�а취ʵ�������������ͬһ�ɱ�ֲ���
���Ķ�д��״̬����count���ܱ���Ϊ�ֲ�����,��ǽ����ΪһԪ��
��,������ʾ:
    Enumeration  myEnumerate ( final  Object  array[])  {
     final  int  count[]= 0;  // final  reference
     class  E  implements  Enumeration  {
      public  boolean  hasMoreElements( )
        {  return  count[0] < array.length;  }
     }
      }
    �̳кʹʻ�������������ʱ���ܻ������Ի�����,�����E��
Enumeration�̳���һ�����array����,����򽫰Ѱ�Χ�������е�
ͬ������ڲص���Ϊ�˷�ֹ���ֶ�����,Java 1.1���?�̳е�����
�ص���Щ�ڰ�Χ����Χ���������ڶ����ͬ�����,���ǽ�ֹ����
�ڲ��߱���ʽ���ʸ�ʱ�����á�

    ������
    �������������,�ֲ�����E����û�л��߿���˵��ȫû�����Ӵ�
�������ȡ����ⲻ������̫��:һ���ٳ�������Ҳ����ʹά����ɨ
һ����������������е����ݡ�Ϊ�˹��쾡���ܼ��ķdz�С�Ľӿ�
��,Java1.1����ʹ�þֲ��������д��ʾ��һ�ּ򵥵ı��ʽ�ķ���
������Ķ����ʵ��ķ�������,������ʾ:
    Enumeration  myEnumerate ( final  Object  array[])  {
     return  new  Enumeration( )  {
       int  count = 0;
       public  boolean  hasMoreElements( )
    {  return  count < array.length;  }
       public  Object  nextElement( )
    {  return  array[count++];  }
     };
      }
    һ���˵,һ��new���ʽ(ʵ����ʽ)������������Ϊ����
������������ʹ����( ��ӿ�)��new��־֮������,���Ҹ�ݸ��
�����������������(��ʵ����)���������������Ƕ��ͬ����Ա��
��ǰ��������һ�����־ֲ����������ͬ������塣
    �������������뱣�ּ����Ա������Ƕ�׹�������ȷ
ʹ��,���DZ�����һ�����ֵľֲ���򶥲�ӿ��඼��������ά��
��
    ���һ�����������һ�������ϵĿ�ִ�д���,��ô�����˼��
���ܲ����Զ��׼��,���Ӧ�ø�����ͨ��һ���ֲ��������ʵ��
һ�������Եľֲ���ơ�
    ����������г�ʼ������,�������й��������ص�new���ʽ
�ı�Ԫ��(��Ϊ��)��ʽ�ش��ݸ���Ĺ������
    ����ǰ������ʾ��һ��,������������ɽӿ�I���������,��ô
��ʵ�ʵ��ϼ�����Obje ct,������ʵ��I�����չ֮(��ʽ��implemen
ts�Ӿ��ǷǷ���)�����ǽӿ�����ܺϷ��ط��ڹؼ���new�����Ψһ
�����������������,��Ԫ��������ǿյ�,�Ա���ʵ�ʵ��ϼ���Obje
 ct�Ĺ��������ƥ�䡣

--
�� ��Դ:���ϴ�С�ٺ���Ϣ����վ bbs.nju.edu.cn��[FROM: hsia.nju.edu.cn]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads