"Constructor"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(다른 사용자 한 명의 중간 판 3개는 보이지 않습니다)
5번째 줄: 5번째 줄:
==JavaScript==
==JavaScript==
[[분류: JavaScript]]
[[분류: JavaScript]]
<source lang='JavaScript'>
<syntaxhighlight lang='JavaScript'>
function Dog(name) {
function Dog(name) {
   this.name = name;
   this.name = name;
12번째 줄: 12번째 줄:
console.log(myDog.name);
console.log(myDog.name);
// snoopy
// snoopy
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[분류: PHP]]
[[분류: PHP]]
<source lang='PHP'>
<syntaxhighlight lang='PHP'>
class Dog {
class Dog {
   public $name;
   public $name;
26번째 줄: 26번째 줄:
echo $myDog->name;
echo $myDog->name;
// snoopy
// snoopy
</source>
</syntaxhighlight>
 
==Python==
[[분류: Python]]
<syntaxhighlight lang='Python'>
class Dog(object):
def __init__(self, name):
super(Dog, self).__init__()
self.name = name
 
mydog = Dog("snoopy")
print mydog.name
# snoopy
</syntaxhighlight>


==Ruby==
==Ruby==
<source lang='Ruby'>
<syntaxhighlight lang='Ruby'>
class Dog
class Dog
   def initialize(name)
   def initialize(name)
37번째 줄: 50번째 줄:


myDog = Dog.new("snoopy")
myDog = Dog.new("snoopy")
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[class]]
*[[class]]
*[[class variable]]
*[[생성자]]
*[[생성자]]
*[[destructor]]

2020년 11월 2일 (월) 02:31 기준 최신판

클래스 생성자
__construct

1 JavaScript[ | ]

function Dog(name) {
  this.name = name;
}
var myDog = new Dog("snoopy");
console.log(myDog.name);
// snoopy

2 PHP[ | ]

class Dog {
  public $name;
  public function __construct($name) {
    $this->name = $name;
  }
}
$myDog = new Dog("snoopy");
echo $myDog->name;
// snoopy

3 Python[ | ]

class Dog(object):
	def __init__(self, name):
		super(Dog, self).__init__()
		self.name = name

mydog = Dog("snoopy")
print mydog.name
# snoopy

4 Ruby[ | ]

class Dog
  def initialize(name)
    @name = name
  end
end

myDog = Dog.new("snoopy")

5 같이 보기[ | ]

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