慕盖茨7517560
2018-07-23 12:57
父类
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();
}
}
可以尝试下通过Scanner来让数据手动输入
这样子类继承父类的变量太多,最好不要这么写,有些变量写到本类最好
Java入门第二季 升级版
530656 学习 · 6091 问题
相似问题