Create class and object for human

Contents

Java

// javac -d . Human.java;
// java jp.co.firstclass.Human;

// package jp.co.firstclass;

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

    public Human(String name, String sex){
        this.name = name;
        this.sex = 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[]) {
        Human human = new Human("FirstName LastName", "Male");
        human.sayName();
        human.saySex();
    }
}

JavaScript

if (typeof value !== 'undefined') {
    console.log('value is defined');
} else {
    console.log('value is not defined');
}

PHP

<?php
namespace FirstClass\Example;
class Human{
  private $name;
  private $sex;
  public function __construct($name, $sex){
    $this->name = $name;
    $this->sex = $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('FirstName LastName', 'Male');
  $pro->sayName();
  $pro->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";
  }
}

my $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(self.name)

	def saySex(self):
		print(self.sex)

if __name__ == "__main__":
	person1 = Human({"name":"First Last", "sex":"Male"})
	person1.sayName()
	person1.saySex()

Ruby

if defined? value
    puts "value is defined"
else
    puts "value is not defined"
end