public Information clone()
{
Informationinfo = (Information)MemberwiseClone();
//
// オブジェクト型のフィールドのコピー
//
return info;
}
本当に見ますか?
Information.cs
public class Information
{
private string name; // 製品名
private string district; // 産地
private int retailPrice; // 小売価格
private int purchasePrice; // 買い付け価格
private ArrayListcomment; // コメント(複数持てる)
public Information(string n, string d, int p1, int p2)
{
name = n;
district = d;
retailPrice = p1;
purchasePrice = p2;
}
public override string ToString()
{
string s = "製品名:" + name + "、小売価格:" + retailPrice;
if (comment != null)
{
if (comment.Count > 0)
{
s += "、コメント:";
foreach (string c in comment)
s += "[" + c + "]";
}
}
return s;
}
public void setRetailPrice(int p)
{
retailPrice = p;
}
public void setComment(string c)
{
if (comment == null)
comment = new ArrayList();
comment.Add(c);
}
public Information clone()
{
Informationinfo = (Information)MemberwiseClone();
if (comment != null)
info.comment = new ArrayList(comment); // オブジェクト型のフィールドのコピー
return info;
}
}
Enshu703.vb
Module Enshu703
Sub Main()
Dim pi As ProductInfo = New ProductInfo()
Dim i1 As Information = pi.get(1)
Dim i2 As Information = pi.get(3)
Dim i3 As Information = pi.get(1)
i1.setComment("新製品")
i3 = i3.clone() ' 複製
i3.setRetailPrice(150) ' 傷物なので、150円に値下げ
i3.setComment("傷物")
Console.WriteLine(i1.ToString)
Console.WriteLine(i2.ToString)
Console.WriteLine(i3.ToString)
End Sub
End Module
Public Function clone() As Information
Dim info As Information = MemberwiseClone()
'
' オブジェクト型のフィールドのコピー
'
Return info
End Function
本当に見ますか?
Information.vb
Public Class Information
Private name As String ' 製品名
Private district As String ' 産地
Private retailPrice As Integer ' 小売価格
Private purchasePrice As Integer ' 買い付け価格
Private comment As ArrayList ' コメント(複数持てる)
Public Sub New()
End Sub
Public Sub New(n As String, d As String, p1 As Integer, p2 As Integer)
name = ndistrict = dretailPrice = p1purchasePrice = p2
End Sub
Public Overrides Function ToString() As String
Dim s As String = "製品名:" & name & "、小売価格:" & retailPrice
If Not IsNothing(comment) Then
If comment.Count <> 0 Then
s &= "、コメント:"
For Each c In comments &= "[" & c & "]"
Next
End If
End If
Return s
End Function
Public Sub setRetailPrice(p As Integer)
retailPrice = p
End Sub
Public Sub setComment(c As String)
If IsNothing(comment) Then
comment = New ArrayList()
End If
comment.Add(c)
End Sub
Public Function clone() As Information
Dim info As Information = MemberwiseClone()
If Not IsNothing(comment) Then
info.comment = New ArrayList(comment) ' オブジェクト型のフィールドのコピー
End If
Return info
End Function
End Class
Enshu703.js
const ProductInfo = require("./ProductInfo.js");
let pi = new ProductInfo();
let i1 = pi.get(1);
let i2 = pi.get(3);
let i3 = pi.get(1);
i1.setComment("新製品");
i3 = i3.clone(); // 複製
i3.setRetailPrice(150); // 傷物なので、150円に値下げ
i3.setComment("傷物");
process.stdout.write(i1 + "\n");
process.stdout.write(i2 + "\n");
process.stdout.write(i3 + "\n");
class Information
def initialize(name, district, rPrice, pPrice)
@name = name # 製品名
@district = district # 産地
@retailPrice = rPrice # 小売価格
@purchasePrice = pPrice # 買い付け価格
@comment = [] # コメント(複数持てる)
end
def to_s()
s = "製品名 #{@name}、小売価格:#{@retailPrice}"
if !@comment.empty?
s += "、コメント:"
for item in @comments += "[#{item}]"
end
end
return s
end
def setRetailPrice(price)
@retailPrice = price
end
def setComment(comment)
@comment.push(comment)
end
def clone()
return Marshal.load(Marshal.dump(self))
end
end
public override fun clone(): Information {
val info = super.clone() as Information
//
// オブジェクト型のフィールドのコピー
//
return info
}
本当に見ますか?
Information.kt
import java.util.Vector
class Information(val name: String, val district: String, var retailPrice: Int, var purchasePrice: Int) {
private var comment: Vector<string> = Vector() // コメント(複数持てる)
override fun toString() : String {
val s = StringBuffer()
s.append("製品名:${name}、小売価格:${retailPrice}")
if (comment != null) {
if (!comment!!.isEmpty()) {
s.append("、コメント:")
for (c in comment) s.append("[$c]")
}
}
return s.toString()
}
fun setComment(c: String) {
if (comment == null) comment = Vector()
comment!!.add(c)
}
public override fun clone(): Information {
val info = super.clone() as Information
if (comment != null) info.comment = Vector(comment)
return info
}
}
Enshu703.scala
object Main703 {
def main(args: Array[String]) {
val pi = new ProductInfo()
val i1: Information = pi(1)
val i2: Information = pi(3)
var i3: Information = pi(1)
i1.setComment("新製品")
try i3 = i3.clone() // 複製
catch {
case e: CloneNotSupportedException => System.err.println(e.message)
}
i3.setRetailPrice(150) // 傷物なので、150円に値下げ
i3.setComment("傷物")
System.out.println(i1) // i1 の内容は変わらない
System.out.println(i2)
System.out.println(i3) // i3 の内容は変わる
}
}
override def clone(): Information = {
val clone = super.clone().asInstanceOf[Information]
//
// オブジェクト型のフィールドのコピー
//
clone
}
本当に見ますか?
Information.scala
import scala.collection.mutable.ListBuffer
class Information(val name: String, val district: String, var retailPrice: Int, var purchasePrice: Int) extends Cloneable {
private var comment = new ListBuffer[String]() // コメント(複数持てる)
override def toString() : String = {
val s = new StringBuffer()
s.append("製品名:" + name + "、小売価格:" + retailPrice)
if (comment != null) {
if (!comment.isEmpty) {
s.append("、コメント:")
for (c <- comment) s.append("[" + c + "]")
}
}
s.toString()
}
def setRetailPrice(p: Int) {
retailPrice = p
}
def setComment(c: String) {
if (comment == null) comment = new ListBuffer[String]()
comment.addOne(c)
}
override def clone(): Information = {
val info = super.clone().asInstanceOf[Information]
if (comment != null) {
info.comment = new ListBuffer[String]()
for (c <- comment)
info.comment.addOne(c)
}
info
}
}
import std.conv;
public class Information {
//
// その他のメソッド
//
public this(Information info) {
//
// フィールドのコピー
//
}
public Information clone() {
return new Information(this);
}
}
本当に見ますか?
information.d
import std.conv;
public class Information {
private string name; // 製品名
private string district; // 産地
private int retailPrice; // 小売価格
private int purchasePrice; // 買い付け価格
private string[] comment; // コメント(複数持てる)
public this(in string n, in string d, in int p1, in int p2) {
name = n;
district = d;
retailPrice = p1;
purchasePrice = p2;
}
public this(Informationinfo) {
this.name = info.name;
this.district = info.district;
this.retailPrice = info.retailPrice;
this.purchasePrice = info.purchasePrice;
this.comment = info.comment.dup;
}
public override string toString() {
string s = "";
s ~= "製品名:" ~ name ~ "、" ~ "小売価格:" ~ text(retailPrice);
if (comment.length > 0) {
s ~= "、コメント:";
foreach(string c ; comment)
s ~= "[" ~ c ~ "]";
}
return s;
}
public void setRetailPrice(in int p) {
retailPrice = p;
}
public void setComment(in string c) {
comment ~= c;
}
public Information clone() {
return new Information(this);
}
}
Enshu703.pas
program Enshu703;
uses
System.SysUtils,
UnitProductInfo,
UnitInformation;
var pi:ProductInfo ;
var i1, i2, i3:Information;
begin
pi := ProductInfo.Create();
i1 := pi.get(1);
i2 := pi.get(3);
i3 := pi.get(1);
i1.setComment('新製品');
i3 := i3.clone(); // 複製
i3.setRetailPrice(150); // 傷物なので、150円に値下げ
i3.setComment('傷物');
Writeln(i1.toString()); // i1 の内容は変わらない
Writeln(i2.toString());
Writeln(i3.toString()); // i3 の内容は変わる
end.
implementation
//
// その他のメソッド
//
constructor Information.Create(info:Information);
begin
//
// フィールドのコピー
//
end;
// 途中略
function Information.clone():Information;
begin
Result := Information.Create(self);
end;
end.
本当に見ますか?
Information.pas
unit UnitInformation;
interface
uses
System.Generics.Collections,
System.SysUtils;
type
Information = class
private
var name:string; // 製品名
var district:string; // 産地
var retailPrice:integer; // 小売価格
var purchasePrice:integer; // 買い付け価格
var comment:TList<string>;
public
constructor Create(n:string; d:string; p1:integer; p2:integer); overload;
constructor Create(info:Information); overload;
destructor Destroy(); override;
function ToString():string; override;
procedure setRetailPrice(p:integer);
procedure setComment(c:string);
function clone():Information;
end;
implementation
constructor Information.Create(n:string; d:string; p1:integer; p2:integer);
begin
name := n;
district := d;
retailPrice := p1;
purchasePrice := p2;
end;
constructor Information.Create(info:Information);
var com:string;
begin
self.name := info.name;
self.district := info.district;
self.retailPrice := info.retailPrice;
self.purchasePrice := info.purchasePrice;
if info.comment <> nil then begin
self.comment := TList<string>.Create();
for com in info.comment do begin
self.comment.Add(com);
end;
end;
end;
destructor Information.Destroy();
begin
if comment <> nil then begin
comment.Free;
end;
end;
function Information.ToString():string;
var s:string;
var com:string;
begin
s := '製品名:' + name + '、小売価格:' + IntToStr(retailPrice);
if comment <> nil then begin
if comment.Count > 0 then begin
s := s + '、コメント:';
for com in comment do begin
s := s + '[' + com + ']';
end;
end;
end;
Result := s;
end;
procedure Information.setRetailPrice(p:integer);
begin
retailPrice := p;
end;
procedure Information.setComment(c:string);
begin
if comment = nil then begin
comment := TList<string>.Create();
end;
comment.Add(c);
end;
function Information.clone():Information;
begin
Result := Information.Create(self);
end;
end.