ballerina 学习七 object 创建&& 初始化
在 ballerina 总中object 是一个包含public private 类型字段同时包含函数,需要开发人员进行自定义类型以及行为
说白了,就是类似面向对象的class
基本使用
- 代码
import ballerina/http;
import ballerina/io;
type App object {
public {
int age,
string name,
}
private {
string email = "default@abc.com",
}
};
function main (string… args) {
App app =new ;
App app2 =new();
App app3=new App();
io:println(app);
io:println(app2);
io:println(app3);
}
- 输出结果
{age:0, name:""}
{age:0, name:""}
{age:0, name:""}
object 初始化
- 代码
import ballerina/http;
import ballerina/io;
type App object {
public {
int age,
string name,
}
private {
string email = "default@abc.com",
int[] marks,
}
new(age, name = "John", string firstname,
string lastname = "Doe", int… scores) {
marks = scores;
}
};
function main (string… args) {
App app1 =new (5,"demoapp",4,5);
io:println(app1);
App app2 = new(5, "Adam", name = "Adam", lastname = "Page", 3);
io:println(app1);
}
- 输出结果
{age:5, name:"John"}
{age:5, name:"John"}
成员方法
import ballerina/http;
import ballerina/io;
type App object {
public {
int age,
string name,
}
private {
string email = "default@abc.com",
int[] marks,
}
new(age, name = "John", string firstname,
string lastname = "Doe", int… scores) {
marks = scores;
}
function getFullName() returns string {
return age + " " + name;
}
// 抽象方法
function checkAndModifyAge(int condition, int age);
};
// 实现抽象方法
function App::checkAndModifyAge(int condition, int age){
io:println("demo");
}
function main (string… args) {
App app1 =new (5,"demoapp",4,5);
io:println(app1);
App app2 = new(5, "Adam", name = "Adam", lastname = "Page", 3);
io:println(app1);
app1.checkAndModifyAge(1,3);
}
- 输出结果
{age:5, name:"John"}
{age:5, name:"John"}
demo
对象赋值
如果两个对象的结构相同,那么就可以进行赋值操作
import ballerina/http;
import ballerina/io;
public type Person object {
public {
int age,
string name,
}
public function getName() returns string {
return name;
}
};
public type Employee object {
public {
int age,
string name,
string address,
}
public new(age, name, address) {
}
public function getName() returns string {
return name + " Doe";
}
public function getAge() returns int {
return age;
}
};
function main (string… args) {
Person p1 = new Employee(50, "John", "street1");
io:println(p1);
io:println(p1.getName());
}
- 输出结果
{age:50, name:"John", address:"street1"}
John Doe
参考资料
https://ballerina.io/learn/by-example/object-initializer.html
https://ballerina.io/learn/by-example/object-assignability.html
https://ballerina.io/learn/by-example/object-member-functions.html
ballerina 学习七 object 创建&& 初始化的更多相关文章
- android学习七(创建自己定义控件)
前面学习的是android的基本控件和布局的使用,可是主要的控件和布局有时候并不能实现复杂的布局.我们来看下各种控件和布局的关系. 可见全部的控件都是直接或者间接的继承自View的,全部的布局都是直接 ...
- 我的Go语言学习之旅七:创建一个GUI窗口
在上次中,刚刚学过了 弹窗效果.这里再接着学习一下怎样创建一个窗口. 还是老路子,先上代码: package main import ( "github.com/lxn/go-winapi ...
- 七天学会ASP.NET MVC(七)——创建单页应用
系列文章 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC 七天学会ASP.NET MVC (二)——ASP.NET MVC 数据传递 七天学会ASP.NET MVC (三)— ...
- 跟着刚哥学习Spring框架--创建HelloWorld项目(一)
1.Spring框架简介 Spring是一个开源框架,Spring是在2003年兴起的一个轻量级的开源框架,由Rod johnson创建.主要对JavaBean的生命周期进行管理的轻量级框架,Spri ...
- 七天学会ASP.NET MVC(七)——创建单页应用 【转】
http://www.cnblogs.com/powertoolsteam/p/MVC_Seven.html 系列文章 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC 七天学 ...
- Java虚拟机JVM学习04 类的初始化
Java虚拟机JVM学习04 类的初始化 类的初始化 在初始化阶段,Java虚拟机执行类的初始化语句,为类的静态变量赋予初始值. 在程序中,静态变量的初始化有两种途径: 1.在静态变量的声明处进行初始 ...
- MyBatis学习七:spring和MyBatis整合
<\mybatis\day02\16mybatis和spring整合-sqlSessionFactory配置.avi;> MyBatis学习七:spring和MyBatis整合.逆向工程 ...
- Javascript学习一Object
构造函数 new Object() new Object(value) 参数 value 可选的参数,声明了要转换成Number对象.Boolean对象或String对象的原始值(即数字.布尔 ...
- (转)MyBatis框架的学习(七)——MyBatis逆向工程自动生成代码
http://blog.csdn.net/yerenyuan_pku/article/details/71909325 什么是逆向工程 MyBatis的一个主要的特点就是需要程序员自己编写sql,那么 ...
随机推荐
- SpringBoot下的值注入
在我们实际开发项目中,经常会遇到一些常量的配置,比如url,暂时不会改变的字段参数,这个时候我们最好是不要直接写死在代码里的,因为这样编写的程序,应用扩展性太差了,我们可以直接写在配置文件中然后通过配 ...
- for each/in/of的解释and example
for-of 循环:代码示例for (var value of myArray) {console.log(value);}循环的对象需为一个数组 无法记录索引 可以相应break.continue. ...
- JavaScript中Function Declaration与Function Expression 或者说 function fn(){}和var fn=function(){} 的区别
JavaScript是一种解释型语言,函数声明会在JavaScript代码加载后.执行前被解释,而函数表达式只有在执行到这一行代码时才会被解释. 在JS中有两种定义函数的方式, 1是:var aaa= ...
- jquery extend源码解析
$.extend(obj1,0bj2,{"name":"s","age":22}) //target 要拷贝到哪个对象上 // i 要执行拷 ...
- Hibernate所用15个jar包
Hbernate3.jar-------------------核心包antlr.jar----------------------------语言转换工具,hibernate用他将hql语句转换为s ...
- Ansible 小手册系列 二十(经常遇到的问题)
(1). 怎么为任务设置环境变量? - name: set environment shell: echo $PATH $SOME >> /tmp/a.txt environment: P ...
- 清华大学 pip 源
pypi 镜像使用帮助 pypi 镜像每 5 分钟同步一次. 临时使用 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-pac ...
- Web字体(链接)嵌入
下面是我最近在学习的两种字体嵌入方法 1.@font-face 使用@font-face可以这样做: @font-face{ font-family:"Garamod Premier Pro ...
- GPON命令模式
1.添加ont步骤 1.1 查看自动发现的ONT,并记录SN号和PON口 MA5680T(config)#display ont autofind all --------------------- ...
- tcping的安装和使用
1.LINUX安装方法: 编译安装下载地址: http://linuxco.de/tcping/tcping.html tar zxvf tcping-1.3.5.tar.gz cd tcping-1 ...