返回信息流Scala:
class News(val title: String, var content: String) {
private var _readCount = 0
// def title = ... // implied
// def content = ... // implied
// def content_= = ... // implied
def length = content.length
def read() { _readCount += 1 }
def readCount = _readCount
}
val n = News("Hello World", "How are you?")
n.content = "Fine. Thank you."
val len = n.length()
n.read()
printf("%s %s\n", len, n.readCount)
Lua:
function News(title, content)
local self = {}
local _readCount = 0
function self:getTitle() return title end
function self:getContent() return content end
function self:setContent(c) content = c end
function self:getLength() return string.len(content) end
function self:read() _readCount = _readCount + 1 end
function self:getReadCount() return _readCount end
return self
end
n = News("Hello World", "How are you?")
n:setContent("Fine. Thank you!")
len = n:getLength()
n:read()
print(len, n:getReadCount())
JavaScript
function News(title, content) {
var _readCount = 0
this.getTitle = function() { return title; }
this.getContent = function() { return content; }
this.setContent = function(c) { content = c; }
this.getLength = function() { return content.length; }
this.read = function() { _readCount++; }
this.getReadCount = function() { return _readCount; }
}
n = new News("Hello World", "How are you?");
n.setContent("Fine. Thank you!");
len = n.getLength();
n.read();
print(len, n.getReadCount());
Java
class News {
private String title;
private String content;
private int readCount = 0;
public News(String title, String content) {
this.title = title; this.content = content;
}
public String getTitle() { return title; }
public String getContent() { return content; }
public void setContent(String c) { content = c; }
public int getLength() { return content.getLength(); }
public void read() { readCount++; }
public int getReadCount() { return readCount; }
}
public static void main(String[] args) {
News n = new News("Hello world", "How are you?");
n.setContent("Fine. Thank you.");
int len = n.getLength();
n.read();
System.out.printf("%d %d\n", len, n.getReadCount());
}
这是一条镜像帖。来源:北邮人论坛 / java / #42940同步于 2015/7/21
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
Java,为什么你这么特殊?
nuanyangyang
2015/7/21镜像同步47 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
【 在 icyfox 的大作中提到: 】
: 暖神又在秀操作
最近在学Lua。Lua的书籍一直是个怨念,国内没有引进Programming in Lua第三版,但网上的免费的第一版是Lua 5.0的,到5.1、5.2、5.3每个版本都变好多,细节都要看manual。果然Roberto是个“不是很仁慈的独裁者”。
这是什么编辑器,谢谢
【 在 nuanyangyang 的大作中提到: 】
: Scala:
: [code=scala]
: class News(val title: String, var content: String) {
: ...................