已成功运行 有不同想法可以交流
1.
package com.imooc;
public abstract class Shape {
public abstract void ares();
public abstract void girth();
}
2.
package com.imooc;
public class Rec extends Shape {
private int l;
private int w;
public Rec(int l,int w) {
this.l=l;
this.w=w;
}
@Override
public void ares() {
System.out.println("长方形面积为: "+l*w);
}
@Override
public void girth() {
System.out.println("长方形周长为: "+(l+w)*2);
}
}
3.
package com.imooc;
public class Squ extends Shape {
private int r;
public Squ() {
}
public Squ(int r) {
this.r=r;
}
@Override
public void ares() {
System.out.println("圆面积: "+Math.PI*r*r);
}
@Override
public void girth() {
System.out.println("圆周长: "+Math.PI*2*r);
}
}
4.
package com.imooc;
public class Initial {
public static void main(String[] args) {
Shape R=new Rec(15,8);
Shape S=new Squ(7);
R.ares();
R.girth();
S.ares();
S.girth();
}
}
1. package jszcmj; public abstract class shape { public double pi=3.14; public abstract void Reactangle(double a,double b); public abstract void Circle(double a); } 2. package jszcmj; public class ReacLong extends shape { @Override public void Reactangle(double a, double b) { // TODO Auto-generated method stub double rt=(a+b)*2; System.out.println("矩形周长为:"+rt); } @Override public void Circle(double a) { // TODO Auto-generated method stub double cc=pi*a*2; System.out.println("圆形周长为:"+cc); } } 3. package jszcmj; public class Cirmian extends shape { @Override public void Reactangle(double a, double b) { // TODO Auto-generated method stub double rtm=a*b; System.out.println("矩形面积为:"+rtm); } @Override public void Circle(double a) { // TODO Auto-generated method stub double ccm=pi*a*a; System.out.println("圆形面积为;"+ccm); } } 4. package jszcmj; public class Initail { public static void main(String[] args) { // TODO Auto-generated method stub shape sh1=new ReacLong(); shape sh2=new Cirmian(); sh1.Reactangle(10, 20); sh2.Reactangle(10, 20); sh1.Circle(4); sh2.Circle(4); } }
要用传参方法怎么实现?矩形两个变量,圆一个变量