|NO.Z.00061|——————————|BigDataEnd|——|Java&多态特殊类.V02|——|Java.v02|Shape类|Rect类|Rect类重写|show方法使用|

一、[Shape类和Rect类的实现]——[Rect类重写show方法的使用]
### --- 案例题目

~~~     ——>        编程实现Shape类的封装,特征有:横纵坐标,要求提供打印所有特征的方法。
~~~     ——>        编程实现Rect类的封装并继承自Shape类,特征有:长度和宽度。
~~~     ——>        编程实现ShapeRectTest类,在main方法中分别创建Shape和Rect类型对象并打印特征。
二、编程代码:编程实现shape类的封装
package com.yanqi.task09;

public class Shape {
    private int x;
    private int y;

    public Shape() {
    }

    public Shape(int x, int y) {
        setX(x);
        setY(y);
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void show() {
        System.out.println("横坐标:" + getX() + ",纵坐标:" + getY());
    }

    // 自定义静态方法
    public static void test() {
        System.out.println("Shape类中的静态方法!");
    }
}
三、编程代码:编程实现Rect类的封装并继承自Shape类
package com.yanqi.task09;

public class Rect extends Shape {
    private int len;
    private int wid;

    public Rect() {
    }

    public Rect(int x, int y, int len, int wid) {
        super(x, y);
        setLen(len);
        setWid(wid);
    }

    public int getLen() {
        return len;
    }

    public void setLen(int len) {
        if(len > 0) {
            this.len = len;
        } else {
            System.out.println("长度不合理哦!!!");
        }
    }

    public int getWid() {
        return wid;
    }

    public void setWid(int wid) {
        if (wid > 0) {
            this.wid = wid;
        } else {
            System.out.println("宽度不合理哦!!!");
        }
    }

    @Override
    public void show() {
        super.show();
        System.out.println("长度是:" + getLen() + ",宽度是:" + getWid());
    }

    // 自定义静态方法
    //@Override Error: 历史原因、不是真正意义上的重写
    public static void test() {
        System.out.println("---Rect类中的静态方法!");
    }
}
四、编程代码: 编程实现ShapeRectTest类
package com.yanqi.task09;

public class ShapeRectTest {

    public static void main(String[] args) {

        // 1.声明Shape类型的引用指向Shape类型的对象并打印特征
        Shape s1 = new Shape(1, 2);
        // 当Rect类中没有重写show方法时,下面调用Shape类中的show方法
        // 当Rect类中重写show方法后,下面调用Shape类中的show方法
        s1.show(); // 1 2

        // 使用ctrl+d快捷键可以复制当前行
        System.out.println("------------------------------------");
        // 2.声明Rect类型的引用指向Rect类型的对象并打印特征
        Rect r1 = new Rect(3, 4, 5, 6);
        // 当Rect类中没有重写show方法时,下面调用Shape类中的show方法
        // 当Rect类中重写show方法后,下面调用Rect类中的show方法
        r1.show(); // 3 4 5 6

        // 使用alt+shift+上下方向键  可以移动代码
        System.out.println("------------------------------------");
        // 3.声明Shape类型的引用指向Rect类型的对象并打印特征
        // 相当于从Rect类型到Shape类型的转换  也就是子类到父类的转换   小到大的转换  自动类型转换
        Shape sr = new Rect(7, 8, 9, 10);
        // 当Rect类中没有重写show方法时,下面调用Shape类中的show方法
        // 当Rect类中重写show方法后,下面的代码在编译阶段调用Shape类的方法,在运行阶段调用Rect类中的show方法
        sr.show(); // 7 8 9 10

        System.out.println("------------------------------------");
        // 4.测试Shape类型的引用能否直接调用父类和子类独有的方法呢???
        int ia = sr.getX();
        System.out.println("获取到的横坐标是:" + ia); // 7
        //sr.getLen();  error  Shape类中找不到getLen方法,也就是还在Shape类中查找

        // 调用静态方法
        sr.test(); // 提示:不建议使用引用.的方式访问
        Shape.test(); // 推荐使用类名.的方式访问

        System.out.println("------------------------------------");
        // 5.使用父类类型的引用调用子类独有方法的方式
        // 相当于从Shape类型到Rect类型的转换,也就是父类到子类的转换  大到小的转换   强制类型转换
        int ib = ((Rect) sr).getLen();
        System.out.println("获取到的长度是:" + ib); // 9

        // 希望将Shape类型转换为String类型  强制类型转换要求必须拥有父子类关系
        //String str1 = (String)sr;  Error
        // 希望将Shape类型强制转换为Circle类型,下面没有报错
        //Circle c1 = (Circle)sr; // 编译ok,但运行阶段发生  ClassCastException类型转换异常

        // 在强制类型转换之前应该使用instanceof进行类型的判断
        // 判断sr指向堆区内存中的对象是否为Circle类型,若是则返回true,否则返回false
        if(sr instanceof Circle) {
            System.out.println("可以放心地转换了!");
            Circle c1 = (Circle)sr;
        } else {
            System.out.println("强转有风险,操作需谨慎!");
        }
    }
}
五、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=62171:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task09.ShapeRectTest
横坐标:1,纵坐标:2
------------------------------------
横坐标:3,纵坐标:4
长度是:5,宽度是:6
------------------------------------
横坐标:7,纵坐标:8
长度是:9,宽度是:10
------------------------------------
获取到的横坐标是:7
Shape类中的静态方法!
Shape类中的静态方法!
------------------------------------
获取到的长度是:9
强转有风险,操作需谨慎!

Process finished with exit code 0

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(44)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示