"함수 type()"의 두 판 사이의 차이

 
(같은 사용자의 중간 판 12개는 보이지 않습니다)
87번째 줄: 87번째 줄:
==JavaScript==
==JavaScript==
[[category: JavaScript]]
[[category: JavaScript]]
{{참고|JavaScript typeof}}
<syntaxhighlight lang='JavaScript' run>
<syntaxhighlight lang='JavaScript' run>
console.log( typeof [] );            // object
console.log( typeof [] );            // object
112번째 줄: 113번째 줄:
{{소스헤더|Python 3}}
{{소스헤더|Python 3}}
<syntaxhighlight lang='Python' run>
<syntaxhighlight lang='Python' run>
print(type(123))
print(type(123))     # <class 'int'>
print(type('123'))
print(type('123'))   # <class 'str'>
print(type({}))
print(type({}))     # <class 'dict'>
print(type([]))
print(type([]))     # <class 'list'>
# <class 'int'>
# <class 'str'>
# <class 'dict'>
# <class 'list'>


print(type('안녕'))
print(type('안녕'))   # <class 'str'>
print(type(u'안녕'))
print(type(u'안녕')) # <class 'str'>
# <class 'str'>
# <class 'str'>
</syntaxhighlight>
</syntaxhighlight>
:→ Python 3는 항상 str = unicode
:→ Python 3는 항상 str = unicode
130번째 줄: 125번째 줄:
{{소스헤더|Python 2}}
{{소스헤더|Python 2}}
<syntaxhighlight lang='Python'>
<syntaxhighlight lang='Python'>
print(type(123))
print(type(123))     # <type 'int'>
print(type('123'))
print(type('123'))   # <type 'str'>
print(type({}))
print(type({}))     # <type 'dict'>
print(type([]))
print(type([]))     # <type 'list'>
# <type 'int'>
# <type 'str'>
# <type 'dict'>
# <type 'list'>


print(type('안녕'))
print(type('안녕'))   # <type 'str'>
print(type(u'안녕'))
print(type(u'안녕')) # <type 'unicode'>
# <type 'str'>
# <type 'unicode'>
</syntaxhighlight>
</syntaxhighlight>


150번째 줄: 139번째 줄:
use Scalar::Util qw(reftype);
use Scalar::Util qw(reftype);
my $x = bless {}, 'Wiki::ZetaWiki';
my $x = bless {}, 'Wiki::ZetaWiki';
print "type of x: " . ref($x) . "\n";
print "type of x: "     . ref($x)     . "\n"; # type of x: Wiki::ZetaWiki
print "base type of x: " . reftype($x) . "\n";
print "base type of x: " . reftype($x) . "\n"; # base type of x: HASH
#type of x: Wiki::ZetaWiki
#base type of x: HASH
</syntaxhighlight>
</syntaxhighlight>


160번째 줄: 147번째 줄:
{{참고|R언어 class()}}
{{참고|R언어 class()}}
<syntaxhighlight lang='r' run>
<syntaxhighlight lang='r' run>
class(42)
class(42)             #[1] "numeric"
#[1] "numeric"
class(TRUE)           #[1] "logical"
class(TRUE)
class("Hello World") #[1] "character"
#[1] "logical"
class("Hello World")
#[1] "character"
</syntaxhighlight>
</syntaxhighlight>


==Ruby==
==Ruby==
[[category: Ruby]]
[[category: Ruby]]
{{참고|Ruby .class}}
<syntaxhighlight lang='Ruby' run>
<syntaxhighlight lang='Ruby' run>
puts "123".class
puts "123".class         # String
# String
puts 123.class           # Integer
puts 123.class
puts 123.45.class        # Float
# Fixnum
 
puts [].class
puts [].class           # Array
# Array
puts :hello.class       # Symbol
print :hello.class
puts Proc.new {}.class   # Proc
# Symbol
print Proc.new {}.class
# Proc
</syntaxhighlight>
</syntaxhighlight>



2022년 5월 18일 (수) 19:57 기준 최신판

  다른 뜻에 대해서는 리눅스 type 문서를 참조하십시오.

