適当に書いたAnimalクラスを継承して適当に多言語に移植したサンプル。

オンラインコンパイラ: HotSoupProcessor C# VisualBasic JavaScript Python CommonLisp


HSP

#runtime "hsp3cl"

#cmpopt varinit 1

#module Animal _name,virtualBark,isGrow
#define super(%1) \
_name=%1 :\
virtualBark=*overrideBark

#modcfunc name
return _name
#modcfunc bark
gosub virtualBark
return refstr
#global

#module Dog _name,virtualBark
#modinit str __name
super@Animal __name
return

*overrideBark
return "ワオーン"
#global

#module Cat _name,virtualBark
#modinit str __name
super@Animal __name
return

*overrideBark
return "ニャーン"
#global

#module Chick _name,virtualBark,isGrow
#modinit str __name
super@Animal __name
isGrow=0
return

#modfunc growing
isGrow=1
return

*overrideBark
if isGrow@Animal: return "コケコッコー": else: return "ピヨピヨ"
#global

#module Program
#deffunc main
dimtype animals,5
newmod animals,Dog,"ポチ"
newmod animals,Chick,"しゃくれ"
growing animals(length(animals)-1)
newmod animals,Dog,"ジョン"
newmod animals,Cat,"タマ"
newmod animals,Chick,"コッコ"
newmod animals,Cat,"チビ"

foreach animals
mes strf("%s > %s",name(animals.cnt),bark(animals.cnt))
loop
return
#global
main



C#

using System;

using System.Collections.Generic;

class Animal{
public readonly string name;
public virtual string bark{get;}

public Animal(string name){
this.name=name;
}
}

class Dog: Animal{
public Dog(string name):base(name){}

public override string bark{get{
return "ワオーン";
}}
}

class Cat: Animal{
public Cat(string name):base(name){}

public override string bark{get{
return "ニャーン";
}}
}

class Chick: Animal{
bool isGrow=false;
public Chick(string name):base(name){}

public void growing(){
isGrow=true;
}

public override string bark{get{
return isGrow? "コケコッコー": "ピヨピヨ";
}}
}

class Program{
static void Main(){
var animals=new List<Animal>();
animals.Add(new Dog("ポチ"));
animals.Add(new Chick("しゃくれ"));
((Chick)animals[animals.Count-1]).growing();
animals.Add(new Dog("ジョン"));
animals.Add(new Cat("タマ"));
animals.Add(new Chick("コッコ"));
animals.Add(new Cat("チビ"));

foreach(var v in animals){
Console.WriteLine("{0} > {1}",v.name,v.bark);
}
}
}



VB

Option Strict On

Imports System
Imports System.Collections.Generic

Class Animal
Public ReadOnly name As String
Overridable ReadOnly Property bark As String

Sub New(name As String)
Me.name=name
End Sub
End Class

Class Dog
Inherits Animal
Sub New(name As String)
MyBase.New(name)
End Sub

Overrides ReadOnly Property bark As String
Get
Return "ワオーン"
End Get
End Property
End Class

Class Cat
Inherits Animal
Sub New(name As String)
MyBase.New(name)
End Sub

Overrides ReadOnly Property bark As String
Get
Return "ニャーン"
End Get
End Property
End Class

Class Chick
Inherits Animal
Dim isGrow as Boolean=False

Sub New(name As String)
MyBase.New(name)
End Sub

Sub growing
isGrow=True
End Sub

Overrides ReadOnly Property bark As String
Get
Return if(isGrow,"コケコッコー","ピヨピヨ")
End Get
End Property
End Class

Module Program
Sub Main
Dim animals As New List(Of Animal)
animals.Add(New Dog("ポチ"))
animals.Add(New Chick("しゃくれ"))
CType(animals(animals.Count-1),Chick).growing
animals.Add(New Dog("ジョン"))
animals.Add(New Cat("タマ"))
animals.Add(New Chick("コッコ"))
animals.Add(New Cat("チビ"))

For Each v In animals
Console.WriteLine("{0} > {1}",v.name,v.bark)
Next
End Sub
End Module



JavaScript




Python

from typing import List

class Animal:
@property
def name(self): return self.__name

def __init__(self,name:str):
self.__name=name

class Dog(Animal):
def __init__(self,name:str):
super().__init__(name)

@property
def bark(self):
return "ワオーン"

class Cat(Animal):
def __init__(self,name:str):
super().__init__(name)

