首页 > 代码库 > 5.18下午
5.18下午
Overview of the basic template of a Java class: package declaration import statements access specifier class ClassName { } access specifier is either "public", "private", "protected" or it can be unspecified. If it is unspecified then it has package visibility. We extended the Fraction class to have a equals() method and a toString() method. This was an example of method overriding. We talked about how every class is a subclass of java.lang.Object. We saw how the subclass (which automatically extends Object) will ineritet the toString() and equals() method from the parent class. So we saw an example of inheritence, and also overloading the inherited methods. Implemented a main method in a TestFraction class, for testing the Fraction test using assertions. Throw Exception if assertion fails. Since we throw new Exception() from inside the main method, main must specifiy "throws Exception" public static void main(String[] args) throws Exception { Fraction A = new Fraction(BigInteger.valueOf(1), BigInteger.valueOf(2); Fraction B = new Fraction(BigInteger.valueOf(1), BigInteger.valueOf(4); if (A.equals(B)) throw new Exception(); // assert false A.equals(B); } Also we discussed a few primitive types : int, long, float, double, boolean and mentioned the auto-boxing and auto-unboxing between boolean and Boolean. There are corresponding boxed types for int (Integer), long (Long), float (Float), double (Double) also. We covered 3 types of loops: for(;;){} (very convenient syntax for conditional interation) while(){} (can be used to implement same logic as a for loop) do{}while() (body of loop is always executed the first time) break (terminates the loop) continue (skips to next iteration)
5.18下午
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。