Dart语言教程:对象

2019-06-2215:23:05编程语言入门到精通Comments2,199 views字数 996阅读模式

面向对象编程将对象定义为“具有已定义边界的任何实体”。对象具有以下内容 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13758.html

  • 状态 - 描述对象,类的字段表示对象的状态。
  • 行为 - 描述对象可以执行的操作。
  • 标识 - 将对象与一组类似的其他对象区分开的唯一值。两个或多个对象可以共享状态和行为,但不能共享身份。

句点运算符(.)与对象一起使用以访问类的数据成员。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13758.html

示例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13758.html

Dart以对象的形式表示数据,Dart中的每个类都扩展了Object类。下面给出了一个创建和使用对象的简单示例。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13758.html

class Student { 
   void test_method() { 
      print("This is a  test method"); 
   } 

   void test_method1() { 
      print("This is a  test method1"); 
   } 
}  
void main()    { 
   Student s1 = new Student(); 
   s1.test_method(); 
   s1.test_method1(); 
}
Dart

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13758.html

This is a test method 
This is a test method1
Shell

级联运算符(..)

上面的示例调用了类中的方法。但是,每次调用函数时都需要引用该对象。在存在一系列调用的情况下,级联运算符可用作速记。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13758.html

级联(..)运算符可用于通过对象发出一系列调用。上述示例代码可以按以下方式重写。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13758.html

class Student { 
   void test_method() { 
      print("This is a  test method"); 
   } 

   void test_method1() { 
      print("This is a  test method1"); 
   } 
}  
void main() { 
   new Student() 
   ..test_method() 
   ..test_method1(); 
}
Dart

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13758.html

This is a test method 
This is a test method1
Shell

toString()方法

此函数返回对象的字符串表示形式。请查看以下示例以了解如何使用toString方法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13758.html

void main() { 
   int n = 212; 
   print(n.toString()); 
}
Dart

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13758.html

212

原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/dart/dart_programming_object.html#article-start文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13758.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13758.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/13758.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定