课后练习的问题。求指导!
我不知道怎么把数据带进去...
1. 这个是父类的abstract。
package com.imooc;
public abstract class Shape {
public abstract void perimeter();
public abstract void area();
}
2. 然后是子类的Rectangle。尝试了一下失败了。
package com.imooc;
public class Rectangle extends Shape {
Rectangle p1 = new Rectangle();
private int periSum(){
int length = 10;
int width = 5;
int pSum = (length + width) *2;
return pSum;
}
@Override
public void perimeter() {
// TODO Auto-generated method stub
System.out.println("圆形的周长是:"+ p1.periSum());
}
@Override
public void area() {
// TODO Auto-generated method stub
System.out.println("圆形的面积是:");
}
}
3. 接着是子类的Circle。
package com.imooc;
public class Circle extends Shape {
@Override
public void perimeter() {
// TODO Auto-generated method stub
System.out.println("长方形的周长是:");
}
@Override
public void area() {
// TODO Auto-generated method stub
System.out.println("长方形的面积是:");
}
}
4. 最后是Initial。
package com.imooc;
public class Initial {
public static void main(String[] args) {
// TODO Auto-generated method stub
//计算周长
Shape per1 = new Rectangle();
Shape per2 = new Circle();
//求长方形的周长
per1.perimeter();
//求圆的周长
per2.perimeter();
//计算面积
Shape ar1 = new Rectangle();
Shape ar2 = new Circle();
ar1.area();
ar2.area();
}
}