함수 type()

Jmnote (토론 | 기여)님의 2020년 1월 12일 (일) 17:22 판
  다른 뜻에 대해서는 리눅스 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 *
    return 0;
}

3 Java

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() );
System.out.println( obj2.getClass() );
// class java.util.ArrayList
// class java.util.HashMap

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

4 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

5 PHP

echo gettype(123); // integer
echo gettype('123'); // string
echo gettype(array()); // array

6 Python

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

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

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

7 Perl

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

8 R

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

9 Ruby

puts "123".class
# String
puts 123.class
# Fixnum
puts [].class
# Array
print :hello.class
# Symbol
print Proc.new {}.class
# Proc

10 같이 보기

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