Say i wanted to create a variety of shapes consisting of a line, circle, square etc
For the line, i ask for 4 coordinates (x,y) (x,y);
I then have a different class called point which must contain
2 fields: int x, y.
It must have a constructor to initialize its field's values and the required get methods to read those values.
Shape which is an abstract class to be extended by all shapes. It should contain the signature of all the methods that a shape must have. The implementation of each method may be different in each class that extends this abstract class.
then another class Line which is a shape (extends Shape), it should have these fields:
- String TYPE = "Line"
- MyPoint p1,p2
- get methods for class fields such as getP1()
- Length() : calculates the length of a line returning a double rounded to two decimal points
- Area() : which will return a the value zero as double
- renderShape(Graphics2D g2d) : used to draw the line to the screen. This method is provided for you, no need to edit it.
- printDetails(): will print the shape details , check typical input/output
I am just wandering how you would be able to get the coordinates from the original class and print them out in the Line class, thanks :) Smells like homework, not work.
以上就是java using abstract classes的详细内容，更多请关注web前端其它相关文章！