4-3最后留的问题的代码有人可以写一份给我吗
抽象类那个
抽象类那个
2016-09-30
abstract class Shape{ abstract int getArea(int r,int o); abstract int getPm(int r,int o); } public class Circle extends Shape{ public int r,o; public int area,pm; public int getArea(int r,int o){ return 3*r*r; } public int getPm(int r,int o){ return 6*r; } } public class Rectangle extends Shape{ public int length; public int width; public int area; public int pm; public int getArea(int length,int width){ return length*width; } public int getPm(int length,int width){ return 2*(length+width); } } class Initial { public static void main (String args[]) { Circle c = new Circle(); c.r = 2;c.o=5; c.area = c.getArea(c.r,c.o); c.pm = c.getPm(c.r,c.o); System.out.println("area = "+c.area+"pm = "+c.pm ); Rectangle rt = new Rectangle(); rt.width = 1; rt.length = 1; rt.area = rt.getArea(rt.width,rt.length); rt.pm = rt.getPm(rt.width,rt.length); System.out.println("Area = "+rt.area+"pm = "+rt.pm); } }
package com.proA2; public abstract class Shape {//抽象类shape public abstract void perimeter(double a,double b);//求每个子类的周长 public abstract void area(double a,double b);//求每个子类的面积 } package com.proA2; public class Rectangle extends Shape {//子类:矩形Rectangle @Override public void perimeter(double a,double b) {//求周长 // TODO Auto-generated method stub double Long = (a+b)*2;//得矩形周长 System.out.println("矩形的周长为"+Long); } @Override public void area(double a,double b) {//求面积 // TODO Auto-generated method stub double Area = a*b;//得矩形面积 System.out.println("矩形的面积为"+Area); } } package com.proA2; public class Circle extends Shape {//子类:圆形Circle @Override public void perimeter(double a,double b) {//求周长 // TODO Auto-generated method stub double Long = a*3.14;//得圆形周长 System.out.println("圆形的周长为"+Long); } @Override public void area(double a,double b) {//求面积 // TODO Auto-generated method stub double Area = 3.14*((a/2)*(a/2)); System.out.println("圆形的面积为"+Area); } } package com.proA2; public class Initail { public static void main(String[] args) { Shape object1 = new Rectangle();//父类Shape引用指向Rectangle。 Shape object2 = new Circle();//父类Shape引用指向Circle。 object1.perimeter(5, 6);//Rectangle类的长和宽。 object1.area(5, 6); object2.perimeter(9,0);//Circle类的直径 object2.area(9, 0); } }
package test; public abstract class shape { public abstract double getArea(double a,double b); public abstract double getPm(double a,double b); } package test; public class Circle extends shape { public double area=0; public double pm=0; public double r=0; @Override public double getArea(double r, double b) { // TODO Auto-generated method stub area=r*r*3.14; System.out.println("圆的半径为:"+r+"\n面积为:"+area); return area; } @Override public double getPm(double r, double b) { // TODO Auto-generated method stub pm=2*r*3.14; System.out.println("圆的半径为:"+r+"\n周长为:"+pm); return pm; } } package test; public class Rectangle extends shape { public double area=0; public double pm=0; public double length=0; public double width=0; @Override public double getArea(double length, double width) { // TODO Auto-generated method stub area=length*width; System.out.println("长方形的长为:"+length+"\t宽为:"+width+"\n面积为:"+area); return area; } @Override public double getPm(double length, double width) { // TODO Auto-generated method stub pm=(width+length)*2; System.out.println("长方形的长为:"+length+"\t宽为:"+width+"\n周长为:"+pm); return pm; } } package test; public class Initial { public static void main(String[] args) { // TODO Auto-generated method stub shape circle=new Circle(); shape rectangle=new Rectangle(); double r=5.6; double width=3.2; double length=2.5; circle.getArea(r, 0); circle.getPm(r, 0); rectangle.getArea(width, length); rectangle.getPm(width, length); } }
package imooc;
public abstract class Shape {
public abstract void lo(double a,double b);
public abstract void so(double a,double b);
}
public class Rectangle extends Shape {
@Override
public void lo(double a, double b) {
// TODO Auto-generated method stub
System.out.println("长方形的周长:"+(a+b)*2);
}
@Override
public void so(double a, double b) {
// TODO Auto-generated method stub
System.out.println("长方形的面积:"+a*b);
}
}
public class Cercle extends Shape {
final double PI=3.14;
@Override
public void lo(double a, double b) {
// TODO Auto-generated method stub
System.out.println("圆形的周长是:"+a*2*PI);
}
@Override
public void so(double a, double b) {
// TODO Auto-generated method stub
System.out.println("圆形的面积是:"+a*a*PI);
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Shape l=new Rectangle();
l.lo(4,5);
l.so(4,5);
Shape s=new Cercle();
s.lo(5,5);
s.so(5,5);
}
}
长方形的周长:18.0
长方形的面积:20.0
圆形的周长是:31.400000000000002
圆形的面积是:78.5
举报