D语言property函数

import std.stdio;

struct Rectangle { 
   double width; 
   double height;  

   double area() const @property {  
      return width*height;  
   } 

   void area(double newArea) @property {  
      auto multiplier = newArea / area; 
      width *= multiplier; 
      writeln(Value set!);  
   } 
}

void main() { 
   auto rectangle = Rectangle(20,10); 
   writeln(The area is , rectangle.area);  

   rectangle.area(300); 
   writeln(Modified width is , rectangle.width); 
}

相关文章

// ------------------- import std.stdio; void main(str...
import std.stdio; int main () { /* local variable defi...
import std.stdio; int main(string[] args) { int a = 20...
import std.stdio; int main(string[] args) { int a = 4;...
import std.stdio; int main(string[] args) { int a = 21;...
import std.stdio; int main(string[] args) { uint a = 60...