Dart语言类中的getter和setter方法

class Student { 
   String name; 
   int age; 

   String get stud_name { 
      return name; 
   } 

   void set stud_name(String name) { 
      this.name = name; 
   } 

   void set stud_age(int age) { 
      if(age<= 0) { 
        print("Age should be greater than 5"); 
      }  else { 
         this.age = age; 
      } 
   } 

   int get stud_age { 
      return age;     
   } 
}  
void main() { 
   Student s1 = new Student(); 
   s1.stud_name = 'MARK'; 
   s1.stud_age = 0; 
   print(s1.stud_name); 
   print(s1.stud_age); 
}

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

(0)
上一篇 2022年6月7日 22:02
下一篇 2022年6月7日 22:03

相关推荐

发表回复

登录后才能评论