• Java homework


    Java homework

    NO.1: Generics(泛型)

    package cn.com.hrbust.Test;

    public class Generics {
     public static void main(String[]args) {
      Chorus<Singer, Instrument>model = new Chorus<Singer,Instrument>();
      Singer JJ = new Singer();
      Instrument piano = new Instrument();
      model.makeChorus(JJ, piano);
     }
    }
    class Chorus<E,F> {
     void makeChorus(E person,F instrument) {
      person.toString();
      instrument.toString();
     }
    }
    class Singer {
     public String toString() {
      System.out.println("lalallala");
      return "";
     }
    }
    class Instrument {
     public String toString() {
      System.out.println("|455|8784|");
      return "";
     }
    }

    No.2:Exception(异常)

    package cn.com.hrbust.Test;

    public class Exception {
     public static void main(String[]args) {
      B b = new B();
      try {
       b.f(100);
       b.f(-9);
      } catch (MyException e) {
       System.out.println(e.getMessage());
      }
     }
    }

    class MyException extends Throwable {
     String message;
     MyException(int n) {
      message = n+"不是正数";
     }
     public String getMessage() {
      return message;
     }
    }
    class B{
     public void f(int n) throws MyException {
      if(n<0) {
       MyException e = new MyException(n);
       throw(e);
      }
      double number = Math.sqrt(n);
      System.out.println(n+"的平方根:"+number);
     }
    }

    No.3:内部类VS外部类

    package nice;

    public class InnerClassDemo {
     public static void main(String[]args) {
    //  Outer o = new Outer();
    //  Outer.Inner inner = o.new Inner();
    //   inner.display();
      Outer outer = new Outer();
      outer.inst();
     }
    }
    class Outer {
     int score = 100;
     void inst() {
      class Inner {
       void display() {
        System.out.println("score is "+score);
       }
      }
      Inner inner = new Inner();
      inner.display();
     }
    // class Inner {
    //  String name = "zhangsan";
    //  void display() {
    //   System.out.println("score is "+score);
    //  }
     }
    // public void print() {
    //  Inner inner = new Inner();
    //  System.out.println(inner.name);
    // }


     NO.4:与接口有关的内部类

    package cn.com.hrbust.Test;


    interface Show {
     public void show();
    }
    class A {
     void f(Show s) {
      s.show();
     }
    }
    public class Interface {
     public static void main(String[]args) {
      A a = new A();
      a.f(new Show() {
       public void show() {
        System.out.println("Hello");
       }
      });
     }
    }

    NO.5:Integer。parseInt and Integer.toString

    package cn.com.hrbust.Test;

    public class Test {
     public static void main(String[]args)throws NumberFormatException
     {
      System.out.println(Integer.parseInt("285527")+1);
      System.out.println(Integer.toString(123)+1);
     }
    }

  • 相关阅读:
    Tita 360 评估:新员工转正评估模版
    360反馈实例实例:如何给他人的弱点进行反馈
    数据库无法查询中文问题的解决过程
    sql server中判断表或临时表是否存在的方法
    5G网络
    Sqlserver中使用DBLINK
    Oracle 11g 安装图文教程
    SqlServer的Pivot和Unpivot用法
    ORACLE常用傻瓜問題1000問
    SqlServer的PIVOT、UNPIVOT将行转成列,列传成行
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3015993.html
Copyright © 2020-2023  润新知