创Class和Object

Contents

输出结果

My name is FirstName LastName
My sex is male

编程代码示例

C++

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

class Human {
    private:
        string name;
        string sex;

    public:
        Human(std::map<string, string> opt) {
            name = opt["name"];
            sex = opt["sex"];
        }

        void sayName() {
            std::cout << "My name is " + name << endl;
        }

        void saySex() {
            std::cout << "My sex is " + sex << endl;
        }
};

int main () {
    std::map<string, string> opt;
    opt["name"] = "FirstName LastName";
    opt["sex"] = "male";
    Human human(opt);
    human.sayName();
    human.saySex();
    return 0;
}

Java

import java.util.*;

public class Human {
    private String name;
    private String sex;

    public Human(Map<String, String> opt){
        this.name = opt.get("name");
        this.sex = opt.get("sex");
    }

    public void sayName() {
        System.out.println("My name is " + this.name);
    }

    public void saySex() {
        System.out.println("My sex is " + this.sex);
    }

    public static void main(String args[]) {
        Map<String, String> opt = new HashMap<String, String>();
        opt.put("name", "FirstName LastName");
        opt.put("sex", "male");
        Human human = new Human(opt);
        human.sayName();
        human.saySex();
    }
}

Go

package main
import "fmt"

type Human struct{
	name string
	sex string
}

func (h *Human) sayName() {
	fmt.Println("My name is " + h.name)
}

func (h *Human) saySex() {
	fmt.Println("My sex is " + h.sex)
}

func main() {
    human := Human{name:"FirstName LastName", sex:"male"}
    human.sayName()
	human.saySex()
}

JavaScript

class Human {
	constructor(opt) {
		this.name = opt["name"];
        this.sex = opt["sex"];
    }

	sayName() {
		console.log("My name is " + this.name);
    }

	saySex() {
		console.log("My sex is " + this.sex);
    }
};
module.exports = Human;

if(!module.parent) {
	let human1 = new Human({"name":"FirstName LastName", "sex":"male"});
	human1.sayName();
	human1.saySex();
}

Perl

package Human;
use strict;
use Data::Dumper;
use warnings;

sub new(){
  my $class = shift;
  my $op = shift;
  my $name = $op->{'name'} || "";
  my $sex = $op->{'sex'} || "";
  my $self = {'name' => $name,
  'sex' => $sex};
  return bless($self);
}

sub sayName(){
  my $self = shift;
  if($self->{'name'}){
    print "My name is ".$self->{'name'}."\n";
  }
}

sub saySex(){
  my $self = shift;
  if($self->{'sex'}){
    print "My sex is ".$self->{'sex'}."\n";
  }
}

if ($0 eq __FILE__) {
  my $pro = new Human({'name' => 'FirstName LastName', 'sex' => 'male'});
  $pro->sayName();
  $pro->saySex();
}
else{
  1;
}

PHP

<?php
namespace FirstClass\Example1;

class Human{
  private $name;
  private $sex;
  public function __construct($opt){
    $this->name = $opt["name"];
    $this->sex = $opt["sex"];
  }

  public function sayName(){
    print "My name is ".$this->name."\n";
  }

  public function saySex(){
    print "My sex is ".$this->sex."\n";
  }
}

if ( !isset(debug_backtrace()[0]) ) {
  $pro = new Human(["name" => "FirstName LastName", "sex" => "male"]);
  $pro->sayName();
  $pro->saySex();
}

Python

class Human:
	def __init__(self, opt):
		self.name = opt["name"]
		self.sex = opt["sex"]

	def sayName(self):
		print("My name is " + self.name)

	def saySex(self):
		print("My sex is " + self.sex)

if __name__ == "__main__":
	person1 = Human({"name":"FirstName LastName", "sex":"male"})
	person1.sayName()
	person1.saySex()

Ruby

class Human
    def initialize(opt)
        @name = opt["name"]
        @sex = opt["sex"]
    end

    def sayName
        puts "My name is " + @name.to_s
    end

    def saySex
        puts "My sex is " + @sex.to_s
    end
end

if $0 == __FILE__
    person1 = Human.new({"name" => "FirstName LastName", "sex" => "male"})
    person1.sayName
    person1.saySex
end