Java 重写与重载区别 Java父类的Object方法 要重写tostring原因,多个catch处理逻辑

来源:互联网 发布:新浪财经数据库 编辑:程序博客网 时间:2024/05/19 03:17

Java 重写与重载区别 Java父类的Object方法 要重写tostring原因,多个catch处理逻辑 

package com.xuan.demo01;/** *  * 重写方法的规则: * 1、参数列表必须完全与被重写的方法相同,否则不能称其为重写而是重载。2、返回的类型必须一直与被重写的方法的返回类型相同,否则不能称其为重写而是重载。3、访问修饰符的限制一定要大于被重写方法的访问修饰符(public>protected>default>private)4、重写方法一定不能抛出新的检查异常或者比被重写方法申明更加宽泛的检查型异常。例如:父类的一个方法申明了一个检查异常IOException,在重写这个方法是就不能抛出Exception,只能抛出IOException的子类异常,可以抛出非检查异常。而重载的规则:1、必须具有不同的参数列表;2、可以有不责骂的返回类型,只要参数列表不同就可以了;3、可以有不同的访问修饰符;4、可以抛出不同的异常; * */public class TestDemo003 extends TestDemo003_P{private String string002="hello";protected String string003="helloccc";public String string004="hellocccc333";String testStr="test";public static void main(String[] args) {String aString="hello";// create one objectString string=new String("abc");//create two objectTestDemo003 testDemo003=new TestDemo003();/* * 学习Java的人都知道,Java所有类都是object的子类 * protected Object clone()创建并返回此对象的一个副本。 boolean equals(Object obj)指示其他某个对象是否与此对象“相等”。 protected void finalize()当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。 Class<?> getClass()返回此 Object 的运行时类。 int hashCode()返回该对象的哈希码值。 void notify()唤醒在此对象监视器上等待的单个线程。 void notifyAll()唤醒在此对象监视器上等待的所有线程。 String toString()返回该对象的字符串表示。 void wait()在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待。 void wait(long timeout)在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过指定的时间量前,导致当前线程等待。 void wait(long timeout, int nanos)在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者其他某个线程中断当前线程,或者已超过某个实际时间量前,导致当前线程等待。 */Class<TestDemo003> class1=TestDemo003.class;//为什么要重写object的toString()方法System.out.println(testDemo003.toString());//com.xuan.demo01.TestDemo003@1540e19d //类名@hascode值,输出的不是我们想要的结果,所以需要重写来打印object的详细信息//TestDemo003 j9=new TestDemo003_P();//编译不通过TestDemo003_P p=new TestDemo003();System.out.println(p.testStr);System.out.println(p.string003);System.out.println(p.hashCode());/** * 找到有异常,抛出相关异常 * 捕获相关的异常去处理,不相关的异常不会捕获与自己无关的异常 * 例如:运算异常,只会给运算异常,他不会给其他无关的异常捕获 * Exception 是所以异常的集合, * 这里没有声明数组越界异常捕获,但它还是会给Exception捕获的 * 这里声明了运算异常,所以就给具体的异常捕获了,而不是给Exception捕获的 */String kString=null;        int ss[]={1,2};try {    System.out.println(19/0);    System.out.println(ss[8]);} catch ( NullPointerException e) {// TODO Auto-generated catch blockSystem.out.println("opopopop1111");e.printStackTrace();}catch (ArithmeticException e) {System.out.println("opopopop2222");// TODO Auto-generated catch blocke.printStackTrace();}catch (Exception e) {System.out.println("opopopop22223333");// TODO Auto-generated catch blocke.printStackTrace();}finally {System.out.println("dsfsdfsd");}}public TestDemo003() {// TODO Auto-generated constructor stub}public void reload(String name,int age){System.out.println("重载父类的reload方法,参数个数,参数类型必须不同,返回类型可以不同,修饰符可以不一样");}public void rewrite(String name,int age){System.out.println("重写父类方法 ");}} class TestDemo003_P{private String string002="hellopppp";protected String string003="hellopppp111";public String string004="hellopppp3333";String testStr="testpppp";public void reload(String name){System.out.println("被子类重载");}public void rewrite(String name,int age){System.out.println("被子类重写 基本一模一样");}}


0 0
原创粉丝点击