LOGO inheritance 26112563 Nerissa Onkhum LOGO 26112563 LOGO

  • Slides: 70
Download presentation
LOGO การสบทอด (inheritance( 26/11/2563 Nerissa Onkhum

LOGO การสบทอด (inheritance( 26/11/2563 Nerissa Onkhum

LOGO ���� 26/11/2563

LOGO ���� 26/11/2563

LOGO ������ (Polymorphism ( 26/11/2563

LOGO ������ (Polymorphism ( 26/11/2563

LOGO ������� 26/11/2563

LOGO ������� 26/11/2563

������ public enum Color{ Red, Green, Blue } public class Shape { private Color

������ public enum Color{ Red, Green, Blue } public class Shape { private Color color; public Shape} () color = Color. Red ; { public double get. Area } () return 0 ; { public void set. Color(Color c } ( color = c; { public Color get. Color } () return color ; {

�������� public class Test. Shape { public static void main(String[] args} ( Shape s

�������� public class Test. Shape { public static void main(String[] args} ( Shape s 1 = new Shape; () System. out. println(s 1. get. Color; (() System. out. println(s 1. get. Area; (() Shape s 2 = new Shape; () s 2. set. Color(Color. Blue; ( System. out. println(s 2. get. Color; (() System. out. println(s 2. get. Area; (() } } 26/11/2563

�������� class Rectangle extends Shape} private double width; private double height; public Rectangle(int w,

�������� class Rectangle extends Shape} private double width; private double height; public Rectangle(int w, int h}( width = w ; height = h; { public double get. Width}() return width; { public double get. Hight}() //��������� } }

�������� class Rectangle extends Shape} public double get. Area} () return width * height;

�������� class Rectangle extends Shape} public double get. Area} () return width * height; { … { 26/11/2563

�������� class Rectangle extends Shape} public String to. String}() String str = “Rectangle; ”

�������� class Rectangle extends Shape} public String to. String}() String str = “Rectangle; ” str += “color = “ +get. Color; () str += “width = “ +width; str += “height = “ +height; str += “area = “ +get. Area; () return str; { {

�������� class Test. Shape 1 { public static void main(String[] args) { Rectangle r

�������� class Test. Shape 1 { public static void main(String[] args) { Rectangle r = new Rectangle(3, 4); System. out. println(r. to. String()); } } 26/11/2563

LOGO ���� Object 26/11/2563

LOGO ���� Object 26/11/2563

เมธอด to. String () public class Rectangle extends Shape {. . . public String

เมธอด to. String () public class Rectangle extends Shape {. . . public String to. String() { String str = "Rectangle"; str += " color=" + get. Color(); str += " width=" + width; str += " height=" + height; str += " area=" + get. Area(); return str; } } 26/11/2563

����� equals() public boolean equals(Object other. Object) { if (other. Object instanceof Rectangle) {

����� equals() public boolean equals(Object other. Object) { if (other. Object instanceof Rectangle) { Rectangle other. Rect = (Rectangle) other. Object; boolean equal. Width = width == other. Rect. width; boolean equal. Height = height == other. Rect. height; return equal. Width && equal. Height; { return false; { 26/11/2563

����� hash. Code() v ������������������������ public int hash. Code() { return (int) (width +

����� hash. Code() v ������������������������ public int hash. Code() { return (int) (width + height * 17); } 26/11/2563

����� clone() ��������������� public Object clone() { Rectangle clone = new Rectangle(width, height); return

����� clone() ��������������� public Object clone() { Rectangle clone = new Rectangle(width, height); return clone; } 26/11/2563

��������� �� Object public class Test. Rectangle 3 { public static void main(String[] args}

��������� �� Object public class Test. Rectangle 3 { public static void main(String[] args} ( Rectangle r 1 = new Rectangle(2, 5; ( System. out. println(r 1. hash. Code; (() Rectangle r 2 = new Rectangle(5, 2; ( System. out. println(r 1. equals)r 2; (( Rectangle r 3 = (Rectangle) r 1. clone; () System. out. println(r 1. equals(r 3; (( { { 26/11/2563

�������� Public class Rectangle extends Shape{ public Rectangle(double w, double h){ …. } }

�������� Public class Rectangle extends Shape{ public Rectangle(double w, double h){ …. } } public class Square extends Rectangle { public Square( double w) { super(w, w); ������ } �������. . . �� Rectangle }

คลาสสเหลยมจตรส public class Square extends Rectangle { public Square(double w){ super(w, w) } public

คลาสสเหลยมจตรส public class Square extends Rectangle { public Square(double w){ super(w, w) } public String to. String() { String str = "Rectangle"; str += " color=" + get. Color(); str += " width=" + get. Width(); str += " area=" + get. Area(); return str; } } 26/11/2563 ������ Rectangle ����� Shape ���������� Rectangle

LOGO ������� 26/11/2563

LOGO ������� 26/11/2563

������ v ����������� public final class Square ����� Public class Square. Child extends Square

������ v ����������� public final class Square ����� Public class Square. Child extends Square 26/11/2563 ������

final class vคลาสในแพจเกจ java. lang*. § Final class เชน • String. Buffer • Math

final class vคลาสในแพจเกจ java. lang*. § Final class เชน • String. Buffer • Math • System • Integer • Double

����� v����������� public class Shape} public final Color get. Color} () return color; {.

����� v����������� public class Shape} public final Color get. Color} () return color; {. . . { public class Rectangle extends Shape} public Color get. Color(){ return Color. Blue; } } 26/11/2563

LOGO ������ protected 26/11/2563

LOGO ������ protected 26/11/2563

����� public class Circle extends Shape} protected double radius; public Circle(double r} ( radius

����� public class Circle extends Shape} protected double radius; public Circle(double r} ( radius = r; { public double get. Radius} () return radius; { public double get. Area}() return Math. PI* radius; { {

��������� public class Ring 1 extends Circle} public double get. Area} () double outer.

��������� public class Ring 1 extends Circle} public double get. Area} () double outer. Area = Math. PI * radius ; double inner. Area = Math. PI * inner. Radius ; return outer. Area - inner. Area; {. . . { 26/11/2563

��������� public class Ring 2 extends Circle} public double get. Area} () double outer.

��������� public class Ring 2 extends Circle} public double get. Area} () double outer. Area = super. get. Area; () double inner. Area = Math. PI * inner. Radius ; return outer. Area - inner. Area; {. . . { 26/11/2563

LOGO ������� (abstract class( 26/11/2563

LOGO ������� (abstract class( 26/11/2563

����� abstract class v ������������ Shape s = new Shape; () v ������������ Shape

����� abstract class v ������������ Shape s = new Shape; () v ������������ Shape s; s = new Rectangle(3, 2; ( s = new Square(5; ( 26/11/2563

������� public class Test. Polymorphism { public static void main(String[] args} ( Shape shape;

������� public class Test. Polymorphism { public static void main(String[] args} ( Shape shape; shape = new Rectangle(10, 20; ( System. out. println(shape. get. Area; (() shape = new Circle(10; ( System. out. println(shape. get. Area; (() { { 26/11/2563

������� public class Shape. Array} public static double sum. Area(Shape[] shapes} ( double sum

������� public class Shape. Array} public static double sum. Area(Shape[] shapes} ( double sum = 0; for( Shape s : shapes} ( sum += s. get. Area; () { return sum; { { 26/11/2563

������� Shape[] s = new Shape[3]; s[0] = new Triangle(10); s[1] = new Rectangle(10,

������� Shape[] s = new Shape[3]; s[0] = new Triangle(10); s[1] = new Rectangle(10, 20); s[2] = new Pentagon(20); double d = Shape. Array. sum. Area(s); 26/11/2563

LOGO ������� 26/11/2563

LOGO ������� 26/11/2563

LOGO ���� 26/11/2563

LOGO ���� 26/11/2563

LOGO 26/11/2563

LOGO 26/11/2563