質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

1937閲覧

Javaでは実装できるメソッドを持つenumについて

Paalon

総合スコア232

Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

1グッド

0クリップ

投稿2016/05/11 16:45

編集2016/05/11 17:42

Javaでは以下のように方向を管理するクラスDirectionが作れ、直感的に使えます。

Java

1public enum Direction { 2 FRONT(0), 3 RIGHT(1), 4 BACK(2), 5 LEFT(3), 6 DOWN(0), 7 UP(2); 8 9 private int value; 10 11 Direction(int value) { 12 this.value = value; 13 } 14 public void turnRight() { 15 this.value = (this.value + 3) % 4; 16 } 17 public void turnLeft() { 18 this.value = (this.value + 1) % 4; 19 } 20 public void reverse() { 21 this.value = (this.value + 2) % 4; 22 } 23 public int get() { 24 return this.value; 25 } 26} 27 28public class Test { 29 public static void main(String[] args) { 30 Direction a = Direction.FRONT; 31 Direction b = Direction.LEFT; 32 System.out.println("a: " + a.get()); // a: 0 33 System.out.println("b: " + b.get()); // b: 3 34 b.turnRight(); 35 System.out.println("b: " + b.get()); // b: 2 36 } 37}

JavaScriptとC++とGolangとPythonではそれぞれ、どのようにこのようなクラスを実装すれば良いのでしょうか?(見た目の綺麗さの面や処理速度的な面、妥協点について)
4つまとめてでなくて構わないのでお聞かせ下さい。

と思っていたのですが、以上のJavaのコードは間違っていたのでC++で書いたものを載せます。すみません。

C++

1#include <iostream> 2 3class Direction { 4public: 5 static const int FRONT = 0; 6 static const int RIGHT = 1; 7 static const int BACK = 2; 8 static const int LEFT = 3; 9 static const int DOWN = 0; 10 static const int UP = 2; 11private: 12 int value; 13 14public: 15 Direction() { 16 this->value = FRONT; 17 } 18 Direction(int value) { 19 this->value = value; 20 } 21 void turnRight() { 22 this->value = (this->value + 3) % 4; 23 } 24 void turnLeft() { 25 this->value = (this->value + 1) % 4; 26 } 27 void reverse() { 28 this->value = (this->value + 2) % 4; 29 } 30 int get() { 31 return this->value; 32 } 33}; 34 35int main() { 36 Direction a(Direction::FRONT); 37 std::cout << a.get() << std::endl; 38 a.turnLeft(); 39 std::cout << a.get() << std::endl; 40 return 0; 41}
kei344👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

swordone

2016/05/11 16:53

質問自体とは余り関係ありませんが、このような実装はやめたほうがいいかと。仮にLEFTのオブジェクトに対してturnRightした場合、「オブジェクト名がLEFTなのに前方向を意味する0を持つオブジェクト」となり、何が何やら意味不明なことになります。
Paalon

2016/05/11 17:35

御指摘の通りです。いろいろと勘違いをしていました、すみません。意図した動作をしている(はずの)C++のコードをあげました。
guest

回答2

0

ベストアンサー

C++14だと

#include <iostream> #include <cstdint> #include <type_traits> class Direction { public: using value_type = std::uint8_t; static constexpr value_type front = 0; static constexpr value_type right = 1; static constexpr value_type back = 2; static constexpr value_type left = 3; static constexpr value_type down = 0; static constexpr value_type up = 5; private: value_type value; public: constexpr Direction() noexcept : value(front) {} constexpr Direction(value_type value) noexcept : value(value) {} Direction(const Direction&) = default; Direction(Direction&&) = default; Direction& operator=(const Direction&) = default; Direction& operator=(Direction&&) = default; constexpr void turn_right() noexcept { this->value = (this->value + 3) & 0b11; } constexpr void turn_left() noexcept { this->value = (this->value + 1) & 0b11; } constexpr void reverse() noexcept { this->value = (this->value + 2) & 0b11; } template<typename T = value_type, std::enable_if_t<std::is_arithmetic<T>::value, std::nullptr_t> = nullptr> constexpr T get() const noexcept { return this->value; } }; int main() { Direction a(Direction::front); std::cout << a.get<unsigned>() << std::endl; a.turn_left(); std::cout << a.get<unsigned>() << std::endl; return 0; }

こんなのかな。
動作
sprout使えばコンパイル時に文字列使えるけどそこまでしたくない

投稿2016/05/13 16:32

編集2016/05/13 16:40
yumetodo

総合スコア5850

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

JavaScriptで。JAVAもC++も不勉強なため、根本的に違う実装だったらすみません。turnRight turnLeft は回転方向が逆な気もしますが例示されているコードに準拠しました。

動くサンプル

JavaScript

1"use strict" 2function Direction( val ) { 3 if ( !( this instanceof Direction ) ) { return new Direction( val ); } 4 var value; 5 this.setValue = function( int ) { 6 value = this.checkInt( int ); 7 } 8 this.getValue = function() { 9 return value; 10 } 11 value = this.checkDirection( val ); 12 if ( isNaN( value ) ) { 13 value = this.checkInt( val ); 14 } 15 return this; 16} 17Direction.prototype = { 18 checkInt : function( int ) { 19 int = parseInt( int, 10 ); 20 int = isNaN( int ) ? 0 : int; 21 return ( int < 0 || int > 3 ) ? 0 : int; 22 } 23 , checkDirection : function( str ) { 24 var int = [ 'FRONT', 'RIGHT', 'BACK', 'LEFT' ].indexOf( str ); // array.indexOf : IE9+ 25 return ( int === -1 ) ? NaN : int; 26 } 27 , turnRight : function() { 28 this.setValue( ( this.getValue() + 3 ) % 4 ); 29 } 30 , turnLeft : function() { 31 this.setValue( ( this.getValue() + 1 ) % 4 ); 32 } 33 , reverse : function() { 34 this.setValue( ( this.getValue() + 2 ) % 4 ); 35 } 36 , get : function() { 37 return this.getValue(); 38 } 39} 40var d = new Direction( 'LEFT' ); 41console.log( d.get() ); 42d.turnRight(); 43console.log( d.get() ); 44var a = Direction( 3 ); 45console.log( a.get() ); 46a.turnRight(); 47console.log( a.get() ); 48console.log( a );

投稿2016/05/11 19:38

編集2016/05/11 20:12
kei344

総合スコア69357

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問