LOGO inheritance 1232564 Nerissa Onkhum LOGO 1232564 LOGO

  • Slides: 70
Download presentation
LOGO การสบทอด (inheritance( 12/3/2564 Nerissa Onkhum

LOGO การสบทอด (inheritance( 12/3/2564 Nerissa Onkhum

LOGO ���� 12/3/2564

LOGO ���� 12/3/2564

LOGO ������ (Polymorphism ( 12/3/2564

LOGO ������ (Polymorphism ( 12/3/2564

������ 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; (() } } 12/3/2564

�������� 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; { … { 12/3/2564

�������� 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()); } } 12/3/2564

LOGO ���� Object 12/3/2564

LOGO ���� Object 12/3/2564

เมธอด 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; } } 12/3/2564

����� 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; { 12/3/2564

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

����� clone() ��������������� public Object clone() { Rectangle clone = new Rectangle(width, height); return clone; } 12/3/2564

��������� �� 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; (( { { 12/3/2564

�������� 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; } } 12/3/2564 ������ Rectangle ����� Shape ���������� Rectangle

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

������ v ����������� public final class Square ����� Public class Square. Child extends Square 12/3/2564 ������

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; } } 12/3/2564

LOGO ������ protected 12/3/2564

LOGO ������ protected 12/3/2564

����� 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; {. . . { 12/3/2564

��������� 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; {. . . { 12/3/2564

LOGO ������� (abstract class( 12/3/2564

LOGO ������� (abstract class( 12/3/2564

����� 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; ( 12/3/2564

������� 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; (() { { 12/3/2564

������� 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; { { 12/3/2564

������� 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); 12/3/2564

LOGO ���� 12/3/2564

LOGO ���� 12/3/2564

LOGO 12/3/2564

LOGO 12/3/2564