国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java重要知識點(diǎn)(繼承、多態(tài)、接口,異常,工具,測試)

2019-11-11 06:50:29
字體:
供稿:網(wǎng)友

重載、多態(tài)、接口

多態(tài)調(diào)用的順序

當(dāng)父類,和子類有Static時(shí),先初始化Static,再初始化子類的Static,再初始化父類的其他成員變量->父類構(gòu)造方法->子類其他成員變量->子類的構(gòu)造方法。

重寫 hashcode equals compareto

Employee.java

import java.util.Date;import java.util.GregorianCalendar;import java.util.Objects;public class Employee implements Comparable<Employee> { PRivate String name; private double salary; private Date hireDay; public Employee(String n, double s, int year, int month, int day) { name = n; salary = s; GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day); hireDay = calendar.getTime(); } public String getName() { return name; } public double getSalary() { return salary; } public Date getHireDate() { return hireDay; } public boolean equals(Object Otherobject) { if (this == Otherobject) return true; if (Otherobject == null) return false; // if(! Otherobject instanceof Employee) return false; if (getClass() != Otherobject.getClass()) return false; Employee other = (Employee) Otherobject; return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay); } public int hashCode() { return Objects.hash(name, salary); } public String toString() { return getClass().getName() + "name=" + name + ",salary=" + salary + ",hireDay=" + hireDay; } @Override public int compareTo(Employee other) { // if(getClass() != other.getClass()) throw new ClassCastException(); return Double.compare(salary, other.salary); }}

Manager.java

public class Manager extends Employee { private double bonus; public Manager(String n, double s, int year, int month, int day) { super(n, s, year, month, day); bonus = 0; } public void setBonus(double b) { bonus = b; } @Override public double getSalary() { double baseSalary = super.getSalary(); return bonus + baseSalary; } @Override public boolean equals(Object Otherobject) { if (!super.equals(Otherobject)) return false; Manager other = (Manager) Otherobject; return bonus == other.bonus; } @Override public int hashCode() { return super.hashCode() + 17 * new Double(bonus).hashCode(); } @Override public String toString() { return super.toString() + ",bonus=" + bonus; }}

異常

涉及return finally 以及異常拋出的問題

這里寫圖片描述 這里寫圖片描述

各個異常出現(xiàn)的情況

package exp;import java.awt.Font;import java.awt.FontFormatException;import java.awt.Graphics;import java.awt.Image;import java.awt.Toolkit;import java.beans.Statement;import java.io.EOFException;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.nio.ByteBuffer;import java.sql.Connection;import java.sql.DriverManager;import java.util.Stack;public class AllExcept { public static void main(String[] args) throws Exception { // 01 空指針異常 java.lang.NullPointerException try { int[] s = null; int i = s.length; } catch (Exception e) { System.out.println("1、" + e.getClass().getName() + "異常"); } // 02數(shù)組越界 java.lang.ArrayIndexOutOfBoundsException異常 try { int[] s1 = new int[2]; s1[2] = 5; } catch (Exception e) { System.out.println("2、" + e.getClass().getName() + "異常"); } // 03 錯誤類型的對象存儲到一個對象數(shù)組時(shí)拋出的異常 java.lang.ArrayStoreException異常 try { Object x[] = new String[3]; x[0] = new Integer(0); } catch (Exception e) { System.out.println("3、" + e.getClass().getName() + "異常"); } // 04 除數(shù)為0異常 java.lang.ArithmeticException異常 try { int a = 0; int b = 10; b /= a; } catch (Exception e) { System.out.println("4、" + e.getClass().getName() + "異常"); } // 05 數(shù)據(jù)類型轉(zhuǎn)換異常 java.lang.NumberFormatException異常 try { String s2 = "19k"; int n = Integer.parseInt(s2); } catch (Exception e) { System.out.println("5、" + e.getClass().getName() + "異常"); } // 06 超過字符串添加長度異常 java.lang.StringIndexOutOfBoundsException異常 try { StringBuffer buffer = new StringBuffer(); buffer.charAt(5); } catch (Exception e) { System.out.println("6、" + e.getClass().getName() + "異常"); } // 07 數(shù)組大小為負(fù)值異常。 java.lang.NegativeArraySizeException異常 try { int i = -1; int[] s = new int[i]; } catch (Exception e) { System.out.println("7、" + e.getClass().getName() + "異常"); } // 08 正則表達(dá)式異常 java.util.regex.PatternSyntaxException異常 try { String a = "//";// 錯誤的正則表達(dá)式 String a1 = "//.";// 正確的正則表達(dá)式 String[] x5 = "abc.cde".split(a1); x5 = "abc.cde".split(a); } catch (Exception e) { System.out.println("8、" + e.getClass().getName() + "異常"); } // 09 空棧異常 java.util.EmptyStackException異常 try { Stack s = new Stack(); s.pop(); } catch (Exception e) { System.out.println("9、" + e.getClass().getName() + "異常"); } // 10 找不到類異常 java.lang.ClassNotFoundException異常 try { Object s = Class.forName("ClassName"); } catch (Exception e) { System.out.println("10、" + e.getClass().getName() + "異常"); } // 11非法參數(shù)異常 // 12 字體格式錯誤異常 try { Font f = new Font("楷體", Font.BOLD, 7); f.createFont(2, new File("1.txt")); } catch (FontFormatException e) { System.out.println("11、" + e.getClass().getName()); } catch (IllegalArgumentException e) { System.out.println("12、" + e.getClass().getName() + "異常"); } // 13不支持畫圖異常 try { Image img = Toolkit.getDefaultToolkit().getImage("Images/1.jpg"); Graphics g = img.getGraphics(); g.drawRect(-1, -1, -1, -1); } catch (Exception e) { System.out.println("13、" + e.getClass().getName() + "異常"); } // 14 BufferOverflowException try { int cap = 2; ByteBuffer bf = ByteBuffer.allocate(cap); for (int i = 0; i < cap; i++) { bf.put((byte) i); } bf.put((byte) 10); bf.getInt(9); } catch (Exception e) { System.out.println("14、" + e.getClass().getName() + "異常"); } // 15文件找不到異常 // 16.當(dāng)發(fā)生某種 I/O 異常時(shí),拋出IOException // 17.文件已結(jié)束異常EOFException try { File f = new File("E://1.txt"); byte[] buff = new byte[2]; FileInputStream file = new FileInputStream(f); int bytes = file.read(buff, 0, 2); System.out.println(new String(buff, 0, 0, bytes)); file.close(); } catch (FileNotFoundException e) { System.out.println("15、" + e.getClass().getName() + "異常"); } catch (EOFException e) { System.out.println("16、" + e.getClass().getName() + "異常"); } catch (IOException e) { System.out.println("17、" + e.getClass().getName() + "異常"); } // 18 ClassCastException異常 try { Object s = new Integer(0); System.out.println((String) s); } catch (Exception e) { System.out.println("18、" + e.getClass().getName() + "異常"); } // 19 BufferUnderflowException異常 try { ByteBuffer buf = ByteBuffer.allocate(50); buf.flip(); byte[] byt = new byte[100]; buf.get(byt); new RuntimeException(); } catch (Exception e) { System.out.println("19、" + e.getClass().getName() + "異常"); } // 20 java.sql.SQLException異常 try { String sql = "select * from Table";// sql語句錯誤 Connection conn = DriverManager.getConnection("com.MySQL.jdbc.Driver"); Statement smt = (Statement) conn.createStatement(); smt.execute(); } catch (Exception e) { System.out.println("20、" + e.getClass().getName() + "異常"); } }}

工具

git svn 的 區(qū)別 和 常用命令

Maven依賴中的scope

測試

JUnit

Die.java

public class Die{ private int faceValue; private int numofsides; public Die(){ faceValue = 0; numofsides = 6; } public Die(int v){ numofsides = v; } public int numOfSides(){ return numofsides; } public int topFace(){ return faceValue; } public void roll() { faceValue = (int) (1 + Math.random() * numofsides ); }}

TestDie.java

import org.junit.After;import org.junit.Assert;import org.junit.Test;import org.junit.Before;/** * Unit test for simple App. */public class TestDie { private Die die1,die2; @Before public void setUp() { die1 = new Die(); die2 = new Die(10); die1.roll(); die2.roll(); } @Test public void testdie1numofsides() { Assert.assertEquals(6, die1.numOfSides()); } @Test public void testdie1Face() { Assert.assertTrue(die1.topFace() >= 1 && die1.topFace() <= 6); } @Test public void testdie2numofsides() { Assert.assertEquals(10, die2.numOfSides()); } @Test public void testdie2Face() { Assert.assertTrue(die2.topFace() >= 1 && die2.topFace() <= 10); } @After public void tearDown() { }}

實(shí)現(xiàn) assertEquals aseertSame

assertEquals.java

當(dāng)expected和actual都是String類型時(shí),ComparisonFailure還會找出是前后相同的串,并用[Different String]標(biāo)明那些不相同的字符串,也就是expectedToString和actualToString的格式將會變成:…${sameString}[${differentString}]${sameString}…。其中“…”只會在相同的字符串太長的情況下才會出現(xiàn),這個長度標(biāo)準(zhǔn)目前(JUnit4.10)是20個字符。具體實(shí)現(xiàn)參考ComparisonFailure類,它繼承自AssertionError,這里不再展開。

/** * Asserts that two objects are equal. If they are not, an * {@link AssertionError} is thrown with the given message. If * <code>expected</code> and <code>actual</code> are <code>null</code>, * they are considered equal. * * @param message * the identifying message for the {@link AssertionError} (<code>null</code> * okay) * @param expected * expected value * @param actual * actual value */ static public void assertEquals(String message, Object expected, Object actual) { if (expected == null && actual == null) return; if (expected != null && isEquals(expected, actual)) return; else if (expected instanceof String && actual instanceof String) { String cleanMessage= message == null ? "" : message; throw new ComparisonFailure(cleanMessage, (String) expected, (String) actual); } else failNotEquals(message, expected, actual); } private static boolean isEquals(Object expected, Object actual) { return expected.equals(actual); } /** * Asserts that two objects are equal. If they are not, an * {@link AssertionError} without a message is thrown. If * <code>expected</code> and <code>actual</code> are <code>null</code>, * they are considered equal. * * @param expected * expected value * @param actual * the value to check against <code>expected</code> */ static public void assertEquals(Object expected, Object actual) { assertEquals(null, expected, actual); }

aseertSame.java

/** * Asserts that two objects refer to the same object. If they are not, an * {@link AssertionError} is thrown with the given message. * * @param message * the identifying message for the {@link AssertionError} (<code>null</code> * okay) * @param expected * the expected object * @param actual * the object to compare to <code>expected</code> */ static public void assertSame(String message, Object expected, Object actual) { if (expected == actual) return; failNotSame(message, expected, actual); } /** * Asserts that two objects refer to the same object. If they are not the * same, an {@link AssertionError} without a message is thrown. * * @param expected * the expected object * @param actual * the object to compare to <code>expected</code> */ static public void assertSame(Object expected, Object actual) { assertSame(null, expected, actual); }

其他

hamcrest 可以用來增強(qiáng) JUnit 中的 assert 功能Cobertura is a free Java tool that calculates the percentage of code accessed by tests.stub & mock. EasyMock has been the first dynamic Mock Object generator, relieving users of hand-writing Mock Objects, or generating code for themDbUnit is a JUnit extension (also usable with Ant) targeted at database-driven projects that,among other things, puts your database into a known state between test runs

本文轉(zhuǎn)自:http://blog.csdn.net/u011239443/article/details/53150159


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 芦溪县| 丰都县| 建瓯市| 旬阳县| 独山县| 昔阳县| 汶川县| 綦江县| 黄梅县| 山东省| 葵青区| 花垣县| 定日县| 廉江市| 满洲里市| 松潘县| 阿克苏市| 通州区| 宁河县| 汝南县| 乌海市| 洞头县| 都江堰市| 页游| 望都县| 苏尼特右旗| 灵山县| 巴彦县| 正蓝旗| 绩溪县| 柳林县| 嘉黎县| 涿州市| 祥云县| 巫山县| 婺源县| 方正县| 余姚市| 墨江| 抚远县| 阿合奇县|