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 ClassExample/D2

Show
Ignore:
Author:
Andrej08 (IP: 78.2.57.183)
Timestamp:
09/05/10 16:23:07 (14 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ClassExample/D2

    v0 v1  
     1= Class = 
     2 
     3''Part of'' TutorialFundamentals 
     4 
     5== Description == 
     6 
     7A class is a similiar idea to a struct, but allows more abstract designs. 
     8 
     9== Example == 
     10 
     11{{{ 
     12#!d 
     13import std.stdio; 
     14 
     15class Foo 
     16{ 
     17    /* #1 */ 
     18    int number; 
     19    string name; 
     20     
     21    /* #2 */ 
     22    this(int number, string aname) 
     23    { 
     24        /* #3 */ 
     25        this.number = number; 
     26        name = aname; 
     27    } 
     28     
     29    void printMe() 
     30    { 
     31        /* #4 */ 
     32        string name = "I'm hiding access to Foo.name!"; 
     33        writefln("number: %s, name: %s, internal name: %s", number, this.name, name); 
     34    } 
     35} 
     36 
     37class Bar 
     38{ 
     39    int x; 
     40    int y; 
     41     
     42    /* #5 */ 
     43    this() 
     44    { 
     45        x = 5; 
     46        y = 6; 
     47    } 
     48     
     49    void printMe() 
     50    { 
     51        writefln("x: %s, y: %s", x, y); 
     52    } 
     53} 
     54 
     55 
     56void main()  
     57{      
     58    /* #6 */ 
     59    Foo foo = new Foo(4, "hello"); 
     60    foo.printMe(); 
     61     
     62    /* #7 */ 
     63    foo.number = 1; 
     64    foo.name = "world!"; 
     65    foo.printMe(); 
     66     
     67    /* #8 */ 
     68    Bar bar = new Bar; 
     69    bar.printMe(); 
     70} 
     71 
     72}}} 
     73 
     74== Explanation == 
     75 
     761: A class can have it's own variables.  
     77 
     782: A class can have a user-defined constructor called '''this()'''. It gets called when we create an instance of the class. A constructor can optionally parameters. 
     79 
     803: If the parameter in a method of a class has the same name as a variable in the same class, you can use '''this''' to explicitly refer to the class instance, and use the dot-notation to access its variables. This is what we do when we use the first parameter to assign to our variable, while the second paremeter doesn't conflict with any names so we don't have to use '''this''' in this case. 
     81 
     824: A class can have its own methods. Again, you don't necessarily have to use '''this''' to refer to the class variables, unless there are any paramters or variables defined inside the method that have conflicting names with the class variables. Note how ''string name'' is hiding access to ''name'' of the Foo instance, but we can use '''this''' to work around this issue. 
     83 
     845: We can make a constructor that doesn't take any arguments. We could also provide multiple constructors, but that's not shown here. 
     85 
     866. A class instance is made by using the '''new''' keyword, following by a call to the constructor. The constructor is called by using the class name ( '''Foo''' ), followed by arguments in parantheses if a constructor requires any. We can then call the methods of the class instance. 
     87 
     887. Foo's variables are public by default. This means that you can read and modify them directly by using dot-notation. In some cases, this is desirable. But most often you would protect the variables from being easily modified outside the class by using access specifiers (More info on this is in the docs). 
     89 
     908. Since Bar's constructor doesn't take any arguments, we don't need to add the parantheses when calling its constructor. 
     91 
     92'''Note:''' In D1 code (and some D2 code), you might notice the use of the '''delete''' expression with class instances.  
     93The delete expression is scheduled for deprecation, and you should not use it with new code. 
     94 
     95== More Information == 
     96 
     97See the [http://www.digitalmars.com/d/2.0/class.html D Specification].