zoukankan      html  css  js  c++  java
  • 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);
     }
    }

  • 相关阅读:
    12款响应式的 jQuery 旋转木马(传送带)插件
    CSS预处理器实践之Sass、Less大比拼[转]
    jQuery学习笔记
    7件你不知道但可以用CSS做的事
    纯js页面跳转整理
    JavaScript中this的工作原理以及注意事项
    CSS Hack大全-可区分出IE6-IE10、FireFox、Chrome、Opera
    为现代JavaScript开发做好准备
    15 个最佳的 jQuery 表格插件
    全栈式JavaScript
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3015993.html
Copyright © 2011-2022 走看看