栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java多态(含静态方法)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java多态(含静态方法)

import java.awt.image.SampleModel;

public class Test {
    public static void main(String[] args) {
        Shape a=new Circle();
        a.printName();
        a.printType();
        System.out.println(a.name);
        Circle b=(Circle)a;

        b.printName();
        b.printType();
        System.out.println(b.name);

        Circle c=new Circle();

        c.printName();
        c.printType();
        System.out.println(c.name);
    }
}
class Shape{
    public String name="shape";
    public Shape(){
        System.out.println("shpe constructor");

    }
    public void printType(){
        System.out.println("shapenormal");
    }
    public static void printName(){
        System.out.println("shapestatic");
    }
}
class Circle extends Shape{
    public String name="circle";
    public Circle(){
        System.out.println("circle construct");

    }

    @Override
    public void printType() {
        System.out.println("circlenormal");
    }
    public static void printName(){

        System.out.println("circlestatic");
    }
}

输出结果:

shpe constructor // 创建circle默认调用父类构造函数
circle construct
shapestatic //静态方法不能重写
circlenormal //多态
shape
circlestatic //强转之后静态函数是子类的
circlenormal
circle //变量也是子类的
shpe constructor
circle construct
circlestatic
circlenormal
circle

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/764187.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号