小编And*_*tib的帖子

向下转换为间接子类不起作用?

        public class A
    {

      public void printA(){
        System.out.println("A");  
      }

    }


    public class B extends A
    {
       public void printB(){
        System.out.println("B");  
      }

    }

    public class C extends B
    {
       public void printC(){
        System.out.println("C");  
      }

    }

    public class test {
        public static void main(String[] args)
      {

     A  a = new B();
          a.printA(); // work

        B b = (B) a;
          b.printB(); // work

          C c = (C) b;
          c.printC(); // not work throw  java.lang.ClassCastException

      }
}
Run Code Online (Sandbox Code Playgroud)

我有三个A类和B类,C
C从B延伸,B从A延伸到A
为什么从A到B,而不是从B到C,尽管它们之间的关系如A和B,B是父的C那么JVM怎么工作?

java inheritance casting multiple-inheritance downcast

0
推荐指数
1
解决办法
67
查看次数

解释从ContextCompat类中使用getColor()方法的主要原因?

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);
int color = ContextCompat.getColor(getContext(), mColorResourceId);
linearLayout.setBackgroundColor(color);
Run Code Online (Sandbox Code Playgroud)

我有这些代码: mColorResourceId它是hold R.color.category_numbers- >mColorResourceId = R.color.category_numbers

当我mColorResourceId直接传递给setBackgroundColor(mColorResourceId);它时,尽管方法接受int值,但它不会改变颜色.

我的问题为什么我需要这个额外的步骤int color = ContextCompat.getColor(getContext(), mColorResourceId);来改变颜色?

android converter getcolor

0
推荐指数
1
解决办法
735
查看次数