主要考察:Java的继承和重写的知识
运行结果:
注意:
1、Squale,Circle,Retcangle,Triangle等类的方法不要传参
2、子类对父类的方法的重写,保持一致(是否传参,返回类型)
3、不要为了少写一个方法盲目继承,继承之间必须有附属(父子)关系
同时我们再加一个功能,根据面积和周长大小进行排序
运行结果:
本文地址:http://liuyanzhao.com/3396.html
转载请注明
没有排序功能的代码如下:
- package com.liuyanzhao;
- class Shape {
- String name;
- public String getName() {
- return name;
- }
- public double getArea() {//面积
- return 0;
- }
- public double getGirth() {//周长
- return 0;
- }
- }
- class Square extends Shape {//正方形
- double a;
- public Square(double a) {
- this.name = "正方形";
- this.a = a;
- }
- public double getArea() {
- return a*a;
- }
- public double getGirth() {
- return 4*a;
- }
- }
- class Rectangle extends Shape { //长方形
- double a,b;
- public Rectangle(double a,double b) {
- this.name = "长方形";
- this.a = a;
- this.b = b;
- }
- public double getArea() {
- return a*b;
- }
- public double getGirth() {
- return (a+b)*2;
- }
- }
- class Triangle extends Shape { //三角形
- double a,b,c;
- public Triangle(double a,double b,double c) {
- this.name = "三角形";
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public double getArea() {
- double p = (a+b+c)/2;
- double s = (double)Math.sqrt(p*(p-a)*(p-b)*(p-c));
- return s;
- }
- public double getGirth() {
- return (a+b+c);
- }
- }
- class Circle extends Shape { //圆
- public static final double PI = 3.14;
- double r;
- public Circle(double r) {
- this.name = "圆";
- this.r = r;
- }
- public double getArea() {
- return (double)(PI*r*r);
- }
- public double getGirth() {
- return 2*PI*r;
- }
- }
- public class Demo {
- public static void main(String[] args) {
- Shape []s = {new Square(3),new Rectangle(3, 4),new Triangle(3, 4, 5),new Circle(3)};
- for(int i = 0;i<s.length;i++) {
- System.out.println(s[i].getName()+",面积="+s[i].getArea()+",周长="+s[i].getGirth());
- }
- }
- }
运行结果:
注意:
1、Squale,Circle,Retcangle,Triangle等类的方法不要传参
2、子类对父类的方法的重写,保持一致(是否传参,返回类型)
3、不要为了少写一个方法盲目继承,继承之间必须有附属(父子)关系
同时我们再加一个功能,根据面积和周长大小进行排序
加排序功能代码如下:
- package com.liuyanzhao;
- class Shape {
- String name;
- public String getName() {
- return name;
- }
- public double getArea() {//面积
- return 0;
- }
- public double getGirth() {//周长
- return 0;
- }
- public void compareArea(Shape []ss,int len) //根据面积大小升序排序
- {
- Shape s;
- int i,j;
- for(i=0;i<len;i++)
- {
- for(j=i+1;j<len;j++)
- {
- if(ss[j].getArea()<ss[i].getArea())
- {
- s=ss[i];
- ss[i]=ss[j];
- ss[j]=s;
- }
- }
- }
- }
- public void compareGirth(Shape []ss,int len) //根据周长大小升序排序
- {
- Shape s;
- int i,j;
- for(i=0;i<len;i++)
- {
- for(j=i+1;j<len;j++)
- {
- if(ss[j].getGirth()<ss[i].getGirth())
- {
- s=ss[i];
- ss[i]=ss[j];
- ss[j]=s;
- }
- }
- }
- }
- }
- class Square extends Shape {//正方形
- double a;
- public Square(double a) {
- this.name = "正方形";
- this.a = a;
- }
- public double getArea() {
- return a*a;
- }
- public double getGirth() {
- return 4*a;
- }
- }
- class Rectangle extends Shape { //长方形
- double a,b;
- public Rectangle(double a,double b) {
- this.name = "长方形";
- this.a = a;
- this.b = b;
- }
- public double getArea() {
- return a*b;
- }
- public double getGirth() {
- return (a+b)*2;
- }
- }
- class Triangle extends Shape { //三角形
- double a,b,c;
- public Triangle(double a,double b,double c) {
- this.name = "三角形";
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public double getArea() {
- double p = (a+b+c)/2;
- double s = (double)Math.sqrt(p*(p-a)*(p-b)*(p-c));
- return s;
- }
- public double getGirth() {
- return (a+b+c);
- }
- }
- class Circle extends Shape { //圆
- public static final double PI = 3.14;
- double r;
- public Circle(double r) {
- this.name = "圆";
- this.r = r;
- }
- public double getArea() {
- return (double)(PI*r*r);
- }
- public double getGirth() {
- return 2*PI*r;
- }
- }
- public class Demo {
- public static void main(String[] args) {
- Shape []s = {new Square(3),new Rectangle(3, 4),new Triangle(3, 4, 5),new Circle(2)};
- for(int i = 0;i<s.length;i++) {
- System.out.println(s[i].getName()+",面积="+s[i].getArea()+",周长="+s[i].getGirth());
- }
- System.out.println("-------------------------------------");
- System.out.println("根据面积大小排序:");
- new Shape().compareArea(s,s.length);
- for(int i = 0;i<s.length;i++) {
- System.out.println(s[i].getName()+",面积="+s[i].getArea()+",周长="+s[i].getGirth());
- }
- System.out.println("-------------------------------------");
- System.out.println("根据周长大小排序:");
- new Shape().compareGirth(s,s.length);
- for(int i = 0;i<s.length;i++) {
- System.out.println(s[i].getName()+",面积="+s[i].getArea()+",周长="+s[i].getGirth());
- }
- }
- }
运行结果:
本文地址:http://liuyanzhao.com/3396.html
转载请注明
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