flash企业网站源码,丹徒网站建设机构,app生成器手机版,wordpress图片批量设置重写#xff08;Override#xff09; 重写是子类重写父类的方法#xff0c;如果重写了父类的方法#xff0c;访问时父类的方法就会被覆盖#xff0c;如果想要再访问父类的同名方法#xff0c;要用super关键字。重写的好处在于子类可以根据自己的需要#xff0c;定义特定…重写Override
重写是子类重写父类的方法如果重写了父类的方法访问时父类的方法就会被覆盖如果想要再访问父类的同名方法要用super关键字。重写的好处在于子类可以根据自己的需要定义特定于自己的行为。重写的规则 参数列表必须与被重写的方法完全相同返回类型必须与被重写的方法的返回类型完全相同访问权限不能比父类的访问权限更低。例如父类的一个方法被声明为public那么子类中重写该方法就不能声明为protected。父类的成员方法只能被它的子类重写。声明为final的方法不能被重写。声明为static的方法不能被重写但是能被再次声明。子类和父类在同一个包中那么子类可以重写父类所有方法除了声明为private和final的方法。子类和父类不在同一个包中那么子类可以重写父类声明为public和protected和非final的方法。重写的方法能够抛出任何非强制异常无论被重写的方法是否抛出异常。但是重写的方法不能抛出新的强制异常或者比被重写方法声明的更广泛的强制性异常反之则可以。构造方法不能被重写。如果不能继承一个方法则不能重写这个方法。 实例如下class Animal {public void move() {System.out.println(动物可以移动);}
}class Dog extends Animal {public void move() {System.out.println(狗可以跑和走);}
}public class TestDog {public static void main(String args[]) {Animal a new Animal(); // Animal 对象Animal b new Dog(); // Dog 对象a.move();// 执行 Animal 类的方法b.move();// 执行 Dog 类的方法}
} 输出动物可以移动
狗可以跑和走子类重写的方法必须先在父类中存在同名的方法。 在上面的例子中可以看到尽管b属于Animal类型但是它运行的是Dog类的move方法。 这是由于在编译阶段只是检查参数的引用类型。 然而在运行时Java虚拟机(JVM)指定对象的类型并且运行该对象的方法。 因此在上面的例子中之所以能编译成功是因为Animal类中存在move方法然而运行时运行的是特定对象的方法。 再看下面的例子 pre namecode classhtmlclass Animal {public void move() {System.out.println(动物可以移动);}
}class Dog extends Animal {public void move() {System.out.println(狗可以跑和走);}public void bark() {System.out.println(狗可以吠叫);}
}public class TestDog2 {public static void main(String args[]) {Animal a new Animal(); // Animal 对象Animal b new Dog(); // Dog 对象a.move();// 执行 Animal 类的方法b.move();// 执行 Dog 类的方法b.bark();}
} 编译结果 TestDog.java:30: cannot find symbol
symbol : method bark()
location: class Animalb.bark(); 该程序抛出一个错误因为Animal类中没有bark方法。 Super关键字的使用 当需要在子类中调用父类被重写的方法是要使用super关键字。 class Bird {public void move() {System.out.println(小鸟会移动);}
}class Swallow extends Bird {public void move() {super.move(); // 应用super类的方法System.out.println(燕子可以飞);}
}public class TestSwallow {public static void main(String args[]) {Bird b new Swallow(); // Bird对象b.move(); // 执行Swallow类的方法}
} 输出 小鸟会移动
燕子可以飞下面给出一个单纯继承的例子public class TestCircle {public static void main(String[] args) {new Circle();}
}class Draw {public Draw(String type) {System.out.println(type draw constructor);}
}class Shape {private Draw draw new Draw(shape);public Shape() {System.out.println(shape constructor);}
}class Circle extends Shape {private Draw draw new Draw(circle);public Circle() {System.out.println(circle constructor);}
} 输出shape draw constructor
shape constructor
circle draw constructor
circle constructor要记住父类的构造器调用以及初始化过程一定在子类的前面。由于Circle类的父类是Shape类所以Shape类先进行初始化然后再执行Shape类的构造器。接着才是对子类Circle进行初始化最后执行Circle的构造器。 重载Overload 重载是在同一个类中方法的名字相同参数列表不同返回类型可以相同也可以不同。每个重载的方法或构造函数都必须有一个独一无二的参数列表。只能重载构造函数不能重写构造函数。重载规则 被重载的方法必须改变参数列表。被重载的方法可以改变返回类型。被重载的方法可以改变访问修饰符。被重载的方法可以声明新的或更广的检查异常。方法能够在同一个类中或者在一个子类中被重载。 public class Overloading {public int test() {System.out.println(test1);return 1;}public void test(int a) {System.out.println(test2);}// 以下两个参数类型顺序不同public String test(int a, String s) {System.out.println(test3);System.out.println(String.format(s, a));// String类的静态方法format()能用来创建可复用的格式化字符串而不仅仅是用于一次打印输出return returntest3;}public String test(String s, int a) {System.out.println(test4);return returntest4;}public static void main(String[] args) {Overloading o new Overloading();System.out.println(o.test());o.test(1);System.out.println(o.test(1, test3));System.out.println(o.test(test4, 1));}
} 输出 test1
1
test2
test3
returntest3
test4
returntest4重写和重载的区别 区别点 重载方法 重写方法 参数列表 必须修改 一定不能修改 返回类型 可以修改 一定不能修改 异常 可以修改 可以减少或删除一定不能抛出新的或者更广的异常 访问 可以修改 一定不能做更严格的限制可以降低限制 1. 重写是子类和父类之间的关系是垂直关系重载是同一类中方法之间的关系是水平关系。2.重写只能由一个方法或只能由一堆方法产生关系重载是多个方法之间的关系。3.重写要求参数列表相同重载要求参数列表不同。