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

麻烦大神帮我指点一下一下这段代码该怎么改?谢谢

父类:

package com.imocc

public abstract class shape {

public abstract void l();

public abstract void s();

}

子类:rectangle

package com.imocc;

public class rectangle extends shape {

public void l(int a,int b){

System.out.println("长方形的周长是:"+(a+b)*2);

}

public void s(int a,int b){

System.out.println("长方形的面积是:"+a*b);

}

}

子类circle:

package com.imocc;

public class circle extends shape {

double π=3.14;

public void l(int r){

double m=2*π*r;

System.out.println("圆形的周长是:"+m);

}

    public void s(int r){

    double n=π*r*r;

    System.out.println("圆形的面积是:"+n);

    }

}

inital;

package com.imocc;

import java.util.Scanner;

public class inital {

public static void main(String[] args) {

shape rec=new rectangle();

Scanner input=new Scanner(System.in);

System.out.println("请输入长方形的长a:");

int a=input.nextInt();

System.out.println("请输入长方形的宽b:");

int b=input.nextInt();

rec.l(a,b);

rec.s(a,b);

shape c=new circle();

Scanner input2=new Scanner(System.in);

System.out.println("请输入圆形的半径r:");

int r=input2.nextInt();

c.l(r);

c.s(r);

// TODO Auto-generated method stub

}

}


提问者:慕用7104560 2018-08-19 09:24

个回答

  • 慕先生9003034
    2018-08-19 16:31:22

    Java抽象类中的抽象方法的参数对应的子类的方法的参数必须一致