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

l练习题代码

求4.3后面的练习题代码??

提问者:慕仔7135344 2016-11-02 18:28

个回答

  • 千修罗
    2016-11-03 15:07:11
    已采纳

    package Shape;
    
    public abstract class Shape {
      public abstract void Girth();
      public abstract void Area();
      
      public static void main(String args[]){
    	  Shape r = new Rectangle(5,4);
    	  r.Girth();
    	  r.Area();
    	  Shape c = new Circle(5);
    	  c.Girth();
    	  c.Area();
      }
    }
    package Shape;
    
    public abstract class Shape {
      public abstract void Girth();
      public abstract void Area();
      
      public static void main(String args[]){
    	  Shape r = new Rectangle(5,4);
    	  r.Girth();
    	  r.Area();
    	  Shape c = new Circle(5);
    	  c.Girth();
    	  c.Area();
      }
    }
    package Shape;
    
    public class Circle extends Shape {
    	int radius;
    	public Circle(int radius){
    		this.radius = radius;
    	}
    
    	public void Girth() {
    		double Girth = radius * 2 * 3.14;
    		System.out.println("圆形的周长为:" + Girth);
    	}
    
    	public void Area() {
    		double Area = radius * radius * 3.14;
    		System.out.println("圆形的面积为:" + Area);
    	}
    }


  • TTTi
    2016-11-24 21:43:25


    class Text //测试类

    {

    public static void main(String[] args) 

    {

    Shape s1 = new Rectangle1();

    Shape s2 = new Circle();

    s1.girth(3,4);

    s1.size(5,2);

    s2.girth(6.0);

    s2.size(10.0);

    }

    }

    abstract class Shape //父类

    {

    public abstract void girth(int a,int b);

    public abstract void size(int a,int b);

    public abstract void girth(double r);

    public abstract void size(double r);

    }

    class Rectangle1 extends Shape//子类1

    {

    public void girth(int a,int b)

    {

    int gir;

    gir = (a + b)*2;

    System.out.println("矩形的周长为:" + gir );

    }

    public void size(int a,int b)

    {

    int si;

    si = a * b;

    System.out.println("矩形的面积为:" + si);

    }

    public void girth(double r){}

    public void size(double r){}

    }

    class Circle extends Shape//子类2

    {

    double pai = 3.1415926;

    double r;

    public void girth(double r)

    {

    double gir = pai *r;

    System.out.println("圆的周长为:" + gir);

    }

    public void size(double r)

    {

    double si;

    si = pai * (r *r)/4;

    System.out.println("圆的面积为:" + si);

    }

    public void girth(int a,int b){}

    public void size(int a,int b){}

    }


  • talent85012016
    2016-11-24 15:19:30

    public abstract class Figure {

    public abstract void perimeter(double a,double b);

    public abstract void area(double a , double b);

    }

    抽象方法是可以带参数的

  • TTTi
    2016-11-24 02:46:00

    我用抽象写不出来啊,一直报错 类不是抽象的,并且未覆盖抽象方法,最后只能这样写了

    public class Text //测试类

    {

    public static void main(String[] args) 

    {

    Shape s1 = new Rectangle1();

    Shape s2 = new Circle();

    s1.zhouChang(3,4);

    s1.squareMeasure(5,2);

    s2.zhouChang(6.0);

    s2.squareMeasure(10.0);

    }

    }


    class Shape   //父类

    {

    public void zhouChang(int a,int b){}

    public void squareMeasure(int a,int b){}

    public void zhouChang(double r){}

    public void squareMeasure(double r){}

    }


    class Rectangle1 extends Shape  //矩形

    {

    public void  zhouChang(int a,int b)

    {

    int zc;

    zc = (a + b)*2;

    System.out.println("矩形的周长为:" + zc );

    }

    public void squareMeasure(int a,int b)

    {

    int sm;

    sm = a * b;

    System.out.println("矩形的面积为:" + sm);

    }

    }


    class Circle extends Shape //圆形

    {

    double pai = 3.1415926;

    public void zhouChang(double r)

    {

    double zc = pai *r;

    System.out.println("圆的周长为:" + zc);

    }

    public void squareMeasure(double r)

    {

    double sm;

    sm = pai * (r *r)/4;

    System.out.println("圆的面积为:" + sm);

    }

    }


  • zivf
    2016-11-02 21:59:29

    package com.Shape;
    public abstract class Shape {
     public abstract void girth();
     public abstract void area();
    }
    
    package com.Shape;
    public class Rentanle extends Shape {
    
     public void girth() {
      // TODO Auto-generated method stub
         System.out.println("长方形的周长=2*(长+宽)");
     }
     
     public void area() {
      // TODO Auto-generated method stub
          System.out.println("长方形的面积=长*宽");
     }
    }
    
    package com.Shape;
    public class Circle extends Shape {
     
     public void girth() {
      // TODO Auto-generated method stub
        System.out.println("圆形的周长=π*直径");
     }
     
     public void area() {
      // TODO Auto-generated method stub
        System.out.println("圆形的面积=π*r*r");
     }
    }
    
    package com.Shape;
    public class Initial {
     public static void main(String[] args) {
      // TODO Auto-generated method stub
        Shape s1 =new Rentanle();
      
        s1.area();
        s1.girth();
         Shape s2=new Circle();
         s2.area();
         s2.girth();
       
        
          
     }
    }

    亲测可用,好像有个单词写错了,但是懒得改了。给你参考一下吧,交流一下。