问题

本文通过一个具体的Java程序示例,展示了多态的概念及其在实际编程中的应用。通过对父类引用指向子类对象的情况进行分析,解释了方法重写与静态方法调用的区别。
public class Test {
    public static void main(String[] args)  {
        Shape shape = new Circle();
        System.out.println(shape.name);
        
        /**
         * 为什么输出是
         * shape constructor
           circle constructor
           shape
           this is circle
           shape
         */
        shape.printType();
        shape.printName();
    }
}
 
class Shape {
    public String name = "shape";    
    public Shape(){
        System.out.println("shape constructor");
    }    
    public void printType() {
        System.out.println("this is shape");
    }     
    public static void printName() {
        System.out.println("shape");
    }
}
 
class Circle extends Shape {
    public String name = "circle";     
    public Circle() {
        System.out.println("circle constructor");
    }     
    public void printType() {
        System.out.println("this is circle");
    }     
    public static void printName() {
        System.out.println("circle");
    }
}

 

 

转载于:https://www.cnblogs.com/matrix1890/p/7821949.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值