@property
def bark(self):
return "ニャーン"

class Chick(Animal):
def __init__(self,name:str):
super().__init__(name)
self.__isGrow=False

def growing(self):
self.__isGrow=True

@property
def bark(self):
return "コケコッコー" if self.__isGrow else "ピヨピヨ"

if __name__=="__main__":
animals:List[Animal]=[]
animals.append(Dog("ポチ"))
animals.append(Chick("しゃくれ"))
animals[len(animals)-1].growing()
animals.append(Dog("ジョン"))
animals.append(Cat("タマ"))
animals.append(Chick("コッコ"))
animals.append(Cat("チビ"))

for v in animals:
print("{0} > {1}".format(v.name,v.bark))



CommonLisp

(defclass Animal()(

(name :reader name
:initarg :name)
))

(defclass Dog(Animal)())

(defmethod vark((self Dog))
"ワオーン"
)

(defclass Cat(Animal)())

(defmethod vark((self Cat))
"ニャーン"
)

(defclass Chick(Animal)(
(isGrow :initform nil)
))

(defmethod growing((self Chick))
(setf (slot-value self 'isGrow) t)
)

(defmethod vark((self Chick))
(if(slot-value self 'isGrow) "コケコッコー" "ピヨピヨ")
)

(let ((animals '()))
(push (make-instance 'Dog :name "ポチ") animals)
(push (make-instance 'Chick :name "しゃくれ") animals)
(growing (car animals))
(push (make-instance 'Dog :name "ジョン") animals)
(push (make-instance 'Cat :name "タマ") animals)
(push (make-instance 'Chick :name "コッコ") animals)
(push (make-instance 'Cat :name "チビ") animals)

(loop for v in (reverse animals) do
(format t "~a > ~a~%" (name v) (vark v))
)
)
(quit)



C++

#include <stdio.h>

#include <string>
#include <vector>
#include <deque>

class Animal{
private:
std::string _name;
public:
std::string name(){return _name;}
virtual std::string bark(){return "";}

Animal(std::string name){
this->_name=name;
}
};

class Dog:public Animal{
public:
Dog(std::string name):Animal(name){}

virtual std::string bark(){
return "ワオーン";
}
};

class Cat:public Animal{
public:
Cat(std::string name):Animal(name){}

virtual std::string bark(){
return "ニャーン";
}
};

class Chick:public Animal{
private:
bool isGrow=false;
public:
Chick(std::string name):Animal(name){}

void growing(){
isGrow=true;
}

virtual std::string bark(){
return isGrow? "コケコッコー": "ピヨピヨ";
}
};

void main(){
using namespace std;
deque<Dog> dg;deque<Cat> ct;deque<Chick> ch;
vector<Animal*> animals;
dg.push_back(Dog("ポチ"));animals.push_back(&dg.back());
ch.push_back(Chick("しゃくれ"));animals.push_back(&ch.back());
((Chick*)animals.back())->growing();
dg.push_back(Dog("ジョン"));animals.push_back(&dg.back());
ct.push_back(Cat("タマ"));animals.push_back(&ct.back());
ch.push_back(Chick("コッコ"));animals.push_back(&ch.back());
ct.push_back(Cat("チビ"));animals.push_back(&ct.back());

for(auto v:animals){
printf("%s > %s\n",v->name().c_str(),v->bark().c_str());
}
}



D

import std.stdio;

import std.conv;
import std.windows.charset;

class Animal{
protected:
string u(string s){return to!(string)(toMBSz(s));}
public:
immutable string name;
string bark(){return "";}

this(string name){
this.name=u(name);
}
}

class Dog: Animal{
public:
this(string name){super(name);}

override string bark(){
return u("ワオーン");
}
}

class Cat: Animal{
public:
this(string name){super(name);}

override string bark(){
return u("ニャーン");
}
}

class Chick: Animal{
private:
bool isGrow=false;
public:
this(string name){super(name);}

void growing(){
isGrow=true;
}

override string bark(){
return u(isGrow? "コケコッコー": "ピヨピヨ");
}
}

void main(){
Animal[] animals;
animals~=new Dog("ポチ");
animals~=new Chick("しゃくれ");
(cast(Chick)animals[animals.length-1]).growing();
animals~=new Dog("ジョン");
animals~=new Cat("タマ");
animals~=new Chick("コッコ");
animals~=new Cat("チビ");

foreach(v;animals){
writefln("%s > %s",v.name,v.bark);
}
}