클래스 만들기
package ch04;
public class Dog {
int age = 20;
String name = "토토";
String color = "하얀색";
final String type = "말티즈";
}
package ch04;
public class DogApp {
public static void main(String[] args) {
Dog d1 = new Dog();
System.out.println(d1.name);
System.out.println(d1.age);
System.out.println(d1.color);
System.out.println(d1.type);
d1.age = d1.age+1;
d1.color = "노란색";
d1.name = "레르코";
System.out.println(d1.name);
System.out.println(d1.age);
System.out.println(d1.color);
System.out.println(d1.type);
}
}
생성자
package ch04;
public class Cat {
String name;
String color;
public Cat(String n,String c) {
System.out.println("고양이가 탄생함");
name = n;
color = c;
}
}
package ch04;
public class CatApp {
public static void main(String[] args) {
Cat c1 = new Cat("집사1","하얀색");
System.out.println(c1.name);
System.out.println(c1.color);
}
}
생성자 (this)
package ch04;
public class People {
String name;
int age;
public People(String name,int age) {
System.out.println("메서드 스택 name : "+name);
System.out.println("메서드 스택 age : "+age);
this.name = name;
this.age = age;
}
}
package ch04;
public class PeopleApp {
public static void main(String[] args) {
People p1 = new People("홍길동",25);
}
}