본문 바로가기

프로그래밍 공부/java

[JAVA] student 클래스의 멤버변수를 적절한 생성자를 이용해 초기화하기

반응형

Q. student 클래스의 멤버변수를 default 생성자가 아닌 적절한 생성자를 이용하여 초기화하는 프로그램을 만들어보세요.

public class Student {
	
	String name;
	int age;
	double height;
	
	public Student(String name, int age, double height) {
		this.name = name;
		this.age = age;
		this.height = height;
	}
}
public class Student_1 {

	public static void main(String[] args) {
		Student stu = new Student("도토리", 33, 160.4);

		System.out.println("이름 : " + stu.name);
		System.out.println("나이 : " + stu.age);
		System.out.println("키 : " + stu.height);
				
	}
}
반응형