• java-继承类练习


    class TuXing 
    1. package com.dong;
    2. /**
    3. * @author Captain_Dong
    4. *
    5. * 这个类不能创建对象,原因:存在抽象方法,该方法无法工作
    6. * 如何才能使用?------>>>>进一步给出具体实现(到底是什么图形,我才能实现类)
    7. */
    8. public abstract class TuXing {
    9. private String mingCheng;
    10. public abstract double Area();
    11. public abstract double zhouChang();
    12. public String getAbc(){
    13. return "11111111111aaaaaaaaaaa222222222222bbbbbbbb!";
    14. }
    15. public TuXing() {
    16. }
    17. //public abstract String pp();
    18. public String pp(){ //一般方法的重写
    19. return "";
    20. }
    21. public TuXing(String mingCheng) {
    22. this.mingCheng = mingCheng;
    23. }
    24. public String getMingCheng() {
    25. return mingCheng;
    26. }
    27. public void setMingCheng(String mingCheng) {
    28. this.mingCheng = mingCheng;
    29. }
    30. @Override
    31. public String toString() {
    32. return "TuXing{" +
    33. "mingCheng='" + mingCheng + '\'' +
    34. '}';
    35. }
    36. }

     

    public class JVXing
    1. package com.dong;
    2. public class JVXing extends TuXing{
    3. //添加新属性
    4. private double x;//长
    5. private double y;//宽
    6. //构造方法
    7. public JVXing(String mingCheng, double x, double y) {
    8. super(mingCheng);
    9. this.x = x;
    10. this.y = y;
    11. }
    12. public JVXing( double x, double y) {
    13. super("矩形");//父类构造方法调用
    14. this.x = x;
    15. this.y = y;
    16. }
    17. @Override
    18. public double Area() {
    19. return this.x*this.y;
    20. }
    21. @Override
    22. public double zhouChang() {
    23. return 2*(this.y+this.x);
    24. }
    25. public String pp(){ //获取矩形相关信息
    26. //super.getAbc();
    27. String ss;
    28. ss=super.getAbc()+"\n"+super.getMingCheng()+"的长为:"+this.x+",宽为:"+this.y+"\n"+
    29. "面积为:"+this.Area()+",周长为:"+this.zhouChang();
    30. return ss;
    31. }
    32. }

     

    public class Circle
    1. package com.dong;
    2. public class Circle extends TuXing{
    3. private double r;
    4. public Circle(){
    5. super();
    6. }
    7. public Circle(String mingCheng, double r) {
    8. super(mingCheng);
    9. this.r = r;
    10. }
    11. public Circle(String s) {
    12. }
    13. @Override
    14. public double Area() {
    15. return Math.PI*Math.pow(this.r,2);
    16. }
    17. @Override
    18. public double zhouChang() {
    19. return 2*Math.PI*this.r;
    20. }
    21. public String pp(){
    22. String ss;
    23. ss="圆的面积为:"+this.Area()+"周长为:"+this.zhouChang();
    24. return ss;
    25. }
    26. public double getR() {
    27. return r;
    28. }
    29. public void setR(double r) {
    30. this.r = r;
    31. }
    32. }
    public class Test01

     

    1. package com.dong;
    2. public class Test01 {
    3. public static void main(String[] args) {
    4. JVXing jvXing2=new JVXing("矩形1",100,15);
    5. JVXing jvXing=new JVXing("第二个矩形",10,2);
    6. JVXing jvXing1=new JVXing(100,200);
    7. TuXing tuXing=new JVXing("矩形2",15,20);
    8. TuXing tuXing1=new JVXing("矩形3",15,30);
    9. System.out.println(tuXing.pp());
    10. System.out.println(tuXing1.pp());
    11. System.out.println(tuXing);
    12. Circle circle=new Circle("圆",100);
    13. TuXing tuXing2=new Circle("圆1"+100);
    14. System.out.println(tuXing1.pp());//调用的是矩形的方法(多态)自动识别对象的方法
    15. System.out.println(tuXing2.pp());//调用的是圆的方法(多态)
    16. }
    17. }

  • 相关阅读:
    【系统架构】-什么是MDA架构、ADL、DSSA
    ELK企业级日志分析系统
    NAS磁盘挂载
    高等数学(第七版)同济大学 习题7-8 个人解答
    ASEMI快恢复二极管MURD560的更换方法
    算法每日一题(合并两个有序的数组)
    【产品经理修炼之道】- 从需求到功能的转化过程
    09-spring的bean创建流程(一)
    UIC 564-2 座椅耐火测试
    ffmpeg之去除视频水印
  • 原文地址:https://blog.csdn.net/m0_63064861/article/details/127653381