zoukankan      html  css  js  c++  java
  • java,属性覆盖,方法覆盖

    class Rootb
    {
    int x = 1;


    public Rootb(int i)
    {
    }


    public int getI()
    {
    return x;
    }


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


    class Stemb extends Rootb
    {
    private int x = 2;


    public Stemb(int i)
    {
    super(i);
    }


    public int getI()
    {
    return x;
    }


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


    public class Test
    {
    public static void main(String args[])
    {
    Rootb stb = new Stemb(47);
    stb.x = 3;
    System.out.println(stb.x);
    System.out.println(((Stemb) stb).getI());


    System.out.println(stb.getI());
    System.out.println(((Stemb) stb).getI());


    stb.setI(5);
    System.out.println(stb.x);
    System.out.println(((Stemb) stb).getI());


    System.out.println(stb.getI());
    System.out.println(((Stemb) stb).getI());


    Stemb rootb = (Stemb) new Rootb(47);
    rootb.setI(3);
    System.out.println(rootb.getI());
    System.out.println(((Rootb) rootb).x);


    }

    }


    3
    2
    2
    2
    3
    5
    5
    5
    Exception in thread "main" java.lang.ClassCastException: Rootb cannot be cast to Stemb
    at Test.main(Test.java:59)

    结论:java属性不覆盖,方法覆盖。

  • 相关阅读:
    Android 手机摇一摇功能的实现
    Android 只开启一个Activity实例
    android 获取Datepicker日期
    帧动画
    进度条ProgressDialog
    AlertDialog错误
    ListView加checkBox可以实现全选等功能
    一些自己常用的工具类
    错层
    Parallax
  • 原文地址:https://www.cnblogs.com/daichangya/p/12958768.html
Copyright © 2011-2022 走看看