JAVA (1) : 객체지향 프로그래밍

Chloé·2023년 4월 13일
6

💻 JAVA

목록 보기
1/7

클래스 구현

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;
  }
}
profile
안녕하세용

6개의 댓글

comment-user-thumbnail
2023년 4월 13일

댓글 달아도 되나요?

1개의 답글
comment-user-thumbnail
2023년 4월 13일

자바를 공부하시다니!! 너무 멋있어요. C++도 찍먹해보세요^^~
장미꽃 한송이 두고 갑니다..총총 @}-`-,-----

1개의 답글
comment-user-thumbnail
2023년 4월 13일

자바 좀 치시네요^^

답글 달기
comment-user-thumbnail
2023년 4월 13일

비밀댓글입니다.

답글 달기