package yuanzhu;
import java.util.Scanner;
public class yuanzhu {
//Circle class Circle
private double radius;
public double getRadius() {
return radius; }
public void setRadius(double radius) {
this.radius = radius;
}
//Constructor without parameters
public yuanzhu(){
this .radius=0;
}
//Constructor with parameters
public yuanzhu(double r ){
this.radius=r;
}
//Get the area
public double getArea(){
double r=this.radius;
double area=r*r* Math.PI;
return area;
}
//Get the perimeter
public double getPerimeter(){
double perimeter=this.radius*2*Math.PI;
return perimeter;
}
//Print circle information
public void show(){
System.out.println("Please enter the radius of the circle");
Scanner sc=new Scanner(System.in);
this.setRadius(sc.nextInt());
System.out.println(" The radius of the circle"+this.getRadius());
System.out.println("Area of the circle"+this.getArea());
System.out.println("Perimeter of the circle"+this.getPerimeter()) ;
}
}
//Cylinder class
class Cylinder extends yuanzhu{
//Cylinder height
private double height;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//Construction method
public Cylinder (double r, double h){
super();
this.height=h;
this.setRadius(r);
}
public double getVolume( ) {
double volume=this.getArea()*this.height;
return volume;
}
//求体积
public void showVolume(){
System.out.println("圆柱的体积"+this.getVolume());
}
public static void main(String[] args) {
yuanzhu circle=new yuanzhu();
circle.show();
Cylinder cylinder=new Cylinder(2.0,8.5);
cylinder.showVolume();}
}
1: The role of this pointer: When using member variables in a method
• When passing the current object to other methods
2: A package cannot have two public classes at the same time.
3: extends represents inheritance
Class B extends A{} represents B inherits from A, and the public methods owned by A can be used directly in B, just like B's own.