small improvements on tutorial 5

This commit is contained in:
Marcus Vinicius de Carvalho 2024-10-22 10:15:20 +08:00
parent 53e70432fe
commit 68d4c9b733
3 changed files with 59 additions and 9 deletions

View File

@ -1,5 +1,5 @@
public abstract class Polygon { //public abstract class Polygon {
//public class Polygon { public class Polygon {
public enum KindofPolygon { public enum KindofPolygon {
POLY_PLAIN, POLY_PLAIN,
POLY_RECT, POLY_RECT,
@ -29,14 +29,45 @@ public abstract class Polygon {
return name; return name;
} }
// public float calArea() { public float calArea() {
// return 0; return 0;
// } }
public void printWidthHeight( ) { public void printWidthHeight( ) {
System.out.println("Width = " + width + " Height = " + height); System.out.println("Width = " + width + " Height = " + height);
} }
public abstract float calArea();
// public abstract float calArea();
} }

View File

@ -38,6 +38,25 @@ public class TestPolygon {
public static void printArea(Polygon poly) { public static void printArea(Polygon poly) {
@ -48,11 +67,11 @@ public class TestPolygon {
public static void main2(String[] args ) { public static void main2(String[] args ) {
Rectangle rect = new Rectangle("Rectangle", 3.0f, 4.0f); Rectangle rect = new Rectangle("Rectangle", 3.0f, 4.0f);
printArea(rect); printArea(rect); // dynamic binding
rect.printWidthHeight(); rect.printWidthHeight();
Triangle triangle= new Triangle("Triangle", 3.0f, 4.0f); Triangle triangle= new Triangle("Triangle", 3.0f, 4.0f);
printArea(triangle); printArea(triangle); // dynamic binding
triangle.printWidthHeight(); triangle.printWidthHeight();
} }
} }