(→PHP) |
(→PHP) |
||
7번째 줄: | 7번째 줄: | ||
|} | |} | ||
== | ==Java== | ||
<source lang=' | {{참고|HR30 Day 4: Class vs. Instance/Java}} | ||
<source lang='java'> | |||
class Person{ | public class Person { | ||
private | private int age; | ||
public | public Person(int initialAge) { | ||
if( | // 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 | public void amIOld() { | ||
if( | // 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; | return; | ||
} | } | ||
if( | if( this.age < 18 ) { | ||
System.out.println("You are a teenager."); | |||
return; | return; | ||
} | } | ||
System.out.println("You are old."); | |||
} | } | ||
public | public void yearPasses() { | ||
// Increment this person's age. | |||
this.age++; | |||
} | } | ||
</source> | </source> | ||
2018년 8월 12일 (일) 09:30 판
1 개요
- Day 4: Class vs. Instance
2 Java

Java
Copy
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 Python
Python
Copy
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
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.