클래스 구현
class Dog {
String name;
String breeds;
int age;
void wag() {
System.out.println("살랑살랑~");
}
void bark() {
System.out.println("멍멍!");
}
}
객체의 활용
Dog dog1 = new Dog();
dog1.name = "망고";
dog1.bark();
변수의 스코프
class DrinkMachine {
String output;
void pushButton(int num) {
String[] drinks = {"콜라", "사이다", "맥주"};
output = drinks[num];
}
void printOutput() {
System.out.println(output);
}
}
정사각형 넓이
public class SquareTest {
public static void main(String[] args) {
Square s = new Square();
s.length = 4;
System.out.prinf("한 변이 %d인 정사각형 넓이: %d", s.length, s.area());
}
}
class Square {
int length;
int area() {
return length * length;
}
}
댓글 달아도 되나요?