class TuXing
- package com.dong;
-
- /**
- * @author Captain_Dong
- *
- * 这个类不能创建对象,原因:存在抽象方法,该方法无法工作
- * 如何才能使用?------>>>>进一步给出具体实现(到底是什么图形,我才能实现类)
- */
- public abstract class TuXing {
- private String mingCheng;
- public abstract double Area();
- public abstract double zhouChang();
- public String getAbc(){
- return "11111111111aaaaaaaaaaa222222222222bbbbbbbb!";
- }
-
- public TuXing() {
- }
-
- //public abstract String pp();
- public String pp(){ //一般方法的重写
- return "";
- }
- public TuXing(String mingCheng) {
- this.mingCheng = mingCheng;
- }
-
- public String getMingCheng() {
- return mingCheng;
- }
-
- public void setMingCheng(String mingCheng) {
- this.mingCheng = mingCheng;
- }
-
- @Override
- public String toString() {
- return "TuXing{" +
- "mingCheng='" + mingCheng + '\'' +
- '}';
- }
- }
public class JVXing
- package com.dong;
-
- public class JVXing extends TuXing{
- //添加新属性
- private double x;//长
- private double y;//宽
-
- //构造方法
-
- public JVXing(String mingCheng, double x, double y) {
- super(mingCheng);
- this.x = x;
- this.y = y;
- }
- public JVXing( double x, double y) {
- super("矩形");//父类构造方法调用
- this.x = x;
- this.y = y;
- }
-
- @Override
- public double Area() {
- return this.x*this.y;
- }
-
- @Override
- public double zhouChang() {
- return 2*(this.y+this.x);
- }
-
- public String pp(){ //获取矩形相关信息
- //super.getAbc();
- String ss;
- ss=super.getAbc()+"\n"+super.getMingCheng()+"的长为:"+this.x+",宽为:"+this.y+"\n"+
- "面积为:"+this.Area()+",周长为:"+this.zhouChang();
- return ss;
- }
- }
public class Circle
- package com.dong;
-
- public class Circle extends TuXing{
-
- private double r;
-
- public Circle(){
- super();
- }
- public Circle(String mingCheng, double r) {
- super(mingCheng);
- this.r = r;
- }
-
- public Circle(String s) {
- }
-
- @Override
- public double Area() {
- return Math.PI*Math.pow(this.r,2);
- }
-
- @Override
- public double zhouChang() {
- return 2*Math.PI*this.r;
- }
- public String pp(){
- String ss;
- ss="圆的面积为:"+this.Area()+"周长为:"+this.zhouChang();
- return ss;
- }
-
- public double getR() {
- return r;
- }
-
- public void setR(double r) {
- this.r = r;
- }
- }
public class Test01
- package com.dong;
-
- public class Test01 {
- public static void main(String[] args) {
- JVXing jvXing2=new JVXing("矩形1",100,15);
-
- JVXing jvXing=new JVXing("第二个矩形",10,2);
- JVXing jvXing1=new JVXing(100,200);
-
- TuXing tuXing=new JVXing("矩形2",15,20);
- TuXing tuXing1=new JVXing("矩形3",15,30);
-
- System.out.println(tuXing.pp());
- System.out.println(tuXing1.pp());
-
- System.out.println(tuXing);
-
- Circle circle=new Circle("圆",100);
-
- TuXing tuXing2=new Circle("圆1"+100);
-
- System.out.println(tuXing1.pp());//调用的是矩形的方法(多态)自动识别对象的方法
- System.out.println(tuXing2.pp());//调用的是圆的方法(多态)
-
-
- }
- }