因為之前是學Java,所以習慣物件導向,自然習慣了「寫程式,從怎麼寫物件開始。」 void main(){ var D = new Demo( 'Dart Tutorial' , 'Happy coding' , '2019/9/14' ); print (D.title); } class Demo { String title; //標題 String description; //資訊 String publishTime; //時間 Demo( String title, String description, String publishTime ) { this .title = title; this .description = description; this .publishTime = publishTime; } } 以上可以看到一個標準物件的結構。 一個物件只能有一個建構子。這是比較奇妙的地方。 void main() { var d1 = new Demo( 'Dart Tutorial' , 'Happy coding' , '2019/9/14 ); print (d1.title); //Output: Dart Tutorial var d2 = new Demo.only Description ( "Happy coding Only Description" ); print (d2.url); //Output: https://123.456 var d3 = new Demo.onlyTitle( "Dart Tutorial Only title" ); print (d3.title); //Output: Only title } class Demo { String _title; //標題 增加下引線表示這是private String _descripti...
留言
張貼留言