PROGRAMMING:Jmu-java-03 object oriented basics-04-shape-inheritance
#Preface
In the previous topic "shape", we see that in order to output the perimeter and area of all shapes, we need to create multiple arrays for multiple loops. This experiment uses inheritance and polymorphism to improve our design.
#Description:
1. Define abstract class ` shape`
**Property: * * immutable static constant 'double Pi', value is' 3.14 ',
**Abstract methods: * * ` public double getperimeter() ', ` public double getarea ()`
2. Both 'rectangle' and 'circle' classes inherit from the shape class.
Rectangle class (* * attribute: * * int width, length), circle class (* * attribute: * * int radius).
The constructors with parameters are ` rectangle (int width, int length) ` and ` circle (int radius) ` respectively.
`ToString 'method (generated automatically by eclipse)
3. Write the 'double sumallarea' method to calculate and return the area and * * and * of all objects in the * * incoming shape array
`The double sumallperimeter 'method calculates and returns the circumference and * * of all objects in the * * passed in shape array.
4 * * main method**
4.1 enter the integer value n, and then create n different shapes. If you enter rect, enter width and length in turn. If you enter CIR, enter the radius.
4.2 then output the sum of perimeter and area of all shapes. And output all the shape information in the format of sample** Tip: * use 'arrays. ToString'.
4.3 finally, output the type and parent type of each shape. Use a method similar to 'shape. Getclass()' / / to get the type and 'shape. Getclass(). Getsuperclass()' / / to get the parent type;
**Note: * * when using a mixture of 'nextint' and 'nextline' in processing input, you should pay attention to the carriage return at the end of the line.
#Thinking
1. Which class do you think is more suitable for sumallarea and sumallperimeter?
2. Should it be declared static?
###Input example:
```in
four
rect
3 1
rect
1 5
cir
one
cir
two
```
###Output example:
```out
thirty-eight point eight four
twenty-three point seven zero zero zero zero zero zero zero zero zero zero zero zero zero three
[Rectangle [width=3, length=1], Rectangle [width=1, length=5], Circle [radius=1], Circle [radius=2]]
class Rectangle,class Shape
class Rectangle,class Shape
class Circle,class Shape
class Circle,class Shape
```
answer:If there is no answer, please comment
In the previous topic "shape", we see that in order to output the perimeter and area of all shapes, we need to create multiple arrays for multiple loops. This experiment uses inheritance and polymorphism to improve our design.
#Description:
1. Define abstract class ` shape`
**Property: * * immutable static constant 'double Pi', value is' 3.14 ',
**Abstract methods: * * ` public double getperimeter() ', ` public double getarea ()`
2. Both 'rectangle' and 'circle' classes inherit from the shape class.
Rectangle class (* * attribute: * * int width, length), circle class (* * attribute: * * int radius).
The constructors with parameters are ` rectangle (int width, int length) ` and ` circle (int radius) ` respectively.
`ToString 'method (generated automatically by eclipse)
3. Write the 'double sumallarea' method to calculate and return the area and * * and * of all objects in the * * incoming shape array
`The double sumallperimeter 'method calculates and returns the circumference and * * of all objects in the * * passed in shape array.
4 * * main method**
4.1 enter the integer value n, and then create n different shapes. If you enter rect, enter width and length in turn. If you enter CIR, enter the radius.
4.2 then output the sum of perimeter and area of all shapes. And output all the shape information in the format of sample** Tip: * use 'arrays. ToString'.
4.3 finally, output the type and parent type of each shape. Use a method similar to 'shape. Getclass()' / / to get the type and 'shape. Getclass(). Getsuperclass()' / / to get the parent type;
**Note: * * when using a mixture of 'nextint' and 'nextline' in processing input, you should pay attention to the carriage return at the end of the line.
#Thinking
1. Which class do you think is more suitable for sumallarea and sumallperimeter?
2. Should it be declared static?
###Input example:
```in
four
rect
3 1
rect
1 5
cir
one
cir
two
```
###Output example:
```out
thirty-eight point eight four
twenty-three point seven zero zero zero zero zero zero zero zero zero zero zero zero zero three
[Rectangle [width=3, length=1], Rectangle [width=1, length=5], Circle [radius=1], Circle [radius=2]]
class Rectangle,class Shape
class Rectangle,class Shape
class Circle,class Shape
class Circle,class Shape
```
answer:If there is no answer, please comment