?? ex3.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; }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -