MiseryKyo
2015-02-12 17:05
如果父类中的抽象方法是无参的,那么子类中重写的方法是不是必须也是无参的?
比如要算矩形的周长需要两个参数l和h,面积需要两个参数l和h,而圆形周长和面积只需要一个参数r。
这个时候需要怎么定义抽象方法?是不是需要再定义两次抽象方法为两个参数的和一个参数的,并且重写的方法中参数还必须和抽象方法中一样?
这是抽象父类Shape:
public abstract class Shape { public abstract void getGirth(int a,int b); public abstract void getArea(int a,int b); public abstract void getGirth(int a); public abstract void getArea(int a); }
这是子类Rectangle:
public class Rectangle extends Shape { @Override public void getGirth(int a,int b) { // TODO Auto-generated method stub System.out.println("矩形的面积为:"+(2*a+2*b)); } @Override public void getArea(int a,int b) { // TODO Auto-generated method stub System.out.println("矩形的面积为:"+a*b); } @Override public void getGirth(int a) { // TODO Auto-generated method stub } @Override public void getArea(int a) { // TODO Auto-generated method stub }
这是子类Circle:
public class Circle extends Shape { @Override public void getGirth(int a) { // TODO Auto-generated method stub System.out.println("圆形的周长为:"+(2*3.14*a)); } @Override public void getArea(int a) { // TODO Auto-generated method stub System.out.println("圆形的面积为:"+3.14*a*a); } @Override public void getGirth(int a, int b) { // TODO Auto-generated method stub } @Override public void getArea(int a, int b) { // TODO Auto-generated method stub }
测试类Test:
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Shape a = new Rectangle(); Shape b = new Circle(); a.getGirth(3, 4); a.getArea(3, 4); b.getGirth(3); b.getArea(3); }
运行结果:
矩形的面积为:14 矩形的面积为:12 圆形的周长为:18.84 圆形的面积为:28.259999999999998
想问下有没有更简便点的办法?
首先重写子类的方法必须和被继承的父类的方法相同 //父类 public class Shape { public void getArea(){ System.out.println("图形的面积"); } public void getGirth(){ System.out.println("图形的周长"); } } //圆形子类 public class Circle extends Shape { int r=3; public void getArea(){ double area=3.14*r*r; System.out.println("圆形的面积"+area); } public void getGirth(){ double girth=2*3.14*r; System.out.println("圆形的周长"+girth); } } //方形子类 public class Rectangle extends Shape { int a=2; int b=4; public void getArea(){ double area=a*b; System.out.println("方形的面积"+area); } public void getGirth(){ double girth=2*(a+b); System.out.println("圆形的周长"+girth); } } //测试类 public class Initial { public static void main(String[] args) { // TODO Auto-generated method stub Circle circle= new Circle(); circle.getArea(); circle.getGirth(); Rectangle rectangle= new Rectangle(); rectangle.getArea(); rectangle.getGirth(); } }
这是我写的。。。。
//父类 public abstract class Shape { public abstract double perimeter(double i,double j); public abstract double Area(double m,double n); } //子类Circle public class Circle extends Shape { @Override public double perimeter(double i, double j) { // TODO Auto-generated method stub double p = 2 * i * j; return p; } @Override public double Area(double m, double n) { // TODO Auto-generated method stub double s = m * n * n; return s; } } //子类Rectangle public class Rectangle extends Shape { @Override public double perimeter(double i,double j) { // TODO Auto-generated method stub double p = 2*(i+j); return p ; } @Override public double Area(double m,double n) { // TODO Auto-generated method stub double s = m*n; return s; } } //测试类 import java.util.*; public class Inital { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); Shape a = new Rectangle(); Shape b = new Circle(); for( int i =0 ;i <= 1; i++){ double m=0; double n=0; if(i == 0){ System.out.println("count Rectangle,Please input length and width"); System.out.println("length:"); m = scanner.nextDouble(); System.out.println("width:"); n = scanner.nextDouble(); double p = a.perimeter(m, n); double s = a.Area(m, n); System.out.println("Rectangle's perimeter :"+p); System.out.println("Rectangle's Area:"+s); } if(i == 1){ System.out.println("count Circle,Please input radius"); // System.out.println("PI:"); // m = scanner.nextDouble(); System.out.println("radius:"); n = scanner.nextDouble(); double p = b.perimeter(Math.PI, n); double s = b.Area(Math.PI, n); System.out.println("Circle's perimeter :"+p); System.out.println("Circle's Area:"+s); } } } }
可以使用可变长参数试试
Java入门第二季 升级版
530650 学习 · 6091 问题
相似问题