Java笔记(6)——多态、instanceof操作符和一个几何形体的例子

这篇博客介绍了Java中的多态性概念,通过Person、Man和Woman类的实例展示了如何实现多态。同时,解释了instanceof操作符的用法,用于检查对象是否为特定类的实例。此外,通过GeometricObject、Circle和MyRectangle类,阐述了抽象类和方法重写在几何对象中的应用,并提供了equalsArea方法来比较不同几何对象的面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 多态

多态:同样的类型变量,调用同样的方法,却产生完全不同的行为。

  1. 当父类类型的变量指向子类的对象,调用父类中已经被重写的方法时,产生多态机制。
  2. 在多态的情况下,不能调用子类新增的方法,使用强制的类型转化。
  3. 只有在有父子关系的情况下才能进行强制类型转换。
public class Person {
     public String name;
     int age;

     public String getInfo(){
         return "name:" + this.name + ",age:" + this.age;
     }

     void walk(){
         System.out.println("人在走路····");
     }

     public Person(){

     }

     public Person(String name, int age){
         super();
         this.name = name;
		 this.age = age;
     }

}



public class Man extends Person{

    void work(){
        System.out.println("男人工作!!");
    }

    void walk(){
        System.out.println("男人快速的走路···");
    }

}


public class Woman extends Person{

    void shopping(){
        System.out.println("女人逛街。。。。") ;
    }

    void walk(){
        System.out.println("女人优雅的走路···");
    }
}


public class TestPerson {
    public static void main(String[] args) {
        Person p1 = new Person();
        p1.walk();

        Man man = new Man();
        man.walk();
        man.work();

        // 一种类型  多种状态
        Person p2 = new Man();
        p2.walk();

        Man m = (Man)p2;
        m.work();

        //p2.work();

        // 多种状态
        Person p3 = new Woman();
        p3.walk();

    }
}
/*
人在走路····
男人快速的走路···
男人工作!!
男人快速的走路···
女人优雅的走路···
*/

2. instanceof操作符

x instanceof A检验x是否为A的对象,返回值为boolean。要求x所属的类与A类必须是子类或者父类的关系,否则编译出错。

3. 一个几何形体的例子

定义一个类GeometricObject,里边有color 和 weight属性。
定义Circle类,继承GeometricObject,增加radius属性。
定义MyRectangle类,继承GeometricObject类,增加 width 和 height 属性。
在测试类中编写equalsArea方法 测试两个对象的面积是否相等。

3.1 代码1GeometricObject

public abstract class GeometricObject {
    protected String color; // protected 可以被子类访问
    protected double weight;

    // ctrl + / 是注释多行
    // ctrl + shift + / 是 注释代码块
//    protected GeometricObject(String color, double weight){
//        this.color = color;
//        this.weight = weight;
//    }


    public GeometricObject(String color, double weight) {
        this.color = color;
        this.weight = weight;
    }


    // 自动生成 get 和 set 方法
    // 右键  generator
    // 或者是 alt + insert 快捷键

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

	// 多态的重要性
    public double findArea(){ // 抽象的概念 没办法去求面积
        // 定义这样的代码  意义尤为重大
        return 0;
    }
}

3.2 代码2 Circle

public class Circle extends GeometricObject{
    private double radius;

    public Circle(String color, double weight, double radius) {
        super(color, weight);
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double findArea(){ // 方法的重写
        return Math.PI * this.radius * this.radius;
    }
}

3.3 代码3MyRectangle

public class MyRectangle extends GeometricObject{

    private double width;
    private double heigth;

    public MyRectangle(String color, double weight, double width, double heigth) {
        super(color, weight);
        this.width = width;
        this.heigth = heigth;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeigth() {
        return heigth;
    }

    public void setHeigth(double heigth) {
        this.heigth = heigth;
    }

    public double findArea(){ // 方法的重写
        return this.heigth * this.width;
    }
}

3.4 代码4TestGeometricObject

public class TestGeometricObject {

    public boolean equalsArea(GeometricObject o1, GeometricObject o2){
        return o1.findArea() == o2.findArea();
    }

    public void displayGeometricObject(GeometricObject o){
        System.out.println("area:" + o.findArea()); // sout  快捷生成输出语句
    }

    public static void main(String[] args) {
        TestGeometricObject tgo = new TestGeometricObject();

        Circle c1 = new Circle("", 0, 2);
        Circle c2 = new Circle("", 0, 3);

        boolean ret = tgo.equalsArea(c1, c2);
        System.out.println("c1和c2的面积是否相等:" + ret);

        tgo.displayGeometricObject(c1);
        tgo.displayGeometricObject(c2);

        MyRectangle m1 = new MyRectangle("", 0, 2, 6.28);
        boolean ret2 = tgo.equalsArea(c1, m1);
        System.out.println("ret2 = " + ret2);;
    }
}
/*
c1和c2的面积是否相等:false
area:12.566370614359172
area:28.274333882308138
ret2 = false
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值