自我感覺良好的程式碼書寫格式(獻醜了)
阿新 • • 發佈:2018-12-17
一直用傳統的程式碼書寫格式,感覺有的程式碼很亂,突發奇想,可不可以經追求技術,又追求藝術了,所以就自己在那瞎排了一個程式碼的書寫格式,目前只是初步的想法而已,,還需要很多改進;
小二,上程式碼(這段程式碼的內容是有關Java網路程式設計的,內容無所謂,咱們主要看書寫的格式)
package com.dong.Address; import java.net.InetAddress; import java.net.UnknownHostException; public class TestAddress { public void TInetAddress() throws UnknownHostException {InetAddress address = InetAddress.getByName("www.baidu.com"); SecurityManager sm = new SecurityManager(); sm.checkConnect("www.baidu.com", -1); sm.checkConnect("www.baidu.com",3); System.out.println(address); } public void TInetAddress2() {InetAddress address; try {address = InetAddress.getByName("61.135.169.125"); System.out.println(address.getHostName()); } catch (UnknownHostException e) { e.printStackTrace(); } } public void TInetAddress3() throws UnknownHostException {InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com"); for(InetAddress address : addresses) {System.out.println(address);} } public static void main(String args[]) {TestAddress ta = new TestAddress(); try { ta.TInetAddress(); ta.TInetAddress2(); ta.TInetAddress3(); } catch (UnknownHostException e) {e.printStackTrace(); } } }
package com.dong.Address; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.CookieHandler; import java.net.CookieManager; import java.net.CookiePolicy; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLDecoder; import java.net.URLEncoder; public class TestURL { public static void main(String args[]) throws IOException {URL url = new URL("https://www.baidu.com"); CookieManager manager = new CookieManager(); manager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER); CookieHandler.setDefault(manager); InputStream input = url.openStream(); OutputStream out = new FileOutputStream(new File("C:\\Users\\liumin\\Desktop\\baidu.txt")); byte [] b = new byte[1024]; while((input.read() != -1)) { input.read(b, 0, 1024); out.write(b); URLConnection uc= url.openConnection(); InputStream in = uc.getInputStream(); Object o = url.getContent(); String str = URLEncoder.encode("this String has spaces", "UTF-8"); System.out.println(str); System.out.println(URLDecoder.decode(str, "UTF-8")); } } }
感覺這樣書寫的方便之處在於:
函式首部和函式體錯開,可以清楚的知道此類有什麼方法,如果想註釋,可在左邊空白處註釋功能;
整個程式碼看起來很整潔
不足:需要在函式體中,必須有明確的程式碼塊區分,方便更迅速的知道各個{}的範圍;