つれずれなるままに… このページをアンテナに追加 RSSフィード Twitter

自身をYuichirouと名乗る謎の男が文字通り「つれれなるままに」書くよくわからん日記。

検索サイトから来た方、こんなページでゴメンナサイ。下にあるフォームに検索ワードを入れて検索すると、情報が得られるかも。

なお、タイトルに打ち間違いはありません。

1000 | 01 | 02 | 03 | 04 |
1504 | 01 | 02 | 03 |
2003 | 10 | 11 | 12 |
2004 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2005 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2006 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2007 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2008 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2009 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2010 | 01 | 02 | 03 | 04 |

2009年4月14日

いろんな言語オブジェクト指向Hello World 19:34 いろんな言語でオブジェクト指向的Hello Worldを含むブックマーク

Java

public class Welcome {
    private String name;
  
    private Welcome(String name) {
        this.name = name;
    }

    private String getName() {
        return this.name;
    }

    public void say() {
        System.out.println("こんにちは、" + this.getName() + "!");
    }

    public static void main(String[] args) {
        Welcome welcome = new Welcome("世界");
        welcome.say();
    }
}

まぁこんな感じのものをいろんな言語で。

Perl

#!/usr/bin/env perl                                                             
use strict;
use warnings;
use utf8;
binmode STDOUT, ":utf8";

package Welcome;

sub new {
    my $class = shift;
    my $name = shift;
    bless { name => $name }, $class;
}

sub name {
    shift->{name}
}

sub say {
    my $self = shift;
    print "こんにちは、" . $self->name . "!\n";
}

my $welcome = Welcome->new("世界");
$welcome->say;

Ruby

#!/usr/bin/env ruby -Ku

class Welcome
  def initialize(name)
    @name = name
  end

  def say
    puts "こんにちは、" + @name + "!"
  end

  attr_reader :name
end

welcome = Welcome.new("世界")
welcome.say

Python

#!/usr/bin/env python                                                           
# -*- coding: utf-8 -*-                                                         

class Welcome:
    def __init__(self, name):
        self.name = name

    def say(self):
        print "こんにちは、" + self.name + "!"

welcome = Welcome("世界")
welcome.say()

Pythonではプライベートプロパティ作れないってホント?

C++

#include <iostream>
#include <string>
using namespace std;

class Welcome
{
  string name;
public:
  Welcome(string s):name(s) {}
  void say() const;
};

void Welcome::say() const {
  cout << "こんにちは、" << name << "!" << endl;
}

int main() {
  Welcome welcome("世界");
  welcome.say();
}

ホントは複数ファイルに分割するんだろうけど短いから1つで。

Objective-C

これはさすがに分割した。

Welcome.h
#import <Foundation/Foundation.h>

@interface Welcome : NSObject
{   
  NSString *name;
}
@property(copy, readwrite) NSString *name;
- (id)initWithName:(NSString *)name;
- (void)say;
@end
Welcome.m
#import <stdio.h>
#import "Welcome.h"

@implementation Welcome
@synthesize name;
- (id)initWithName:(NSString *)theName {
  if (self = [super init]) {
    self.name = theName;
  }
  return self;
}
- (void)say {
  printf("こんにちは、%s!\n", [name UTF8String]);
}
@end
main.m
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "Welcome.h"

int main() {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  Welcome* welcome = [[[Welcome alloc] initWithName:@"世界"] autorelease];
  [welcome say];

  [pool release];
  return 0;
}

Cocoa? ターミナルで動かしたかったので……

TODO