출처 : https://luckyguystory.tistory.com/16
class A{
}
public class Test {
public static void main(String[] args) {
A a = new A();
System.out.println(a);
}
}
Class A안에 toString이 없어서....?
class final -> 상속 불가능하게 만듦.
Method final -> 오버라이딩 불가능하게 만듦.
출처 : https://luckyguystory.tistory.com/16
interface Printable { // MS가 정의하고 제공한 인터페이스
public void print(String doc);
}
//SPrinterDriver 와 LPrinterDriver를 만드시오
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);
}
/*
출력:
From Samsung printer
This is a report about ...
From LG printer
This is a report about ...
*/
interface Printable{ // MS가 정의하고 제공한 인터페이스
public void print(String doc);
}
class SPrinterDriver implements Printable{
@Override
public void print(String doc) {
System.out.println("From Samsung printer");
System.out.println(doc);
}
}
class LprinterDriver implements Printable{
@Override
public void print(String doc) {
System.out.println("From LG printer");
System.out.println(doc);
}
}
public class PrintDriver {
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);
}
}
출처 : https://luckyguystory.tistory.com/16
출처 : https://luckyguystory.tistory.com/16
출처 : https://luckyguystory.tistory.com/16
출처 : https://luckyguystory.tistory.com/16
Object obj = new Circle(10);
System.out.println(obj); //출력: 넓이는 100 입니다. (예시)
class Shape{
public double getArea() {
return 0.0;
}
}
class Circle3 extends Shape{
private int r;
public Circle3(int r) {
this.r=r;
}
@Override
public double getArea() {
return r*r*Math.PI;
}
@Override
public String toString() {
return "넓이는 " + getArea() + "입니다.";
}
}
public class Circle {
public static void main(String[] args) {
Object obj = new Circle3(10);
System.out.println(obj);
}
}