1 개요[ | ]

.getClass()
gettype
type
typeof
.class

2 C[ | ]

#include <stdio.h>

#define type(x) _Generic((x),                                                     \
        _Bool: "_Bool",                  unsigned char: "unsigned char",          \
         char: "char",                     signed char: "signed char",            \
    short int: "short int",         unsigned short int: "unsigned short int",     \
          int: "int",                     unsigned int: "unsigned int",           \
     long int: "long int",           unsigned long int: "unsigned long int",      \
long long int: "long long int", unsigned long long int: "unsigned long long int", \
        float: "float",                         double: "double",                 \
  long double: "long double",                   char *: "char *",                 \
       void *: "void *",                         int *: "int *",                  \
      default: "unknown")

int main() {
    char  ch;
    int   a;
    long  b;
    char* str;
    char  str2[5];

    printf( "%s\n", type(ch)   ); // char
    printf( "%s\n", type(a)    ); // int
    printf( "%s\n", type(b)    ); // long int
    printf( "%s\n", type(str)  ); // char *
    printf( "%s\n", type(str2) ); // char *
}

3 Go[ | ]

package main

import (
    "fmt"
    "reflect"
)

func main() {
    fmt.Println(reflect.TypeOf(123))     // int
    fmt.Println(reflect.TypeOf(123.45))  // float64
    fmt.Println(reflect.TypeOf("hello")) // string
    
    fmt.Println(reflect.TypeOf(true))    // bool
    fmt.Println(reflect.TypeOf(false))   // bool
    fmt.Println(reflect.TypeOf(nil))     // <nil>
}

4 Java[ | ]

import java.util.ArrayList;
import java.util.HashMap;
class MyClass{public static void main(String[] args){
	System.out.println( "hello".getClass() );  // class java.lang.String

	Object obj1 = new ArrayList<Object>();
	Object obj2 = new HashMap<String,Object>();
	System.out.println( obj1.getClass() );     // class java.util.ArrayList
	System.out.println( obj2.getClass() );     // class java.util.HashMap

	System.out.println( obj1.getClass() == ArrayList.class );  // true
	System.out.println( obj1.getClass() == HashMap.class   );  // false
	System.out.println( obj2.getClass() == ArrayList.class );  // false
	System.out.println( obj2.getClass() == HashMap.class   );  // true
}}

5 JavaScript[ | ]

console.log( typeof [] );            // object
console.log( typeof {} );            // object
console.log( typeof 42 );            // number
console.log( typeof "hello" );       // string
console.log( typeof function(){} );  // function

var func = function() {};
console.log( typeof func );          // function

6 PHP[ | ]

echo gettype(123)  ."\n";  # integer
echo gettype('123')."\n";  # string
echo gettype([])   ."\n";  # array

7 Python[ | ]

Python 3
print(type(123))     # <class 'int'>
print(type('123'))   # <class 'str'>
print(type({}))      # <class 'dict'>
print(type([]))      # <class 'list'>

print(type('안녕'))   # <class 'str'>
print(type(u'안녕'))  # <class 'str'>
→ Python 3는 항상 str = unicode
Python 2
print(type(123))     # <type 'int'>
print(type('123'))   # <type 'str'>
print(type({}))      # <type 'dict'>
print(type([]))      # <type 'list'>

print(type('안녕'))   # <type 'str'>
print(type(u'안녕'))  # <type 'unicode'>

8 Perl[ | ]

use Scalar::Util qw(reftype);
my $x = bless {}, 'Wiki::ZetaWiki';
print "type of x: "      . ref($x)     . "\n";  # type of x: Wiki::ZetaWiki
print "base type of x: " . reftype($x) . "\n";  # base type of x: HASH

9 R[ | ]

class(42)             #[1] "numeric"
class(TRUE)           #[1] "logical"
class("Hello World")  #[1] "character"

10 Ruby[ | ]

puts "123".class         # String
puts 123.class           # Integer
puts 123.45.class        # Float

puts [].class            # Array
puts :hello.class        # Symbol
puts Proc.new {}.class   # Proc

11 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}