问答详情
源自:10-3 Java 中的抽象类

写了一个有参无返回值的方法代码,求大神写一个有参有返回值的代码(目前什么情况下用有参有返回值的方法,什么情况下用无参无返回值的方法还不是太清楚)

写了一个有参无返回值的方法代码,求大神写一个有参有返回值的代码(目前什么情况下用有参有返回值的方法,什么情况下用无参无返回值的方法还不是太清楚)
package shape;
public  abstract class Shape {
// double i = 0;
// double j = 0;
 //public abstract double perimeter(double length,double wide);
 public abstract void perimeter(double length,double wide);
 public abstract void perimeter(double radious);
 public abstract void area(double length,double wide);
 public abstract void area(double radius);
}
package shape;
public class Rectangle extends Shape{
 public void perimeter(double length, double wide) {
  // TODO Auto-generated method stub
  double perimeter =(length + wide)*2;
  //return 0;
  System.out.println("矩形的周长为:" + perimeter);
 }
 public void area(double length,double wide) {
  // TODO Auto-generated method stub
  double area = length*wide;
  System.out.println("矩形的面积为:" + area);
  //return area;
 }
 public void perimeter(double radious) {
  // TODO Auto-generated method stub
  //return 0;
 }
 public void area(double radius) {
  // TODO Auto-generated method stub
  //return 0;
 }
 
}

package shape;
public class Circle extends Shape{
 public void perimeter(double length, double wide) {
  // TODO Auto-generated method stub
//  return 0;
 }
 public void perimeter(double radious) {
  // TODO Auto-generated method stub
  double perimeter = 2*Math.PI*radious;
  System.out.println("园的周长为:" + perimeter);
//  return perimeter;
 }
 public void area(double length, double wide) {
  // TODO Auto-generated method stub
//  return 0;
 }
 public void area(double radius) {
  // TODO Auto-generated method stub
  double area = Math.PI*Math.pow(radius, 2);
  System.out.println("圆的的面积为:" + area);
//  return area;
 }
}

package shape;
public class Test {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Shape sh = new Rectangle();
  sh.perimeter(5, 4);
  sh.area(5, 4);
  Shape sh1 = new Circle();
  sh1.perimeter(5);
  sh1.area(5);
 }
}

提问者:淹死的鱼liu 2015-07-01 23:17

个回答

  • 煇s
    2015-12-17 22:14:23

    public class demo2 {

    public static void main(String[] args) {  

     Shape sh = new Rectangle();

    System.out.println(sh.perimeter(5, 4)+"\n"+ sh.area(5, 4));  

     Shape sh1 = new Circle();

     System.out.println(sh1.perimeter(5)+"\n"+ sh1.area(5));

    }

    }

      abstract class Shape {


     public abstract String perimeter(double length,double wide);

     public abstract String perimeter(double radious);

     public abstract String area(double length,double wide);

     public abstract String area(double radius);

    }


      class Rectangle extends Shape{

     public String perimeter(double length, double wide) {

      

      double perimeter =(length + wide)*2;

     

      return"矩形的周长为:" + perimeter;

     }

     public String area(double length,double wide) {

     

      double area = length*wide;

      return"矩形的面积为:" + area;

     

     }

     public String perimeter(double radious) {

     

     return "";

     }

     public String area(double radius) {

      

      return "";

     }

      

    }

     


    class Circle extends Shape{

     

     public String perimeter(double radious) {

      

      double perimeter = 2*Math.PI*radious;

      

      return"园的周长为:" + perimeter;


     }


     public String area(double radius) {

      

      double area = Math.PI*Math.pow(radius, 2);

      return"圆的的面积为:" + area;


     }



    public String perimeter(double length, double wide) {

    return "";

    }



    public String area(double length, double wide) {

    return "";

    }

    }