这样写行吗?
父类
package com.imooc;
public abstract class Shape {
public abstract void zhouchang();
public abstract void mianji();
public int a=3,b=2,c=(a+b)*2,d=a*b;
double π=3.14;
int r=3;
double e=2*r*π;
double s=r*r*π;
}
子类
package com.imooc;
public class Circle extends Shape {
@Override
public void zhouchang() {
// TODO Auto-generated method stub
System.out.println("圆的周长"+e);
}
@Override
public void mianji() {
// TODO Auto-generated method stub
System.out.println("圆的面积"+s);
}
}
子类
package com.imooc;
public class Rectangle extends Shape {
@Override
public void zhouchang() {
// TODO Auto-generated method stub
System.out.println("矩形的周长"+c);
}
@Override
public void mianji() {
// TODO Auto-generated method stub
System.out.println("矩形的面积"+d);
}
}
测试
package com.imooc;
public class Initail {
public static void main(String[] args) {
// TODO Auto-generated method stub
Shape abc=new Rectangle();
abc.zhouchang();
abc.mianji();
Shape ab=new Circle();
ab.zhouchang();
ab.mianji();
}
}