1.方法重写概述及其应用
* A:什么是方法重写
* 重写:子父类出现了一模一样的方法(注意:返回值类型可以是子父类)
* B:方法重写的应用:
* 当子类需要父类的功能,而功能主体子类有自己特有内容时,可以重写父类中的方法。这样,即沿袭了父类的功能,又定义了子类特有的内容。
手机类:
ios7系统 siri speak English
ios8系统 siri 说中文
class Demo7_Phone {
public static void main(String[] args) {
Ios8 i = new Ios8();
i.siri();
i.call();
}
}
/*
ios7系统 siri speak English
ios8系统 siri 说中文
*/
class Ios7 {
public void call() {
System.out.println("打电话");
}
public void siri() {
System.out.println("speak English");
}
}
class Ios8 extends Ios7 {
public void siri() {
System.out.println("说中文");
super.siri();
}
}
2.方法重写的注意事项
* a:父类中私有方法不能被重写
* 因为父类私有方法子类根本就无法继承
* b:子类重写父类方法时,访问权限不能更低
* 最好就一致
* c:父类静态方法,子类也必须通过静态方法进行重写
* 其实这个算不上方法重写,但是现象确实如此,至于为什么算不上方法重写,[多态](静态只能覆盖静态)
* 子类重写父类方法的时候,最好声明一模一样。
3.方法重写的面试题
Override和Overload的区别?Overload能改变返回值类型吗? * 子类对象调用方法的时候: |
4.使用继承前的学生和老师案例
* 属性:姓名,年龄
* 行为:吃饭
* 老师有特有的方法:讲课
* 学生有特有的方法:学习
class Test3_Person {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
class Student {
private String name; //姓名
private int age; //年龄
public Student() {} //空参构造
public Student(String name,int age) { //有参构造
this.name = name;
this.age = age;
}
public void setName(String name) { //设置姓名
this.name = name;
}
public String getName() { //获取姓名
return name;
}
public void setAge(int age) { //设置年龄
this.age = age;
}
public int getAge() { //获取年龄
return age;
}
public void eat() { //吃饭
System.out.println("学生吃饭");
}
public void study() { //学习
System.out.println("学生学习");
}
}
class Teacher {
private String name; //姓名
private int age; //年龄
public Teacher() {} //空参构造
public Teacher(String name,int age) { //有参构造
this.name = name;
this.age = age;
}
public void setName(String name) { //设置姓名
this.name = name;
}
public String getName() { //获取姓名
return name;
}
public void setAge(int age) { //设置年龄
this.age = age;
}
public int getAge() { //获取年龄
return age;
}
public void eat() { //吃饭
System.out.println("老师吃饭");
}
public void teach() { //学习
System.out.println("老师讲课");
}
}
使用继承后的学生和老师案例
* 属性:姓名,年龄
* 行为:吃饭
* 老师有特有的方法:讲课
* 学生有特有的方法:学习
class Test4_Person {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("张三");
s1.setAge(23);
System.out.println(s1.getName() + "..." + s1.getAge());
s1.eat();
s1.study();
System.out.println("------------------");
Student s2 = new Student("李四",24);
System.out.println(s2.getName() + "..." + s2.getAge());
s2.eat();
s2.study();
}
}
/*
* 使用继承后的学生和老师案例
*/
class Person {
private String name; //姓名
private int age; //年龄
public Person() {} //空参构造
public Person(String name,int age) { //有参构造
this.name = name;
this.age = age;
}
public void setName(String name) { //设置姓名
this.name = name;
}
public String getName() { //获取姓名
return name;
}
public void setAge(int age) { //设置年龄
this.age = age;
}
public int getAge() { //获取年龄
return age;
}
public void eat() { //吃饭
System.out.println(name + "吃饭");
}
}
class Student extends Person {
public Student() {} //空参构造
public Student(String name,int age) {
super(name,age);
}
public void study() {
System.out.println(this.getName() + "学习");
}
}
class Teacher extends Person {
public Teacher() {} //空参构造
public Teacher(String name,int age) {
super(name,age);
}
public void teach() {
System.out.println(this.getName() + "讲课");
}
}
5.猫狗案例继承版
* 属性:毛的颜色,腿的个数
* 行为:吃饭
* 猫特有行为:抓老鼠catchMouse
* 狗特有行为:看家lookHome
class Test5_Animal {
public static void main(String[] args) {
Cat c1 = new Cat("花",4);
System.out.println(c1.getColor() + "..." + c1.getLeg());
c1.eat();
c1.catchMouse();
Dog d1 = new Dog("黑",2);
System.out.println(d1.getColor() + "..." + d1.getLeg());
d1.eat();
d1.lookHome();
}
}
/*
* A:猫狗案例分析
* B:案例演示
* 猫狗案例继承版
* 属性:毛的颜色,腿的个数
* 行为:吃饭
* 猫特有行为:抓老鼠catchMouse
* 狗特有行为:看家lookHome
*/
class Animal {
private String color; //毛的颜色
private int leg; //腿的个数
public Animal(){}
public Animal(String color,int leg) {
this.color = color;
this.leg = leg;
}
public void setColor(String color) { //设置颜色
this.color = color;
}
public String getColor() { //获取颜色
return color;
}
public void setLeg(int leg) { //设置腿的个数
this.leg = leg;
}
public int getLeg() { //获取腿的个数
return leg;
}
public void eat() { //吃饭
System.out.println("吃饭");
}
}
class Cat extends Animal {
public Cat() {} //空参构造
public Cat(String color,int leg) { //有参构造
super(color,leg);
}
public void eat() { //吃鱼
System.out.println("猫吃鱼");
}
public void catchMouse() { //抓老鼠
System.out.println("抓老鼠");
}
}
class Dog extends Animal {
public Dog() {} //空参构造
public Dog(String color,int leg) { //有参构造
super(color,leg);
}
public void eat() { //吃肉
System.out.println("狗吃肉");
}
public void lookHome() { //看家
System.out.println("看家");
}
}