Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changes from Version 1 of FinalExample

Show
Ignore:
Author:
jcc7 (IP: 68.97.93.38)
Timestamp:
11/12/05 00:03:33 (19 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • FinalExample

    v0 v1  
     1= final = 
     2 
     3''Part of'' KeywordsCategory 
     4 
     5== Description == 
     6 
     7Shows the usage of the final keyword on a class. 
     8 
     9== Example == 
     10 
     11{{{ 
     12#!d 
     13/*  
     14This example won't compile with either "final" compiled in:  
     15final.d(34): function toString cannot override final function toString   
     16*/  
     17 
     18 
     19/+final+/ class A 
     20{ 
     21    private char[] name; 
     22     
     23    /+final+/ char[] toString() 
     24    { 
     25        return name; 
     26    } 
     27     
     28    this(char[] s) 
     29    { 
     30        name = s; 
     31        printf("'%.*s' created\n", name); 
     32    } 
     33    ~this() 
     34    { 
     35        printf("'%.*s' destroyed\n", name); 
     36    } 
     37} 
     38 
     39 
     40class B: A /+ If class A is final this wouldn't work. +/ 
     41{ 
     42    char[] name; 
     43 
     44    char[] toString() /* If this method were final in class A, this would cause a compile error. */ 
     45    { 
     46        return name; 
     47    } 
     48 
     49    this(char[] s) 
     50    { 
     51        super(s); 
     52    } 
     53    ~this() 
     54    { 
     55        printf("'%.*s' destroyed\n", name); 
     56    } 
     57} 
     58 
     59 
     60void main() 
     61{     
     62} 
     63}}} 
     64 
     65== More Information == 
     66 
     67Apparently similar to [http://www.glenmccl.com/perfj_025.htm Java's final]. 
     68 
     69 
     70== Source == 
     71 
     72|| Link || http://www.dsource.org/tutorials/index.php?show_example=74 || 
     73|| Posted by || jcc7 || 
     74|| Date/Time || Wed May 19, 2004 6:01 pm ||