16: JAVA interface2

jk·2024년 1월 22일
0

kdt 풀스택

목록 보기
28/127



1.아래의 Main메소드가 돌아 가도록 프로그래밍 하시오.

//-SPrinterDriver 와 LPrinterDriver를 만드시오
//
interface Printable {
	public abstract void print(String doc);
}
//
//
//
public class InterfaceTest {
//
	public static void main(String[] args) {
//
		String myDoc = "This is a report about...";
//
		// 삼성 프린터로 출력
		Printable prn = new SPrinterDriver();
		prn.print(myDoc);
//
		System.out.println();
//
		// LG 프린터로 출력
		prn = new LPrinterDriver();
		prn.print(myDoc);
//
	}
//
}
//
// code
//
class StringBuilderPrint {
    private static final StringBuilder print = new StringBuilder();
//    
    static void printAndReset(String str) {
        print.append(str);
        System.out.print(print);
        print.setLength(0);
    }
}
//
class SPrinterDriver implements Printable {
    SPrinterDriver() {
    }
    @Override
    public void print(String doc) {
        StringBuilderPrint.printAndReset(doc);
    }
}
class LPrinterDriver implements Printable {
    LPrinterDriver() {
    }
    @Override
    public void print(String doc) {
        StringBuilderPrint.printAndReset(doc);
    }
}
//
// print
//
This is a report about...
This is a report about...



2.아래의 인터페이스에 맞추어(상속하여) 아래를 프로그래밍 하시오.

Circle, Rectangle , Triangle
interface AreaGetable {
	public double getArea();
}
//
	public static void main(String[] args) {
//		
		AreaGetable area = new Circle(4);
		System.out.println(area.getArea());
//
		area = new Rectangle(4,5);
		System.out.println(area.getArea());
//		
		area = new Triangle(4,5);
		System.out.println(area.getArea());
	}
//
// code
//
class Circle implements AreaGetable {
    private double radius;
    Circle(double radius) {
        this.radius = radius;
    }
    Circle(int radius) {
        this.radius = (double)radius;
    }
    @Override
    public double getArea() {
        double area = Math.PI * Math.pow(this.radius, 2);
        return area;
    }
}
class Rectangle implements AreaGetable {
    private double length;
    private double height;
    Rectangle(double length, double height) {
        this.length = length;
        this.height = height;
    }
    @Override
    public double getArea() {
        double area = this.length * this.height;
        return area;
    }
}
class Triangle implements AreaGetable {
    private double base;
    private double height;
    Triangle(double base, double height) {
        this.base = base;
        this.height = height;
    }
    @Override
    public double getArea() {
        double area = this.base * this.height * 0.5;
        return area;
    }
}
//
// print
//
50.26548245743669
20.0
10.0



3. 디폴트 메소드에 대하여 설명하시오.

  • default method in interface is very similar with methods in parent class.
  • default methods need implementation(method body). Not like other public abstract methods in interface.
  • default methods can prevent compatibility errors when new methods in interface are updated.



4. 인터페이스를 상속하는 방법에 대하여 설명하시오.

  • child class can be inherited with "implements" keyword.
  • child interface can be inherited with "extends" keyword.
profile
Brave but clumsy

0개의 댓글