1. Object 클래스란?
2. 아래의 소스코드에 대하여 아래와 같이 출력되는 이유는?
출력
//
A@28a418fc
//
==============
class A{
}
public class Test {
public static void main(String[] args) {
A a = new A();
System.out.println(a);
}
}
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
- "class Object" is parent class of "class A"
- So "a" went to "println(Object x)" as a parameter Object.
- println(a)
- "println(a)" does "print(s)".
- s = String.valueOf(a)
- a != null
- So "a.toString()".
- "a.toString()" does "return" A@28a418fc
3. class이름 및 함수에서 final의 의미는?
- final class {}: The class does not inherit.
- final method(): The method is not overridden.
4. @Override 에 대하여 설명하시오.
- Annotation to refer the method overrides other same name method in parent class.
5.아래의 결과가 나오도록 프로그래밍 하시오. (개별진척도 1번문제)
public static void main(String[] args) {
Object obj = new Circle(10);
System.out.println(obj);
}
class Const {
Const() {
}
static final double PI = 3.14134;
}
class Print {
private static StringBuilder print = new StringBuilder();
Print() {
}
static void area(double area) {
print.append("넓이는 ");
print.append(area);
print.append(" 입니다.");
}
static StringBuilder getPrint() {
return print;
}
}
class Circle {
double radius;
Circle(int radius) {
super();
this.radius = (double)radius;
}
Circle(double radius) {
super();
this.radius = radius;
}
private double getArea() {
return (Const.PI * Math.pow(this.radius, 2));
}
@Override
public String toString() {
Print.area(getArea());
return Print.getPrint().toString();
}
}
class CircleMain {
CircleMain() {
}
public static void main(String[] args) {
Object obj = new Circle(10);
System.out.println(obj);
}
}
//
// print
//
넓이는 314.134 입니다.