풀스택/Java다시 복습 처음부터
java 복습 3-1(객체지향 프로그래밍) - class
woohyun22
2019. 6. 4. 01:35
클래스와 객체에 대한 설명
1 2 3 4 5 6 7 8 9 | public class Animal { //선언만 있고 내용은 없는 껍데기뿐인 클래스 //껍데기만 있어도 객체 생성이 가능하다. Animal cat = new Animal(); //new는 객체를 생성할때 쓰이고, Animal클래스의 객체인 인스턴스 - cat이 만들어진다. //과자틀은 클래스 //과자틀에 의해서 만들어진 과자들 = } | cs |
cat.name - cat객체의 name객체변수
1 2 3 4 5 6 7 8 9 | public class Animal { String name; public static void main(String[] args) { Animal cat = new Animal(); System.out.println(cat.name); //객체 선언 후 대입이 없으므로 값은 null값이 나온다. } } | cs |
메소드는 클래스 내에서 구현된 함수를 말함.
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class Animal { String name; public void setName(String name) {//입력 : String name , 출력 : void (리턴값 없음) this.name = name; //현재 입력값은 있지만 출력값이 없다. } public static void main(String[] args) { Animal cat = new Animal(); cat.setName("boby");// 메소드 호출 = 객체. System.out.println(cat.name); } } | cs |
728x90