先来看下方法重载(Overloading)的定义:如果有两个方法的方法名相同,但参数不一致,哪么可以说一个方法是另一个方法的重载。 具体说明如下:
以下实例演示了如何重载 MyClass 类的 info 方法:
class MyClass { int height; MyClass() { System.out.println("无参数构造函数"); height = 4; } MyClass(int i) { System.out.println("房子高度为 " + i + " 米"); height = i; } void info() { System.out.println("房子高度为 " + height + " 米"); } void info(String s) { System.out.println(s + ": 房子高度为 " + height + " 米"); } } public class MainClass { public static void main(String[] args) { MyClass t = new MyClass(3); t.info(); t.info("重载方法"); //重载构造函数 new MyClass(); } }
房子高度为 3 米 房子高度为 3 米 重载方法: 房子高度为 3 米 无参数构造函数
方法重载与方法覆盖区别如下:
以下实例演示了 Java 方法覆盖(Overriding)代码的实现:
public class Findareas{ public static void main (String []agrs){ Figure f= new Figure(10 , 10); Rectangle r= new Rectangle(9 , 5); Figure figref; figref=f; System.out.println("Area is :"+figref.area()); figref=r; System.out.println("Area is :"+figref.area()); } } class Figure{ double dim1; double dim2; Figure(double a , double b) { dim1=a; dim2=b; } Double area() { System.out.println("Inside area for figure."); return(dim1*dim2); } } class Rectangle extends Figure { Rectangle(double a, double b) { super(a ,b); } Double area() { System.out.println("Inside area for rectangle."); return(dim1*dim2); } }
以上代码运行输出结果为:
Inside area for figure. Area is :100.0 Inside area for rectangle. Area is :45.0
import java.io.*; public class Main { public static void main(String[] args) { try { BufferedWriter out = new BufferedWriter(new FileWriter("outfilename")); out.write("w3cschool"); out.close(); System.out.println ("文件创建成功!"); } catch (IOException e) { } } }
import java.io.*; public class Main { public static void main(String[] args) { try { BufferedReader in = new BufferedReader (new FileReader("test.log")); String str; while ((str = in.readLine()) != null) { System.out.println(str); } System.out.println(str); } catch (IOException e) { } } }
import java.io.*; public class Main { public static void main(String[] args) { try{ File file = new File("c:\\test.txt"); if(file.delete()){ System.out.println(file.getName() + " 文件已被删除!"); }else{ System.out.println("文件删除失败!"); } }catch(Exception e){ e.printStackTrace(); } } }
import java.net.InetAddress; import java.net.UnknownHostException; public class GetIP { public static void main(String[] args) { InetAddress address = null; try { address = InetAddress.getByName ("www.w3cschool.cc"); } catch (UnknownHostException e) { System.exit(2); } System.out.println(address.getHostName() + "=" + address.getHostAddress()); System.exit(0); } }
以上代码运行输出结果为:
www.w3cschool.cc=222.73.134.120
import java.net.*; import java.io.*; public class Main { public static void main(String[] args) { Socket Skt; String host = "localhost"; if (args.length > 0) { host = args[0]; } for (int i = 0; i < 1024; i++) { try { System.out.println("查看 "+ i); Skt = new Socket(host, i); System.out.println("端口 " + i + " 已被使用"); } catch (UnknownHostException e) { System.out.println("Exception occured"+ e); break; } catch (IOException e) { } } } }
以上代码运行输出结果为:
…… 查看 17 查看 18 查看 19 查看 20 查看 21 端口 21 已被使用 查看 22 查看 23 查看 24
import java.net.InetAddress; public class Main { public static void main(String[] args) throws Exception { InetAddress addr = InetAddress.getLocalHost(); System.out.println("Local HostAddress: "+addr.getHostAddress()); String hostname = addr.getHostName(); System.out.println("Local host name: "+hostname); } }
以上代码运行输出结果为:
Local HostAddress: 192.168.1.4 Local host name: harsh
import java.net.URL; import java.net.URLConnection; public class Main { public static void main(String[] argv) throws Exception { int size; URL url = new URL("http://www.w3cschool.cc/wp-content/themes/w3cschool.cc/assets/img/newlogo.png"); URLConnection conn = url.openConnection(); size = conn.getContentLength(); if (size < 0) System.out.println("无法获取文件大小。"); else System.out.println("文件大小为:" + + size + " bytes"); conn.getInputStream().close(); } }
以上代码运行输出结果为:
文件大小为:4261 bytes
import java.io.IOException; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class MultiThreadServer implements Runnable { Socket csocket; MultiThreadServer(Socket csocket) { this.csocket = csocket; } public static void main(String args[]) throws Exception { ServerSocket ssock = new ServerSocket(1234); System.out.println("Listening"); while (true) { Socket sock = ssock.accept(); System.out.println("Connected"); new Thread(new MultiThreadServer(sock)).start(); } } public void run() { try { PrintStream pstream = new PrintStream (csocket.getOutputStream()); for (int i = 100; i >= 0; i--) { pstream.println(i + " bottles of beer on the wall"); } pstream.close(); csocket.close(); } catch (IOException e) { System.out.println(e); } } }
以上代码运行输出结果为:
Listening Connected