I. Purpose
Inheritance (1) master class
Inheritance Inheritance and a cover (2) variables, methods, and to achieve coverage of overloading;
II. Content Experiment
(1) Circularity Circle class according to the following requirements.
1. Circle class member variable circular: radius represents the radius of the circle.
2. The members of the circle method of class Circle:
Circle():构造方法,将半径置0
Circle(double r):构造方法,创建Circle对象时将半径初始化为r
double getRadius():获得圆的半径值
double getPerimeter():获得圆的周长
void disp():将圆的半径和圆的周长,圆的面积输出到屏幕
(2) round the Circle class inherits the first question in the derived class cylinder Cylinder. Requirements are as follows:
1. Cylinder cylinder class member variables: height represents the height of the cylinder.
2. Cylinder cylinder class member methods:
Cylinder(double r,double h)构造方法,创建Cylinder对象时将圆半径初始化为r,圆柱体高初始化为h。
double getHeight():获得圆柱体的高
double getVol():获得圆柱体的体积
void dispVol():将圆柱体的体积输出到屏幕
Cylinder 3. The second class of problem, connected to the keyboard input as a high value and the radius of a circle of the cylinder, the calculated cylinder volume
1)
package text5;
public class Circle {
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Circle() {
this.radius=0;
}
public Circle(double radius) {
this.radius = radius;
}
public double getPerimeter() {
double perimeter=2*radius*3.1415926;
return perimeter;
}
public double getArea() {
double area=radius*radius*3.1415926;
return area;
}
public void disp() {
System.out.println("半径:"+radius);
System.out.println("周长:"+getPerimeter());
System.out.println("面积:"+getArea());
}
}
package text5;
public class Cylinder extends Circle{
private double height;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public double getVol() {
double vol=super.getArea()*height;
return vol;
}
public void disp() {
super.disp();
System.out.println("体积:"+getVol());
}
}
package text5;
public class Text5 {
public static void main(String[] args) {
Cylinder cyl1=new Cylinder(5,7);
cyl1.disp();
}
}
2) Run Screenshot
Lessons Learned
This put the National Day holiday week, mainly familiar with some general knowledge of last week's learning, such as:
subclass inherits the parent class