Interface java - rectangular computing, the idea after learning

Disclaimer: you can Oh, ha ha ha ha https://blog.csdn.net/qq_44084157/article/details/90754477

And computing circles as to build a Shape interface, and write in it, and round the same code as the interface, where you can directly round Shape, simply create two categories, namely, Rectangle, TestRectangle.
1.Shape Interface
package lesson001.jiekou;

interface the Shape {public
Double the PI = 3.1415926;
Double Circumference (); // calculate the perimeter
double area (); // calculate circle
}

2.Rectangle inside the class, where a lot of code appears, in fact, as long as the write can occur g Double getWidth public () {
return width;
}
else too. It is noted that the back part be returned into the corresponding calculation formula. c c rectangular formula, and the formula s.

package lesson001.jiekou;

public class Rectangle implements Shape {
private double width;
private double heigth;

public double getWidth() {
    return width;
}

public void setWidth(double width) {

    this.width = width;
}

public double getHeigth() {
    return heigth;
}

public void setHeigth(double heigth) {

    this.heigth = heigth;
}

@Override
public double circumference() {
    return (width + heigth) * 2;
}

@Override
public double area() {
    return  width * heigth;
}

}

3. TestRectangle class, The New inside a newly defined class a, b assigned to the corresponding width, heigth.
package lesson001.jiekou;

import lesson001.jiekou.Rectangle;

import java.util.Scanner;
public class TestRectangle {
public static void main(String[] args) {

    //声明部分
    double a,b,c,s;
    Scanner sc = new Scanner(System.in);
    Rectangle rectangle = new Rectangle();


   System.out.print("a= ");
   a =sc.nextDouble();
   System.out.print("b= ");
   b= sc.nextDouble();

    rectangle.setWidth(a);
    rectangle.setHeigth(b);


    c = rectangle.circumference();
    s =rectangle.area();

    System.out.println("c= " + c);
    System.out.println("s = " + s);

}

}

The content is not too difficult to learn, more practice you can remember and understand.
There is a teacher too fast, but fortunately teacher notes to us, or do not know anything

Guess you like

Origin blog.csdn.net/qq_44084157/article/details/90754477