Contents [hide]
输出结果
1 2 | My name is FirstName LastName My sex is male |
编程代码示例
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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 |
您可以找到使用多种编程语言来解决同一问题的编程示例。
请为您的问题和解决方案找到最佳的编程语言。
涵盖所有情况: JavaScript Perl PHP Python Ruby | 涵盖一些情况: C C++ C# Go Java Rust Shell
大类别 Array | Class | Database | 测试 | 可变的 | 时间 | 数值 | 网路 | 文件系统 | 弦乐 | 系统
其他有用的内容
请为您的问题和解决方案找到最佳的编程语言。
涵盖所有情况: JavaScript Perl PHP Python Ruby | 涵盖一些情况: C C++ C# Go Java Rust Shell
大类别 Array | Class | Database | 测试 | 可变的 | 时间 | 数值 | 网路 | 文件系统 | 弦乐 | 系统
其他有用的内容
![](https://vpshikaku.com/app/uploads/vpshikaku.com/2nZbntr9-e1587540364868.jpg)
作者: 栗田 創。1st Class, inc的首席执行官/首席技术官。搜索引擎工程师、网站管理员
证书: AWS Solution Architect,Professional Scrum Master I ,一级IT工程师(日本国家证书),TOEIC900+,东京大学経済学部経営学科卒业
请关注我的Weblio帐户以获取更新
IT相关服务:VPS排名