⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex3.java

📁 讲述各种各样的java初始编程 了解编程
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package chenchao3;import java.util.ArrayList;/** * * @author williechen */public class Ex3 {    ArrayList<Shape> shapes = new ArrayList<Shape>();    public Ex3() {        Rectangle rect = new Rectangle(3, 4);        Circle circle = new Circle(3);        Triangle tri = new Triangle(3,4,5);               shapes.add(rect);        shapes.add(circle);         shapes.add(tri);       }        public void test(){        for(Shape s : shapes)        {            System.out.println(s.getClass().getName());            System.out.println("Area is :" + s.area());            System.out.println("Length is :" + s.circumference());        }    }        public static void main(String[] args) {        Ex3 test = new Ex3();        test.test();    }}abstract class Shape {    public abstract double area();            // Abstract methods: note    public abstract double circumference();   // semicolon instead of body. }class Circle extends Shape {    public static final double PI = 3.14159265358979323846;    protected double r;                              // Instance data    public Circle(double r) {        this.r = r;    }          // Constructor    public double getRadius() {        return r;    }          // Accessor    public double area() {        return PI * r * r;    }          // Implementations of    public double circumference() {        return 2 * PI * r;    } // abstract methods. }class Rectangle extends Shape {    protected double w,  h;                               // Instance data    public Rectangle(double w, double h) {               // Constructor        this.w = w;        this.h = h;    }    public double getWidth() {        return w;    }               // Accessor method    public double getHeight() {        return h;    }              // Another accessor    public double area() {        return w * h;    }                 // Implementations of    public double circumference() {        return 2 * (w + h);    }  // abstract methods.}class Triangle extends Shape {    protected double a,b,c,p;        public Triangle(double a, double b, double c) {        this.a = a;        this.b = b;        this.c = c;          p = (a + b + c)/2;    }    public double area() {       return Math.sqrt(p * (p - a) * (p - b) * (p - c));    }    public double circumference() {        return a + b + c;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -