public class Enshu702 {
public static void main(String[] args) {
ProductInfopi = new ProductInfo();
try {
Informationi0 = pi.get(-1); // 正しくない商品番号
}
catch(IllegalArgumentExceptione) {
System.err.println(e.getMessage());
}
Informationi1 = pi.get(1);
System.out.println(i1);
Informationi2 = pi.get(3);
System.out.println(i2);
Informationi3 = pi.get(1);
System.out.println(i3);
}
}
本当に見ますか?
ProductInfo.java
public class ProductInfo {
private Productpd = null;
public ProductInfo() {}
public Information get(int id) {
if (id <= 0) {
throw new IllegalArgumentException("商品番号が正しくありません。");
}
if (pd == null)
pd = new Product(); // データベースへの接続
return pd.get(id);
}
}
public class ProductInfo
{
private Productpd = null;
public Information get(int id)
{
if (id <= 0)
throw new ArgumentException("商品番号が正しくありません。");
if (pd == null)
pd = new Product(); // データベースへの接続
return pd.get(id);
}
}
Enshu702.vb
Module Enshu702
Sub Main()
Dim pi As ProductInfo = New ProductInfo()
Try
Dim i0 As Information = pi.get(-1) ' 正しくない商品番号
Catch e As ArgumentExceptionConsole.Error.WriteLine(e.Message())
End Try
Dim i1 As Information = pi.get(1)
Console.WriteLine(i1.ToString)
Dim i2 As Information = pi.get(3)
Console.WriteLine(i2.ToString)
Dim i3 As Information = pi.get(1)
Console.WriteLine(i3.ToString)
End Sub
End Module
本当に見ますか?
ProductInfo.vb
Public Class ProductInfo
Dim pd As New Product = Nothing
Public Sub New()
End Sub
Public Function [get](id As Integer) As Information
If id <= 0 Then
Throw New ArgumentException("商品番号が正しくありません。")
End If
If IsNothing(pd) Then
pd = New Product() ' データベースへの接続
End If
Return pd.get(id)
End Function
End Class
Enshu702.js
const ProductInfo = require("./ProductInfo.js");
let pi = new ProductInfo();
try {
let i0 = pi.get(-1); // 正しくない商品番号
}
catch(e) {
process.stderr.write(e.message + "\n");
}
let i1 = pi.get(1);
process.stdout.write(i1 + "\n");
let i2 = pi.get(3);
process.stdout.write(i2 + "\n");
let i3 = pi.get(1);
process.stdout.write(i3 + "\n");
本当に見ますか?
ProductInfo.js
const Product = require("./Product.js");
module.exports = class ProductInfo {
constructor() {
this.pd = null;
}
get(id) {
if (id <= 0) {
throw new RangeError("商品番号が正しくありません。");
}
if (this.pd == null)
this.pd = new Product(); // データベースへの接続
return this.pd.get(id);
}
}
Enshu702.pl
use lib './';
use ProductInfo;
my $pi = new ProductInfo();
eval {
my $i0 = $pi->get(-1); # 正しくない商品番号
};
if ($@) {
my @m = split(/ +/, $@);
print $m[0] . "\n";
}
my $i1 = $pi->get(1);
print $i1->toString() . "\n";
my $i2 = $pi->get(3);
print $i2->toString() . "\n";
my $i3 = $pi->get(1);
print $i3->toString() . "\n";
本当に見ますか?
Product.pm
use Product;
package ProductInfo {
sub new {
my ($class) = @_;
my $this = { pd => 0 };
return bless $this, $class;
}
sub get {
my ($this, $id) = @_;
die "商品番号が正しくありません。" if ($id <= 0);
if ($this->{pd} == null) {
$this->{pd} = new Product(); # データベースへの接続
}
return $this->{pd}->get($id);
}
}
1;
Enshu702.rb
require './ProductInfo'
pi = ProductInfo.new()
begin
i0 = pi.get(-1)
rescue => e
STDERR.puts e.message
end
i1 = pi.get(1)
puts i1i2 = pi.get(3)
puts i2i3 = pi.get(1)
puts i3
本当に見ますか?
ProductInfo.rb
require './Product'
class ProductInfo
def get(id)
if id < 0
raise "商品番号が正しくありません。"
end
if @pd.nil?
@pd = Product.new()
end
return @pd.get(id)
end
end
Enshu702.py
from ProductInfo import ProductInfo
pi = ProductInfo()
try:
i0 = pi.get(-1) # 正しくない商品番号
except Exception as e:
print(e)
i1 = pi.get(1)
print(i1.toString())
i2 = pi.get(3)
print(i2.toString())
i3 = pi.get(1)
print(i3.toString())
本当に見ますか?
Product.py
from Product import Product
class ProductInfo:
def __init__(self):
self.pd = None
def get(self, id):
if (id < 0):
raise Exception("商品番号が正しくありません。")
if (self.pd == None):
self.pd = Product() # データベースへの接続
return self.pd.get(id)
<?php
require_once('Product.php');
class ProductInfo {
private $pd;
public function __construct() {
$this->pd = null;
}
public function get($id) {
if ($id < 0)
throw new Exception("商品番号が正しくありません。\n");
if ($this->pd == null)
$this->pd = new Product(); // データベースへの接続
return $this->pd->get($id);
}
}
?>
Enshu702.ts
import {ProductInfo} from "./ProductInfo";
import {Information} from "./Information";
let pi:ProductInfo = new ProductInfo();
try {
let i0:Information = pi.get(-1); // 正しくない商品番号
}
catch(e) {
process.stderr.write(e.getMessage() + "\n");
}
let i1:Information = pi.get(1);
process.stdout.write(i1 + "\n");
let i2:Information = pi.get(3);
process.stdout.write(i2 + "\n");
let i3:Information = pi.get(1);
process.stdout.write(i3 + "\n");
本当に見ますか?
ProductInfo.ts
import {Information} from "./Information";
import {Product} from "./Product";
export
class ProductInfo {
private pd:Product = null;
public get(id:number):Information {
if (id <= 0) {
throw new RangeError("商品番号が正しくありません。");
}
if (this.pd == null)
this.pd = new Product(); // データベースへの接続
return this.pd.get(id);
}
}
Enshu702.swift
import Foundation
let pi:ProductInfo = ProductInfo()
try {
let _ = pi.get(-1) // 正しくない商品番号
}
catch let error {
let nsError = error as NSError
print(nsError.domain)
}
let i1:Information = try! pi.get(1)
print(i1.toString())
let i2:Information = try! pi.get(3)
print(i2.toString())
let i3:Information = try! pi.get(1)
print(i3.toString())
本当に見ますか?
ProductInfo.swift
import Foundation
public class ProductInfo {
private var pd:Product!
public func get(id:Int) -> Information {
if id <= 0 {
throw NSError(domain: "商品番号が正しくありません。", code: -1, userInfo: nil)
}
if pd == nil {
pd = Product() // データベースへの接続
}
return pd.get(id)
}
}
Enshu702.kt
fun main() {
val pi = ProductInfo()
try {
val i0 = pi[-1] // 正しくない商品番号
}
catch(e: IllegalArgumentException) {
System.err.println(e.message)
}
val i1 = pi[1]
println(i1)
val i2 = pi[3]
println(i2)
val i3 = pi[1]
println(i3)
}
本当に見ますか?
ProductInfo.kt
internal class ProductInfo {
private var pd: Product? = null
operator fun get(id: Int) : Information {
require(id <= 0) { "商品番号が正しくありません。" }
if (pd == null) pd = Product() // データベースへの接続
return pd!![id]
}
}
Enshu702.scala
object Main702 {
def main(args: Array[String])
val pi = new ProductInfo()
try {
val i0: Information = pi.get(-1) // 正しくない商品番号
}
catch {
case e: IllegalArgumentException => System.err.println(e.message)
}
val i1: Information = pi.get(1)
System.out.println(i1)
val i2: Information = pi.get(3)
System.out.println(i2)
val i3: Information = pi.get(1)
System.out.println(i3)
}
}
本当に見ますか?
ProductInfo.scala
class ProductInfo {
private var pd: Product = null
def get(id: Int) : Information = {
if (id <= 0) throw new IllegalArgumentException("商品番号が正しくありません。")
if (pd == null) pd = new Product() // データベースへの接続
pd.get(id)
}
}
import product;
import information;
public class ProductInfo {
private Productpd = null;
public Information get(in int id) {
if (id <= 0) {
throw new Exception("商品番号が正しくありません。");
}
if (pd is null)
pd = new Product(); // データベースへの接続
return pd.get(id);
}
}
Enshu702.dpr
program Enshu702;
uses
System.SysUtils,
UnitProductInfo,
UnitInformation;
var pi:ProductInfo;
var i0, i1, i2, i3:Information;
begin
pi := ProductInfo.Create();
try
i0 := pi.get(-1); // 正しくない商品番号
except
on E: Exception do Writeln(E.ClassName, ': ', E.Message);
end;
i1 := pi.get(1);
Writeln(i1.ToString());
i2 := pi.get(3);
Writeln(i2.ToString());
i3 := pi.get(1);
Writeln(i3.ToString());
pi.Free;
end.
本当に見ますか?
ProductInfo.pas
unit UnitProductInfo;
interface
uses
System.SysUtils,
UnitProduct,
UnitInformation;
type
ProductInfo = class
private
var pd:Product;
public
function get(id:integer):Information;
end;
implementation
function ProductInfo.get(id:integer):Information;
begin
if id <= 0 then begin
raise Exception.Create('商品番号が正しくありません。');
end;
if pd = nil then begin
pd := Product.Create(); // データベースへの接続
end;
Result := pd.get(id);
end;
end.