TIL

TIL - 230815

nayeonee__ 2023. 8. 15. 19:43

오버로딩의 조건

💡

  1. 메서드 이름이 같아야 한다.
  2. 매개변수의 개수 또는 타입이 달라야 한다.
  3. 매개변수는 같고 리턴타입이 다른 경우는 오버로딩이 성립되지 않는다.
       (리턴타입은 오버로딩을 구현하는데 아무런 영향을 주지 못한다.)

 

Math.pow()

public class Pow{
	public static void main(String[] args){
			double result = Math.pow(5, 2); // 5의 제곱
			System.out.println(result);
		}
}

// 출력
// 25.0
  • Math.pow() 메소드는 입력값과 출력값은 모두 double형이며 Math.pow(대상숫자,지수)를 넣는다.

Math.sqrt()

public class Sqrt{
	public static void main(String[] args){
			double result = Math.sqrt(25); // 25의 제곱근
			System.out.println(result);
		}
}

// 출력
// 5.0
  • Math.sqrt() 메소드는 입력값과 출력값은 모두 double형이며 Math.sqrt(대상숫자)를 넣는다.

인터페이스 다형성

package TIL.D230815;

interface Calculable{
    double PI = 3.14; // 변수를 정의할 때는 내용을 적는다.
    int sum(int v1, int v2);
}

interface Printable{
    void print();
}

class RealCal implements Calculable, Printable  {
    public int sum(int v1, int v2) {
        return v1+v2;
    }

    public void print(){
        System.out.println("This is ReaCal!");
    }
}

class AdvancedPrint implements Printable  {
    public void print(){
        System.out.println("This is ReaCal!");
    }
}

class DummyCal implements Calculable{
    public int sum(int v1, int v2){
        return 3;
    }
}

public class InterfaceApp {
    public static void main(String[] args) {
        Printable c = new AdvancedPrint();
     //   System.out.println(c.sum(2,1));
        c.print();
      //  System.out.println(c.PI);

    }
}

/*
다형성
비유 : 전자제품에 기능이 엄청 많으면 좋은가? 너무 많으면 배워야하는 부담감이 많다.
      나한테 필요한 기능만 보여주면 좋겠다~
      필요없는 기능을 감출 수 있다.
 */

 

 

 

 

// 참고 

자바의 정석(책) / https://coding-factory.tistory.com/532 / 유튜브 생활코딩 인터페이스 강의

'TIL' 카테고리의 다른 글

TIL - 230816  (0) 2023.08.16
TIL - 230810  (1) 2023.08.11
TIL -230808  (0) 2023.08.11
TIL - 230807  (0) 2023.08.11
TIL - 230804  (0) 2023.08.11