在矩形类中直接给a b 赋值和在测试类中用矩形类的对象给a b赋值有什么不同
//矩形
public class Rectangle extends Shape {
int a=1;
int b=2;
@Override
public void zhouzhang() {
// TODO Auto-generated method stub
int zz=(a+b)*2;
System.out.println("矩形周长为:"+zz);
}
@Override
public void mianji() {
// TODO Auto-generated method stub
int mj=a*b;
System.out.println("矩形面积为:"+mj);
}
}
测试类
public class Shapexam {
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle re=new Rectangle();
re.a=2;
re.b=5;
re.zhouzhang();
re.mianji();
Shape s1=new Rectangle();
s1.zhouzhang();
s1.mianji();
Shape s2=new Circle();
s2.zhouzhang();
s2.mianji();
}
}
运行结果
矩形周长为:14
矩形面积为:10
矩形周长为:6
矩形面积为:2