ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 자바 step4 - Method(1)
    Java 2022. 10. 25. 20:58

     

    MyObject Class를 생성하였다.

    package test.mypac;
    /*
     * 	[ 메소드를 만들때 고려해야 하는것 ]
     * 	
     * 	1. 접근 지정자
     * 	2. static or non static
     * 	3. 리턴 type
     * 	4. 메소드명
     * 	5. 메소드에 전달하는 인자의 갯수와 데이터 type
     */
    public class MyObject {
    	/*
    	 * 	public => 이 메소드는 어디에서든 접근 가능하다
    	 * 	void => 이 메소드는 어떤 값도 리턴하지 않는다
    	 * 	walk => 메소드명
    	 * 	walk() => 이 메소드는 어떤 값도 인자로 전달받지 않는다
    	 */
    	public void walk() {
    		System.out.println("걸음을 걸어요");
    	}
    	// int type을 리턴해주는 메소드
    	public int getNumber() {
    		return 10;
    	}
    	// String type을 리턴해주는 메소드
    	public String getGreeting() {
    		return "안녕하세요";
    	}
    	//Car type 을 리턴해주는 메소드
    	public Car getCar() {
    		return new Car();
    	}
    	//int type 을 메소드의 인자로 전달 받는 메소드
    	public void setNum(int num) {
    		System.out.println("num:"+num);
    	}
    	//String type 을 메소드의 인자로 전달 받는 메소드
    	public void setName(String name) {
    		System.out.println("name:"+name);
    	}
    	//Radio type 을 메소드의 인자로 전달 받는 메소드
    	public void useRadio(Radio r) {
    		r.listenMusic();
    	}
    	// Gun type 과 Arrow type 을 메소드의 인자로 전달 받는 메소드
    	public void attack(Gun g,Arrow a) {
    		System.out.println("총과 활로 공격을 해요");
    	}
    }

     

    package test.main;
    
    import test.mypac.MyObject;
    
    public class MainClass01 {
    	public static void main(String[] args) {
    		// MyObject 클래스에 정의된 3개의 메소드를 차례로 호출하는 code 를 작성해보세요.
    		MyObject MO=new MyObject();
    		MO.walk();
    		int a=MO.getNumber();
    		String b=MO.getGreeting();
    		System.out.println(a);
    		System.out.println(b);
    	}
    }
    
    <console>
    걸음을 걸어요
    10
    안녕하세요

    MyObject class를 상속받아 MO라는 객체로 생성하였다. 상속받아 객체를 생성하였으므로 메소드를 사용할 수 있다.

    a라는 int타입의 변수에 MO.getNumber메소드를 참조하였다. return 값이 10이기에 10이 들어간다.

    마찮가지로 b라는 string 타입의 변수에 MO.getGreeting메소드를 참조하였다. 안녕하세요의 값을 리턴받았기 때문에 그 값을 호출하였다.

     

     

    package test.mypac;
    
    public class Car {
    	
    	// 생성자 (new 할 때 호출되는 부분)
    	public Car(){// 클래스명() 
    		System.out.println("CAR 생성자 호출됨!");
    	}
    	
    	// non static 메소드
    	public void drive() {
    		System.out.println("달려요!");
    	}
    }

     

     

    package test.main;
    
    import test.mypac.Car;
    import test.mypac.MyObject;
    
    public class MainClass02 {
    	public static void main(String[] args) {
    		//Car 객체의 참조값을 얻어내서 drive() 메소드를 여기서 호출해보세요
    		new MyObject().getCar().drive();
    		
    		//위의 1줄을 풀어서 쓰면 아래와 같다
    		MyObject obj=new MyObject();
    		Car car1=obj.getCar();
    		car1.drive();
    	}
    }
    
    <console>
    CAR 생성자 호출됨!
    달려요!
    CAR 생성자 호출됨!
    달려요!

    MyObject class에 Car 타입으로 메소드를 생성하였으므로 Car타입의 drive()라는 메소드를 MyObject class를 건너 호출 가능하다. 반대로 바로 Car class를 상속받아 호출할 수도 있다.

    System.out.println()을 사용하지 않아도 MyObject class의 메소드 안에 이미 작성해놨기 때문에 따로 작성하지 않아도 된다.

     

     

    package test.main;
    
    import java.awt.BorderLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class MainClass03 {
    	public static void main(String[] args) {
    		JFrame f=new JFrame();
    		f.setSize(300, 400);
    		f.setTitle("오잉 제목?");
    		f.setLayout(new BorderLayout());
    		
    		JButton btn=new JButton();
    		btn.setText("눌러보셈");
    		
    		f.add(btn, BorderLayout.NORTH);
    		
    		f.setVisible(true);
    	}
    }

    JFrame은 내장 class로 GUI를 만들 수 있는 class이다. 개발로서는 자주 사용하지 않으므로 참고만 하는 것이 좋다.

    setSize등의 메소드는 JFrame class 안에 들어있는 메소드이다.

     

     

     

    댓글

Designed by Tistory.