D语言参数化构造函数

import std.stdio;

class Line { 
   public: 
      void setLength( double len ) { 
         length = len; 
      }
      double getLength() { 
         return length; 
      }
      this( double len) { 
         writeln("Object is being created, length = " , len ); 
         length = len; 
      } 

   private: 
      double length; 
} 

// Main function for the program 
void main( ) { 
   Line line = new Line(10.0);

   // get initially set length. 
   writeln("Length of line : ",line.getLength()); 

   // set line length again 
   line.setLength(6.0); 
   writeln("Length of line : ", line.getLength()); 
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/266054.html

(0)
上一篇 2022年6月7日
下一篇 2022年6月7日

相关推荐

发表回复

登录后才能评论