Transcript ((A)this).x

Inheritance
การสื บทอดคลาส
การนา Code กลับนามาใช้ ใหม่
• การนา Code กลับนามาใช้ใหม่ คือ การใช้ Code ที่มีอยูแ่ ล้ว
มาสร้างคลาสใหม่ซ่ ึ งอาจจะเป็ นการสื บทอดจากคลาสหลัก
(Inheritance) ที่ได้นามาเป็ นส่ วนประกอบคลาสใหม่ทสี่ ร้าง
• การสื บทอดจะเป็ นการนา Code เดิมกลับมาใช้อีกครั้ง โดยที่
เราไม่ตอ้ งเขียนใหม่
• ทาให้ประหยัดเวลาและถ้า Code นั้นได้มีการตรวจสอบ
ข้อบกพร่ องอย่างดีแล้วยิง่ ทาให้ช่วยลดเวลาในการตรวจสอบ
Code อีกครั้ง
การนาคลาสกลับมาใช้อาจจาแนกได้เป็ น 4 วิธี คือ
1.
2.
3.
4.
นาโปรแกรมที่เขียนเก็บไว้มาใช้
เขียนใหม่จากข้อมูลที่มีอยู่
นาคลาสมาใช้ในลักษณะ Composition
นาคลาสกลับมาใช้ภายใต้การสื บทอดคุณสมบัติ (Inheritance)
การสื บทอดคุณสมบัติ (Inheritance)
• คือ การใช้คลาสที่มีอยูแ่ ล้วมาสร้างเป็ นคลาสใหม่ (คลาสย่อย)
โดยการสื บทอด วิธีการนี้ เราสามารถใช้คุณสมบัติต่างๆของคลาส
เดิม เช่น ตัวแปร เมธอด คอนสตักเตอร์ หรื ออื่นๆ ทั้งหมดที่
คลาสเดิมมีอยูแ่ ล้วได้ โดยไม่ตอ้ งมีการแก้ไขใดๆ รวมทั้งยัง
สามารถเพิ่มเติมคุณสมบัติใหม่ๆ ที่คลาสเดิมไม่มีได้อีกด้วย
• เราเรี ยกคลาสที่มีอยูว่ า่ คลาสหลัก (Superclass) และคลาส
ใหม่ที่สืบทอดว่า คลาสสื บทอด หรื อ คลาสลูก (Subclass)
การถ่ ายทอดแบบลาดับชั้น (Hierarchical Class)
- การสื บทอดไม่ได้จากัดเพียงระหว่าง superclass และ
subclass เท่านั้น
- sub class สามารถเป็ น superclass ของ class อีกหนึ่งได้
- การเขียนแผนภาพแสดงความสัมพันธ์ นิยมเขียนเป็ น
แผนผังแบบต้นไม้ (Tree)
แผนผังแบบต้ นไม้ (Tree)
Class สัตว์
Class สัตว์เลื้อยคลาน
Class สัตว์บก
Class สัตว์น้ า
Class สัตว์ปีก
การ Inheritance สามารถทาได้ 2 ลักษณะ คือ
1. Single Inheritance คือ การที่ subclass ได้รับการ
ถ่ายทอดคุณสมบัติมาจาก Superclass เพียงคลาสเดียวClass
A และ Class B
Class A
Class B
2. Multiple Inheritance คือการที่ subclass ได้รับ
การถ่ายทอดคุณสมบัติต่างๆ มาจาก Superclass มากกว่า 1 คลาส
Class B
Class A
Class C
หลักในการสร้ างการสื บทอด
- สร้าง class หลักขึ้นมา 1 class แล้วกาหนดให้มีคุณสมบัติต่างๆ
ซึ่งเป็ นคุณสมบัติโดยรวม ที่ class อื่นๆ จาเป็ นต้องมี
- จากนั้ นจึ ง สร้ า ง class คลาสอื่ น ขึ้ นมา เพื่ อ รั บ การถ่ า ยทอด
คุ ณ สมบัติ ท้ ัง หมดจาก class หลัก มาโดยอัต โนมัติ โดยไม่ ต้อ ง
สร้างขึ้นใหม่
- สามารถสร้างคุณสมบัติอื่นๆ นอกเหนื อจาก class หลักขึ้นมาเอง
ได้
การสร้ าง Superclass และ Subclass
• Superclass จะสร้างเหมือนกับการสร้าง Class ทัว่ ไป แต่การ
สร้าง Subclass จะระบุคาว่า “extends” ร่ วมด้วยเพื่อให้ทราบว่า
Subclass จะใช้คุณสมบัติจาก Superclass ชื่ออะไร
• รู ปแบบดังนี้
class subclass_name extends ชื่อ superclass_name
• subclass_name คือ ชื่อของคลาสใหม่หรื อคลาสลูก
• superclass_name คือ ชื่อของคลาสที่สืบทอดหรื อคลาสแม่
การสร้ าง Superclass และ Subclass (ต่ อ)
• class A { int x ; }
class B extends A { int y ; }
ทาการสร้ าง instance จากคลาสทั้ง 2
A a = new A() ;
B b= new B() ;
Example Superclass 1
class A{
public class inheritance{
void printA(){
public
static
void
main(String[]
args)
{
System.out.println("A");
A m = new A();
}
B n = new B();
A
}
m.printA();
A
class B extends A{
n.printA();
B
void printB() {
n.printB();
System.out.println("B");
}
}
}
}
Example Superclass 2
class A{
int x = 1;
}
class B extends A{
int y = 3;
}
class C extends B{
int z = 4;
}
public class inheritance1{
public static void main(String[] args) {
A o = new A();
1
System.out.println (0.x);
1,3
B p = new B();
System.out.println (p.x+ “,”+p.y); 8
C m = new C();
System.out.println (m.x+m.y+m.z);
}
}
การป้ องกันการเรียกใช้ ภายใน Superclass
การเข้ าถึงด้ วย access modifier
• public ระบุที่ method หรื อ variable แสดงว่า class ใดๆ
สามารถ access ได้
• private ระบุที่ method หรื อ variable สามารถใช้ access ได้
เพียงใน class นั้นเท่านั้น subclass ไม่สามารถ access ได้
• protected method หรื อ variable สามารถ access เมื่อเป็ น
สมาชิกของ class หรื อ subclass
การป้ องกันการเรียกใช้ ภายใน Superclass (ต่ อ)
Example 1
class A{
protected int x = 10;
public class Test{
private int w = 5;
public static void main(String[] args) {
}
B m = new B();
m.Mul();
class B extends A{
}
int y;
}
void Mul() {
y = x*w;
Compile Error / private
System.out.println (y);
}
}
การใช้ keyword super
ใช้สาหรับการทางานใน Subclass ซึ่งมีประโยชน์ 2 ประการ คือ
1. เพื่อเรี ยกใช้สมาชิกของ Subclass และ Superclass ที่ใช้ชื่อ
สมาชิกเดียวกัน
รูปแบบ
super.member
เช่น
super.x=x;
super.r=r;
การใช้ super เรียกสมาชิกของ Superclass
public class super1{
class A{ int x; }
public static void main(String[] args) {
class B extends A{
B m = new B(5,8);
int x;
m.show();
B(int a,int b) {
}
super.x = a;
}
x = b;
x in superclass : 5
}
x in subclass : 8
void show(){
System.out.println("x in superclass : "+super.x);
System.out.println("x in subclass : "+x);
}
}
การใช้ keyword super (ต่ อ)
2. Subclass สามารถเรี ยกใช้ Constructor Method ของ Superclass
โดยไม่จาเป็ นต้องสร้าง Constructor ของตนเองขึ้นมาใหม่
(Constructor จะไม่ถูกสื บทอดจากการ inherit ด้วย extends)
รูปแบบ
super(argument)
เช่น
super(r)
การใช้ super เรียก Constructorของ Superclass1
class A{
A(String s){System.out.println(s);}
}
class B extends A{
B() {
super("Hello");
System.out.println("Good Boy");
}
}
public class super_constru{
public static void main(String[] args)
{
B m = new B();
}
}
Hello
Good Boy
การใช้ super เรียก Constructorของ Superclass2
class A{
int a,b;
A(int a, int b){
this.a = a;
this.b = b;
}
}
class B extends A{
int c;
B(int c) {
super(0,0);
this.c = c;
}
}
public class super_constru2{
public static void main(String[] args)
{
B m = new B(5);
System.out.println("a = "+m.a+ " b = " +m.b+
c = "+m.c);
}
}
a=0 b=0 c=5
"
Inheritance Assessments
Refer a difference variable from class C
class A {
int x;
..
}
class B extends A {
int x;
..
}
class C extends B {
int x;
..
}
x
x in class C
this.x
x in class C
super.x
x in class B
((B)this).x
x in class B
((A)this).x
x in class A
super.super.x
x in class A
Inheritance Solutions
A
C
B
((A)this).x
B
C
A
[x]
[x]
[x]
Class Diagram
Memory Space
((B)this).x
this.x
Shadowing
• ถ้าเรากาหนด data member ในคลาสลูกมีชื่อเหมือนกับ data
member ในคลาสแม่ ชื่อของลูกจะบัง(shadow)ชื่อในคลาสแม่
class A {int x = 1;}
class B extends A {float x = 2.0f;}
class shadowing {
public static void main(String args[]) {
B b = new B();
A c = new A();
System.out.println(b.x);
System.out.println(c.x);
}
}
2.0
1
แบบฝึ กหัด
Point2D
x,y : int
Point2D(x:int,y:int)
Point2D(x:int)
Point2D()
Point3D
Test2D3D
Main():void
z : int
Point3D(x:int,y:int,z:int)
Point3D(z:int)
Point3D()
• Class Point2D
- Point2D(x:int,y:int) ใช้ set ค่า x y
- Point2D(x:int) เรี ยก constructor Point2D(x:int,y:int) มาใช้
กาหนดค่า เป็ น (x,0);
- Point2D() เรี ยก constructor Point2D(x:int,y:int) มาใช้
กาหนดค่าเป็ น (0,0)
• Class Point3D
- Extends จาก Point2D
- Point3D(x:int,y:int,z:int) ใช้ set ค่า x y z (x,y เรี ยกใช้
constructor จาก Point2D (x:int,y:int)
- Point3D(z:int) ใช้ set ค่า z และเรี ยก ใช้ constructor จาก
Point2D(x:int,y:int) กาหนดค่า เป็ น (0,0);
- Point3D() เรี ยก constructor Point3D(x:int,y:int,z:int) มาใช้
กาหนดค่าเป็ น (0,0,0)
ผล RUN
x=0y=0
x=5y=0
x = 5 y = 10
x=0y=0z=0
x = 0 y = 0 z = 15
x = 20 y = 30 z = 4