A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 牛德阳 中级黑马   /  2015-12-20 21:30  /  539 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package day10;
/*练习1,写出程序结果
public class A {
public static void func() //throws Exception
{
   try
   {
    throw new Exception();
   }
   finally
   {
    System.out.println("B");
   }
}
public static void main(String[] args)
{
     try
     {
      func();
      System.out.println("A");
     }
     catch(Exception e)
     {
      System.out.println("C");
     }
     System.out.println("D");
}
}
编译失败,如果在func上声明了该异常,结果是B C D*/
/*练习2 写出程序结果
interface A{}
class B implements A
{
      public String func()
      {
             return "func";
      }
}
class Demo
{
      public static void main(String[] args)
      {
          A a=new B();
         
           System.out.println(a.func());
      }
}
编译失败:因为A接口从未定义func方法
*/
/*练习3 写出程序结果
class A
{
      boolean show(char a)
      {
       System.out.println(a);
       return true;
      }
}
public class B extends A
{
public static void main(String[] args)
{
  int i=0;
  A a=new B();
  B b=new B();
  for(a.show('A');a.show('B')&&(i<2);a.show('C'))
  {
    i++;
    b.show('D');
  }
}
  boolean show(char a)
      {
       System.out.println(a);
       return false;
      }
}
运行结果A B 调用子类的Show 方法:a.show(B)==false
*/

/*练习4
写出程序运行结果
class TD
{
       int y=6;
       class Inner
       {
            static int y=3;
            void show()
            {
                System.out.println(y);
            }
       }
}
class TC
{
   public static void main(String[] args)
   {
       TD.INner ti=new TD().new Inner();
       ti.show();
   }
}

编译失败,非静态类内部不可以定义静态成员
内部类如果定义了静态成员,该内部类必须被静态修饰。
*/
/*练习5写出错误答案错误的原因
class demo
{
   int show(int a,int b){return 0;}
}
下面哪些函数可以存在在Demo的子类中
public int show(int a,int b){ return 0;}//可以,覆盖
private int show(int a,int b){ return 0;}//不可以,权限不够
private int show(int a,long b){ return 0;}//可以相当于重载
static int show(int a,int b) { return 0;}//不可以静态只能覆盖静态
*/

/*练习6 以下程序运行结果
class Fu
{
     int num=4;
     void show()
     {
         System.out.println("showFu");      
     }
     
  class Zi extends Fu
  {
     int num=5;
     void show()
     {
         System.out.println("showzi");  
     }   
  }
  
  class T
  {
     public static void main(String[] args)
     {
        Fu f=new Zi();
        Zi z=new Zi();
         System.out.println("f.num");  
          System.out.println("z.num");  
          f.show();
          z.show();
     }
  }
}
运行结果是4 5 showZi showZi,变量不会被覆盖,成员变量看左边 ,方法调用覆盖的方法
*/
/*练习7写出程序运行结果
class Demo
{
  public static void func(){
    try
    {
       throw new Exception();
       System.out.println("A");
    }
    catch(Exception e)
    {
     System.out.println("B");
    }
}
  public static void main(String[] args)
  {
  try
  {
   func();
  }
  catch(Exception e)
  {
   System.out.println("C");
  }
  System.out.println("D");
  }
}
编译失败因为不能执行到System.out.println("A");
注意:throw单独存在,下面不要定义语句,因为执行不到
*/
/*练习8 写出程序结果
class Exc0 extends Exception{}
class Exc1 extends Exc0{}

*class Demo
{
    public static void main(String[] args)
    {
      try
      {
        throw new Exc1();
      }
      catch(Exception e)
      {
      System.out.println("Exception");
      }
      catch(Exc0 e)
      {
      System.out.println("Exc0");
      }
    }
}
编译失败因为,父类的catch要放在最下面
*/









0 个回复

您需要登录后才可以回帖 登录 | 加入黑马