"HR30 Day 4: Class vs. Instance"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
 
(다른 사용자 한 명의 중간 판 6개는 보이지 않습니다)
7번째 줄: 7번째 줄:
|}
|}


==같이 보기==
==Java==
* [[해커랭크 Diagonal Difference]]
{{참고|HR30 Day 4: Class vs. Instance/Java}}
<syntaxhighlight lang='java'>
public class Person {
private int age;
public Person(int initialAge) {
// Add some more code to run some checks on initialAge
if( initialAge < 0 ) {
System.out.println("Age is not valid, setting age to 0.");
initialAge = 0;
}
this.age = initialAge;
}
public void amIOld() {
// Write code determining if this person's age is old and print the correct statement:
if( this.age < 13 ) {
System.out.println("You are young.");
return;
}
if( this.age < 18 ) {
System.out.println("You are a teenager.");
return;
}
System.out.println("You are old.");
}
public void yearPasses() {
// Increment this person's age.
this.age++;
}
</syntaxhighlight>


==PHP==
==PHP==
<source lang='php'>
<syntaxhighlight lang='php'>
<?php
<?php
class Person{
class Person{
37번째 줄: 65번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>
 
==Python==
<syntaxhighlight lang='python'>
class Person:
    def __init__(self,initialAge):
        if initialAge < 0:
            print("Age is not valid, setting age to 0.")
            initialAge = 0
        self.age = initialAge
    def amIOld(self):
        if self.age < 13:
            print("You are young.")
            return
        if 13 <= self.age and self.age < 18:
            print("You are a teenager.")
            return
        print("You are old.")
    def yearPasses(self):
        self.age += 1
</syntaxhighlight>

2021년 7월 31일 (토) 10:35 기준 최신판

1 개요[ | ]

Day 4: Class vs. Instance
해커랭크 30 Days of Code
문제 풀이
0-9 Day e
HR30 Day 0: Hello, World.

HR30 Day 1: Data Types

HR30 Day 2: Operators

HR30 Day 3: Intro to Conditional Statements

HR30 Day 4: Class vs. Instance

HR30 Day 5: Loops

HR30 Day 6: Let's Review

HR30 Day 7: Arrays

HR30 Day 8: Dictionaries and Maps

HR30 Day 9: Recursion

2 Java[ | ]

public class Person {
	private int age;	
	public Person(int initialAge) {
		// Add some more code to run some checks on initialAge
		if( initialAge < 0 ) {
			System.out.println("Age is not valid, setting age to 0.");
			initialAge = 0;	
		}
		this.age = initialAge;
	}
	public void amIOld() {
		// Write code determining if this person's age is old and print the correct statement:
		if( this.age < 13 ) {
			System.out.println("You are young.");
			return;
		}
		if( this.age < 18 ) {
			System.out.println("You are a teenager.");
			return;
		}
		System.out.println("You are old.");
	}
	public void yearPasses() {
		// Increment this person's age.
		this.age++;
	}

3 PHP[ | ]

<?php
class Person{
    private $age;
    public function __construct($initialAge){
        if( $initialAge < 0 ) {
            echo "Age is not valid, setting age to 0.\n";
            $initialAge = 0;
        }
        $this->age = $initialAge;
    }
    public  function amIOld(){
        if( $this->age < 13 ) {
            echo "You are young.\n"; 
            return;
        } 
        if( 13 <= $this->age && $this->age < 18 ) {
            echo "You are a teenager.\n"; 
            return;
        } 
        echo "You are old.\n"; 
    }
    public  function yearPasses(){
        $this->age++;
    }
}

4 Python[ | ]

class Person:
    def __init__(self,initialAge):
        if initialAge < 0:
            print("Age is not valid, setting age to 0.")
            initialAge = 0
        self.age = initialAge
    def amIOld(self):
        if self.age < 13:
            print("You are young.")
            return
        if 13 <= self.age and self.age < 18:
            print("You are a teenager.")
            return
        print("You are old.")
    def yearPasses(self):
        self.age += 1
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}