定义一个形状类(Shape)方法:计算周长,计算面积。子类: 矩形类、圆形类、三角形类
定义一个形状类(Shape)方法:计算周长,计算面积
子类:
矩形类(Rectangle) :额外的方法:differ() 计算长宽差
圆形类(Circle)
三角形类(Triangle)
正方形类(Square) 矩形的子类
生成几个不同的形状对象,放在一个Shape 类型的数组里,分别求每个形状的周长和面积。
如果形状对象是一个矩形,且不是正方形,则计算长宽差。
Circle.java
1 package ex8; 2 3 import java.util.List; 4 5 public class SongDao { 6 7 private List<Song> songList; 8 9 public SongDao() { 10 11 } 12 13 public SongDao(List<Song> songList) { 14 super(); 15 this.songList = songList; 16 } 17 18 // 新增歌曲 19 public void add(Song song) { 20 songList.add(song); 21 } 22 23 // 查询歌曲 24 // 按id查找 25 public Song search(int sid) { 26 for(Song song : songList) { 27 if(song.getSid()==sid){ 28 return song; 29 } 30 } 31 return null; 32 } 33 // 按对象查找 34 public Song search(Song song) { 35 if(song==null) { 36 return null; 37 } 38 return search(song.getSid()); 39 } 40 41 // 删除歌曲 42 //按id删除歌曲 43 public void delete(int sid) { 44 Song song=search(sid); 45 if(song!=null) { 46 songList.remove(song); 47 } 48 } 49 //按对象删除歌曲 50 public void delete(Song song) { 51 if(song!=null) { 52 delete(song.getSid()); 53 } 54 } 55 56 public List<Song> getSongList() { 57 return songList; 58 } 59 60 public void setSongList(List<Song> songList) { 61 this.songList = songList; 62 } 63 64 65 66 67 }
Rectangle.java
package ex4_1; public class Rectangle extends Shape { private double width; private double height; public Rectangle(double height,double width) { super(); // TODO Auto-generated constructor stub setHeight(height); setWidth(width); } @Override public double length() { // TODO Auto-generated method stub return 2*(width+height); } @Override public double area() { // TODO Auto-generated method stub return width*height; } public double differ() { return Math.abs(height-width); } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } }
Shape.java
package ex4_1; public class Shape { public double length(){ return 0; } public double area() { return 0; } }
Square.java
1 package ex4_1; 2 3 public class Square extends Rectangle { 4 5 public double edge; 6 7 public Square(double edge) { 8 super(edge,edge); 9 setEdge(edge); 10 // TODO Auto-generated constructor stub 11 } 12 13 public double getEdge() { 14 return edge; 15 } 16 17 public void setEdge(double edge) { 18 this.edge = edge; 19 } 20 21 22 }
Tester.java
package ex4_1; public class Tester { public static void main (String[] args) { Shape[] shapes= { new Rectangle(5,10), new circle(5), new Square(3), new Triangle(3, 4, 5) }; for(int i=0;i<shapes.length;i++) { double length=shapes[i].length(); double area=shapes[i].area(); if(shapes[i]instanceof circle) { System.out.println("Shape("+(i+1)+")"+ "length"+String.format("%.2f", length)+",area" +String.format("%.2f", area)); } else { System.out.println("Shape("+(i+1)+")"+ "length"+length+",area"+area); if(shapes[i]instanceof Rectangle) { Rectangle r=(Rectangle)shapes[i]; System.out.println("长宽差是"+r.differ()); } } } } }
Triangle.java
package ex4_1; public class Triangle extends Shape { private double a; private double b; private double c; public Triangle(double a,double b,double c) { super(); // TODO Auto-generated constructor stub setA(a); setB(b); setC(c); } @Override public double length() { // TODO Auto-generated method stub return a+b+c; } @Override public double area() { double p= (a+b+c)/2; // TODO Auto-generated method stub return Math.sqrt(p*(p-a)*(p-b)*(p-c)); } public double getA() { return a; } public void setA(double a) { this.a = a; } public double getB() { return b; } public void setB(double b) { this.b = b; } public double getC() { return c; } public void setC(double c) { this.c = c; } }