zoukankan      html  css  js  c++  java
  • 静态方法

    先看题:

    class Base {
    private String name = "Base";
    public void method() {
    System.out.println("In Base method...");
    }
    public static void staticmethod() {
    System.out.println("In Base staticmethod...");
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    }

    class Sub extends Base {
    private String name = "Sub";
    public void method() {
    System.out.println("In Sub method...");
    }
    public static void staticmethod() {
    System.out.println("In Sub staticmethod...");
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    }

    public class Test {
    public static void main(String[] args) {
    Base ob = new Sub();
    System.out.println(ob.getName());
    ob.method();
    ob.staticmethod();
    }
    }

    运行结果:

    Sub
    In Sub method...
    In Base staticmethod...

    之所以这样是静态方法不存在重写覆盖的问题。看你申明变量时是什么类型的,就调用这个类型的方法

    Base ob = new Sub();类型是Base类型所以调用base的静态方法。


    阅读全文
    类别:Java 查看评论

  • 相关阅读:
    SQL补充
    SQL练习题
    HDU 2907
    Codeforces 814D
    Codeforces 814C
    Codeforces 1004D
    Codeforces 1004E
    CodeForces 909F
    CodeForces 909E
    CodeForces 909D
  • 原文地址:https://www.cnblogs.com/zwl24/p/2356805.html
Copyright © 2011-2022 走看看