Title: inheritance
1????? inheritance
- ????? ?????? ????? ?????? ????? ?????? ?????
???????? ?? ?????? ??????. - ?????? ?????? ????? ???? ?? ????? ???? (parent
class, base class) - ?????? ?????? ????? ????? ?? ?? ??-????? (child
class, subcluss)
2?????
- ?????? ?????? ????? ?????? ??????. (??????
????? ?? ????? ?? ????). - ?????? ???? ?????? ?????? ?????? ????? ?? ????
?????? ?? ????? ?? ???? ???? ??????. - ????? ???? ???? (code reuse) ??? ?????????
??????? ?????? ??????. - ??? (???????) ?????? ????? ????? ?????? ?????? ??
????? ?????.
3???? ??????
this
????? ??
Field Y
Field X
Method X
super
????? ??
Field A
Field B
Method A
Method B
Method C
4Triangle.java
- public class Triangle implements Shape
- protected double a,b,c
- protected String color
-
- public Triangle(double a, double b, double c,
String color) - this.a a
- this.color color
- System.out.println("New triangle crerated\n"
- "Sides a"a" b"b" c"c)
-
- public double area()
- double s perimeter()/2
- System.out.println("Calculating the area\n"
- "Heron formula A sqrt(s(s-a)(s-b)(s-c))")
- return Math.sqrt(s(s-a)(s-b)(s-c))
-
- public double perimeter() return abc
5EquilateralTriangle.java
- public class EquilateralTriangle extends Triangle
- public EquilateralTriangle(double a, String
color) - super(a,a,a,color)
- System.out.println("New equilateral triangle
crerated\n" - "Side a"a)
-
-
- public double getAngle() return 60.0
-
- public double area()
- System.out.println("Calculating the area\n"
- "Equilateral formula A 0.5aasin(60deg)"
) - return 0.5bbMath.sin(Math.PI/3.0)
-
6?????
- ?????? ?????? ?????? ?? ????? ????? ???? "???
??..." - ????? ?????? protected ???? ?? ???? ?? ????? ????
?????? ??????? ?????? ?? ?? ??????? ????????. - ???? ?? ????? private ?? ???? ?????? ??????
?????. - ?????? ?????? ????? ????? (override) ????? ??
????? ?????. - ?????? ?????? ????? ?????? ????? ?? ???? ???
?????.
7????? ?-super
- ?????? ?????? ???? ????? ?? ?????? ?? ????? ?????
?? ?? ?? public. - ???? ?????? ?? ????? ????? ????? ???? ??????, ??
??? ???? ????? ?????? ?? ???? ????? ??? ?????
?????. - ????? super ????? ?????? ?????? ??? (??? this
?????? ?? ?????? ?? ????) ????? ?????? ?? ????
????? ?????? ?????. - super ???? ?????? ????? ??????? ?? ?????. ?? ????
????? ????? ?????? ???? ?????? super(). - ???? ?????? ?super- ?? ?? ?????. ??????
super.area()
8multiple inheritance
- ????? ????? ???? ????? ???? ?? ?? ????? ??? ?????
?????. - ???? ?????? ??? public ?? protected ?????? ??
?????? ??????. - ?????? ????? ????? (??? c), ????? ????? ??
????? ???? ???? ?????? ???. - ????????? ?????? ????? ??? ????? ???????.
9????? overriding
- ?????? ?????? ????? ????? (override) ???? ??
????? ?????, ?? ?????? ???? ???? ???? ?????
??????. - ???? ??????? ?? ?????? final ?? ????? ??????.
- ???? ????? ?? ????, ?? ?? ???? ????? ?????.
- ???? ??????? ??????? ?????? ?????? ?????? ?????
?"? super.
10?????? instanceof
- ????? ???????? ??????? true ????? ?-instance ??
????? ??? ??? ?? ????? ????. ?????? - Triangle tri1 new Triangle(1.0 , 1.0 , 1.0 ,
"Blue") - Triangle tri2 new EquilateralTriangle(2.0
, "Blue") - EquilateralTriangle tri3 new
EquilateralTriangle(3.0 , "Blue") -
- System.out.println(tri1 instanceof
Triangle) // true - System.out.println(tri3 instanceof
Triangle) // true - System.out.println(tri3 instanceof Shape)
// true - System.out.println(tri1 instanceof
EquilateralTriangle) // false - System.out.println(tri2 instanceof
EquilateralTriangle) // true
11?????? ?????????? (abstract classes)
- ????? ???????, ?????? ?????????? ????? ?????? ???
???????, ?????? ???? ?????? ?? ????. - ????? ????????? ??? ????? ??? ?? ???? ?????????
??? ?? ????. - ????? ?????? ????? ????????? ????? ????? ?? ??
?????? ??????????? ?????? ?? ???. ?? ???? ????
???, ???? ????? ?????? ??????????. - ??? instance ?????? ?????????, ???? ?????? ?????
??? ??? ????? ??????.
12Polygon.java
- public abstract class Polygon
- protected double sides
- protected double angles
- public Polygon()
-
- public Polygon(double sides, double
angles) - this.sides sides
- this.angles angles
-
-
- public abstract double area()
-
- public double perimeter()
- double s 0.0
- for (int i0 iltsides.length i)
- s sidesi
- return s
13EquilateralTriangle.java
- public class EquilateralTriangle extends Polygon
-
- public EquilateralTriangle(double a)
- sides new double3
- angles new double3
- sides0 sides1 sides2 a
- angles0 angles1 angles2
Math.PI/3.0 - System.out.println("New equilateral triangle
crerated\n" - "Side a"getSide(1))
-
-
- public double area()
- System.out.println("Calculating the area
(I'm an equilateral triangle)\n" - "Equilateral area formula A
0.5aasin(60deg)") - return 0.5getAngle(0)getAngle(0)Math.sin(g
etAngle(0)) -
14????? ?? ??????
- ?? ?????? ?????? ???? ?? ???? ??????.
- ???? ????? ???? ??? ???? ?? ?? ?? ??????
??????????? ???.
15??-??????? polymorphism
- ????? ?????? ???? ?? ????? ?????? ???? ?????? ??
?? ??? ????? ?? ??????, ?? ??? ???? ????? ??
?????? (??? "??????" ???? ?????? ?? "?????????")
. - ???? ?? ????? ?????? ????? ???? ??????. (???
"?????????" ???? ???? ?????? ?? "??????"). - ?? ?????? ??? ??? ?????? ???? ???? ???? ???
?????. - ?????? ?? ????? ??-???????.
- ????? ???? ???-??????? ???? ??? ????? ????? ?????
(??? ?????? ??????, ?????? ?????).
16??-??????? polymorphism
- ???? ?????? ???????? ???-???????
- ????????? ????? ???? ?? ?? ??? ??????. ?????????
?? ???? ???? ?? ??? ?-instance ???? ??????? ????
??? ???? ?????? ?? ???? ????. - ?????? ??????? ?? ????? ????????? ???? ???? ?????
??? ???? ????????. ????? ?? ???? ???? ?? ???????
(????? ???????) ??????? ?-casting. ????? ?? ????
?-casting ????? ?? ???? ????.
17Shapes.java
- public class Shapes
- public static void main(String args)
- Triangle tri1 new Triangle(1.0 , 1.0 , 1.0
, "Blue") - Triangle tri2 new EquilateralTriangle(2.0
, "Blue") - EquilateralTriangle tri3 new
EquilateralTriangle(3.0 , "Blue") - Triangle tri4 tri3
-
- tri1.area()
- tri2.area()
- tri3.area()
- tri4.area()
-
-
- EquilateralTriangle tri5
(EquilateralTriangle) tri2 - tri5.area()
- EquilateralTriangle tri6
(EquilateralTriangle) tri1 -
18???? ???-???????
- ???? ??? ????? ??? ???? ????? (widening
conversion) ????? ?"? ???? ????? (""). - ???? ????? ??? ??? ???? ??? (narrowing
conversion) ????? casting ?????. ??? ????? ?? ??
????? ????? ????? ?? ??? ???? ??. - ??? ??????? ???????? ???-??????? ????? ???? ????.
- ?? ?????? ??? ????? ???? ????? (?? ?????) ??
?????, ??? ????? ?? ??? ???? ?? ???? ????? ??
???? ??? (?? ?? ?????? ??? ???? ?????).
19???? ???-??????? (????)
- ??? ??? ?????? (????? ?????? ??????, ???? ??????
??????, ????? ?????? ??????) ????????? ???? ???
??? ?????? ??? ??? ??? ???? ????? ???????.
class A A() int a1 static int s
1 static int s() return s int a()
return a
class B extends A B()super() int
a2 static int s 2 static int s()
return s int a() return a
20????? ???? ?? ?????
A a1 new A(), b new B() B bb new
B() System.out.println("b.s()
"b.s()) System.out.println("bb.s()
"bb.s()) System.out.println("b.s
"b.s) System.out.println("bb.s
"bb.s) System.out.println("b.a
"b.a) System.out.println("b.a() "b.a())
?? ????? ? main ??? ?
21????? ???? ?? ?????
A a1 new A(), b new B() B bb new
B() System.out.println("b.s()
"b.s()) System.out.println("bb.s()
"bb.s()) System.out.println("b.s
"b.s) System.out.println("bb.s
"bb.s) System.out.println("b.a
"b.a) System.out.println("b.a() "b.a())
???? ?????
b.s() 1 bb.s() 2 b.s 1 bb.s 2 b.a
1 b.a() 2
22?????? Object
- ??? ?????? Java, ?? ????? ????? ????? ????? ????,
????? ?? Object. - ????? ???? ??? ?? Object ??? ????? ??? ?? ??
?????. - Object ???? ????? ????? ?????. ??????? ???? ??
toString() , clone() ?-equals(Object). - Object ??????? ?????? ???? ???-???????, ??????
23???? ???
- public class SmartArray
- protected Object ar
-
- public SmartArray(Object ar)
- this.ar ar
-
-
- public int in(int i,Object obj)
- if ((igt0) (iltar.length))
- ari obj
- return i
-
- else
- return -1
-
-
- public Object out(int i)
- if ((igt0) (iltar.length))
24???? ???
- public class PlayWithSmartArray
- public static void main(String args)
- Object obj "Yossi" , "Chaim" ,
"Nechama" - obj1 new EquilateralTriangle(6.0)
-
-
- SmartArray smart new SmartArray(obj)
- smart.findDuplications()
- smart.in(2,new EquilateralTriangle(6.0))
- smart.findDuplications()
- smart.in(2,smart.out(1))
- smart.findDuplications()
-
-
-
- New equilateral triangle crerated