달력

72025  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31


/*
생성자 Constructor
클래스명과 동일하다
결과형 반환형이 없다.
구현하지 않으면 default가 실행 된다.
객체 생성시 자동 호출 된다.
객체 초기화에 사용된다.
*/

class  ConstructorEx
{
 private String name;
 private int age;
 public void setName( String n )
 {
  name = n;
 }

 public void setAge( int a )
 {
  age = a;
 }

 public String getName()
 {
  return name;
 }

 public int getAge()
 {
  return age;
 }

 public ConstructorEx( String name, int age )
 {
  System.out.println( "생성자" );
  this.name = name;
  this.age = age;
 }

 public ConstructorEx()
 {
  System.out.println( "디폴트 생성자" );
 }

 public static void main(String[] args)
 {
  ConstructorEx ce = new  ConstructorEx();
  System.out.println( "이름 : " + ce.getName() );
  System.out.println( "나이 : " + ce.getAge() );
 
  ConstructorEx ce1 = new ConstructorEx( "홍길동", 30 );
  System.out.println( "이름 : " + ce1.getName() );
  System.out.println( "나이 : " + ce1.getAge() );
 }
}

Posted by cdprkr2077
|