zoukankan      html  css  js  c++  java
  • 随堂笔记8

    随堂笔记8

    多态

    1. 多态就是方法重写,详见笔记7方法重写

    2. 注意点

      1. 属性没有多态

      2. 父类和子类有联系 (类型转换异常)ClassCastException

      3. 存在条件:继承关系,方法需要重写,父类引用指向子类 #Father f1=new Son();

      4. 不能重写的方法

        1. static 方法,属于类,不属于实例

        2. final常量

        3. private 私有的

    instanceof和类型转换

    1. instanceof判断对象和类是否有父子关系

    2. 对象类型看new关键词后边的类型(区分与方法的调用)

      import com.yyl.demo03.Person;
      import com.yyl.demo03.Student;
      import com.yyl.demo03.Teacher;

      public class Application {
         public static void main(String[] args) {
             //Object>String
             //Object>Person>Student
             //Object>Person>Teacher
             Person person=new Student();
             System.out.println(person instanceof Person);
             System.out.println(person instanceof Object);
             System.out.println(person instanceof Student);
             System.out.println(person instanceof Teacher);
             //System.out.println(person instanceof String);编译报错
        }
      }

    类型转换

    1. 类型可以自动从高转到低

    2. 强制转换

      import com.yyl.demo03.Person;
      import com.yyl.demo03.Student;

      public class Application {
         public static void main(String[] args) {
              //低转高可以自动转换student->Person
             Person student=new Student();
             //无法调用student.go();能调用的方法看最左边的定义,对象的类看最右边的定义
             Student student1=(Student)student;//强制转换为student类型
             student1.go();
             //或者((Student) student).go();
        }
      }
    3. 子类自动转化为父类,可能会丢失一些函数

    static

    1. static修饰符修饰的变量或者函数可以直接类调用:类名+函数名(变量名)

    2. 代码块

    3. static最先缓冲,所以内部不能调用非static数据

    抽象类

    1. 在一个类之前加abstract关键字就变为抽象类

    2. 在一个函数之前加abstract关键字变为抽象方法,抽象方法只有方法名字,没有方法实现

    3. 抽象类的子类都必须有重写该函数的方法

    4. java类单继承,但接口可以多继承

    5. 注意

      1. 抽象类不能实例化,不能new一个对象 ~约束

      2. 抽象方法只能在抽象类中

      3. 抽象类中可以有普通方法

      4. 抽象方法只是一种约束

    接口

    1. 关键词:interface #public interface 类名{ }

    2. 接口中全部为抽象方法

    3. 接口中的定义都是抽象的public(默认为public abstract,可以省略该部分)

    4. 实现接口的类关键词implements+接口名多个接口用逗号隔开

    5. 实现接口的类,就必须重写接口的方法

    6. 可以利用接口实现多继承

    7. 接口中可以定义常量,int a=99;默认省略public static final

      //接口1
      public interface UserService {
         void add(String name);
         void delete(String name);
         void update(String name);
         void query(String name);
      }
      //接口2
      public interface TimeService {
         void timer();
      }

       

      //实现类
      //类可以实现接口,通过 implements+接口
      public class UserServiceImpl implements UserService,TimeService{//完整版
      //快捷键alt+insert
         @Override
         public void timer() {

        }

         @Override
         public void add(String name) {

        }

         @Override
         public void delete(String name) {

        }

         @Override
         public void update(String name) {

        }

         @Override
         public void query(String name) {

        }
      }
    8. 接口小节

      1. 约束

      2. 定义一些方法,让不同的人去实现

      3. public abstract

      4. public static final(可省略)(只能声明常量)

      5. 接口不能被实例化,接口中没有构造方法

      6. implements可以实现多个接口,类中必须实现(重写)接口中所有方法

    n种内部类

    public class Outer {
       private int a=1;
       public void out(){
           System.out.println("这是外部类方法");
      }
       public class Inner{
           public void in(){
               System.out.println("这是内部类方法");
          }
           public void visit(){//获得外部类的私有属性
               System.out.println(a);
          }
      }
    }
    public class Application {
       public static void main(String[] args) {
         Outer s1=new Outer();
         s1.out();
         //通过外部类来实例化内部类
         Outer.Inner s2=s1.new Inner();
         s2.in();
         s2.visit();
      }
    }

    =======================================================================================

    匿名类

    public class Test {
       public static void main(String[] args) {
           //没有名字初始化,不用将实例保存到变量中
           new Apple().eat();
           new UserService(){
               @Override
               public void hello() {//重写hello接口的函数

              }
          };
      }
    }

    class Apple{
       public void eat(){
           System.out.println("1");
      }
    }
    interface UserService{
       void hello();
    }

    异常机制

    1. 异常的种类

    2. 异常的捕获

    3. 要捕获多个异常要从小到大捕获

      public class demo01 {
         public static void main(String[] args) {
             int a=1;
             int b=0;

             try{//try监控区域
                 System.out.println(a/b);
                 //new demo01().a();
                 if(b==0){//主动抛出异常 throw throws
                    throw new ArithmeticException();//主动抛出异常
                }
            }catch(ArithmeticException e){//catch捕获异常括号内为捕获异常的类型,最高级为Throwable
                 System.out.println("程序出现异常,变量b不能为0");
                 e.printStackTrace();//打印错误栈信息(和原本错误提示相同)
                 System.exit(0);
            }catch(Error e){
                 System.out.println("Error");
            }
             catch(Throwable e) {
                 System.out.println("Throwable");
            }finally {//处理善后工作
                 System.out.println("Final");
            }//可以不要finally

        }
         public void a(){
             b();
        }
         public void b(){
             a();
        }
      }
    4. 一般用在方法内抛出异常,方法处理不了异常可以进行异常上抛

    5. 所有异常都需要被捕获,否则程序停止

    public class Test {
       public static void main(String[] args) {
           //可以让程序发生不停止,继续运行
           try {
               new Test().test(1,0);
          } catch (ArithmeticException e) {
               e.printStackTrace();
          }
      }
       //假设方法无法处理这个异常,方法上抛出异常
       public void test(int a,int b) throws ArithmeticException{
           if(b==0){//throw
               throw new ArithmeticException();
          }
      }
    }

    自定义异常

    //测试异常
    public class Test {
       //可能会存在异常的方法
       static void test(int a) throws MyException{
           System.out.println("传入的数为"+a);
           if(a>10){
               throw new MyException(a);//抛出
          }
      }

       public static void main(String[] args) {
           try {
               test(11);
          } catch (MyException e) {//可以增加一些处理异常的代码
               System.out.println("My Exception=>"+e);
          }
      }
    }
    //自定义异常
    //首先继承异常类
    public class MyException extends Exception{
       //传递数字
       private int detail;

       public MyException(int detail) {
           this.detail = detail;
      }
       //toString

       @Override
       public String toString() {//异常的打印信息
           return "MyException{" +
                   "detail=" + detail +
                   '}';
      }
    }

     

  • 相关阅读:
    Jessica's Reading Problem POJ
    FatMouse and Cheese HDU
    How many ways HDU
    Humble Numbers HDU
    Doing Homework again
    Stacks of Flapjacks UVA
    Party Games UVA
    24. 两两交换链表中的节点
    面试题 03.04. 化栈为队
    999. 可以被一步捕获的棋子数
  • 原文地址:https://www.cnblogs.com/yylblog/p/13668694.html
Copyright © 2011-2022 走看看