day13

Run Settings
LanguageJava
Language Version
Run Command
package day13; class Point2D{ int x; int y; void pointPrint(){ System.out.println("["+x+","+y+"]"); } public Point2D(){ System.out.println("2D생성"); } } class Point3D1{ // 상속없이 3d로 확장할려면 int x; int y; int z; void pointPrint(){ System.out.println("["+x+","+y+"]"); } } class Point3D extends Point2D { // 부모와 자식은 별도의 객체임. int z; public Point3D(){ super(); // 이게 저절로 됌 // this와 비슷한 느낌. 슈퍼함수:부모의 생성자 / 슈퍼 System.out.println("3D생성"); } } public class Ex01 { public static void main(String[] args) { Point3D pos1 = new Point3D(); pos1.x = 10; pos1.y = 20; pos1.z = 30; pos1.pointPrint(); } }
package day13; class A02 //extends Object //자바의 모든 클래스는 Object클래스를 상속받아 정의된다. { int x; int y; public String toString(){ // 이게 오버라이딩! // 원래 부모에서 toString은 객체식별기능. return "["+x+","+y+"]"; // 여기서는 x,y 좌표 출력 기능으로 재정의. } public boolean equals(Object o){ //return this == o ? true : false; //오브젝트에 정의된 equals의 정의 // (Object o):자바의 모든 객체를 받을 수있다. A02 tmp = (A02)o; return ( (tmp.x == this.x) && (tmp.y == this.y) ? true : false ); // 이렇게 재정의해야 true 나온다. } } public class Ex02 { public static void main(String[] args) { A02 ob1 = new A02(); ob1.x = 10; ob1.y = 20; System.out.println(ob1.toString()); System.out.println(ob1); System.out.println(ob1.hashCode()); System.out.println(); A02 ob2 = new A02(); ob2.x = 10; ob2.y = 20; System.out.println(ob2.toString()); System.out.println(ob2); System.out.println(ob2.hashCode()); System.out.println(); //ob1, ob2가 내용은 같으나, 참조값이 다름. System.out.println(ob1 == ob2); System.out.println(ob1.equals(ob2)); // 재정의하지않으면 false 나옴. System.out.println(); String str = "hello"; System.out.println("hello" == str); System.out.println("hello".equals(str)); System.out.println("hello".hashCode()); // 해시코드가 같다 System.out.println(str.hashCode()); // 해시코드가 같다 System.out.println(); A02 ob3 = new A02(); System.out.println(ob3.toString()); System.out.println(ob3); System.out.println(ob3.hashCode()); } }
package day13; class A03{ void test1(){ System.out.println("부모꺼"); } void method(){ System.out.println("부모꺼"); } } class A03Sub extends A03{ void test2(){ System.out.println("부모꺼"); } void method(){ System.out.println("자식꺼"); } } public class Ex03 { public static void main(String[] args) { A03 ob = new A03(); ob.method(); A03Sub ob1 = new A03Sub(); ob1.method(); A03 tmp = ob1; //업캐스팅 System.out.println(tmp); System.out.println(ob1); tmp.method(); //A03Sub tmp1 = tmp; A03Sub tmp1 = (A03Sub)tmp; //다운캐스팅 tmp1.test2(); } }
Editor Settings
Theme
Key bindings
Full width
Lines