class Demo7_Phone { public static void main(String[] args) { Ios8 i = new Ios8(); i.siri(); i.call(); } } /* B:方法重写的应用: * 当子类须要父类的功能,而功能主体子类有本身特有内容时,能够重写父类中的方法。这样,即沿袭了父类的功能,又定义了子类特有的内容。 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(); } }
A:方法重写注意事项ios
c:父类静态方法,子类也必须经过静态方法进行重写面试
子类重写父类方法的时候,最好声明如出一辙。ide
class Demo8_双桨 { public static void main(String[] args) { DayOne d = new DayOne(); d.泡妞(); d.print(); } } /* * a:父类中私有方法不能被重写 * 由于父类私有方法子类根本就没法继承 * b:子类重写父类方法时,访问权限不能更低 * 最好就一致 * c:父类静态方法,子类也必须经过静态方法进行重写 * 其实这个算不上方法重写,可是现象确实如此,至于为何算不上方法重写,多态中我会讲解(静态只能覆盖静态) * 子类重写父类方法的时候,最好声明如出一辙。 */ class 双桨 { public void sing() { System.out.println("唱红歌"); } public void 泡妞() { System.out.println("唱红歌搞定林夕合鸟女士"); } public static void print() { System.out.println("Fu print"); } } class DayOne extends 双桨 { public void 泡妞() { System.out.println("霸王硬上弓"); } public static void print() { //静态只能覆盖静态,其实不算重写,多态时候详细讲解 System.out.println("Zi print"); } }
A:方法重写的面试题学习
方法重写:子类中出现了和父类中方法声明如出一辙的方法。与返回值类型有关,返回值是一致(或者是子父类)的this
方法重载:本类中出现的方法名同样,参数列表不一样的方法。与返回值类型无关。spa
子类对象调用方法的时候:code
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() + "讲课"); } }
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("看家"); } }
###08.19_面向对象(final关键字修饰类,方法以及变量的特色)(掌握)
* A:final概述
* B:final修饰特色
* 修饰类,类不能被继承
* 修饰变量,变量就变成了常量,只能被赋值一次
* 修饰方法,方法不能被重写
* C:案例演示
* final修饰特色对象
class Demo1_Final { public static void main(String[] args) { Son s = new Son(); s.print(); } } /* * A:final概述 final是最终的 * B:final修饰特色 * 修饰类,类不能被继承 * 修饰变量,变量就变成了常量,只能被赋值一次 * 修饰方法,方法不能被重写 * C:案例演示 * final修饰特色 */ /*final class Father { public void print() { System.out.println("访问底层数据资源"); } }*/ class Son /*extends Father*/ { final int NUM = 10; //常量命名规范,若是是一个单词,全部字母大写,若是是多个单词,每一个单词都大写,中间用下划线隔开 public static final double PI = 3.14; //final修饰变量叫作常量,通常会与public static共用 public void print() { //NUM = 20; System.out.println(NUM); } }
###08.20_面向对象(final关键字修饰局部变量)(掌握)
* A:案例演示
* 方法内部或者方法声明上都演示一下(了解)
* 基本类型,是值不能被改变
* 引用类型,是地址值不能被改变,对象中的属性能够改变继承
class Demo2_Final { public static void main(String[] args) { final int num = 10; //num = 20; System.out.println(num); final Person p = new Person("张三",23); //p = new Person("李四",24); p.setName("李四"); p.setAge(24); System.out.println(p.getName() + "..." + p.getAge()); method(10); method(20); } public static void method(final int x) { System.out.println(x); } } /* * A:案例演示 * 方法内部或者方法声明上都演示一下(了解) * 基本类型,是值不能被改变 * 引用类型,是地址值不能被改变,对象中的属性能够改变 */ 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; } }
###08.21_面向对象(final修饰变量的初始化时机)(掌握)
* A:final修饰变量的初始化时机
* 显示初始化
* 在对象构造完毕前便可资源
class Demo3_Final { public static void main(String[] args) { Demo d = new Demo(); d.print(); } } /* * A:final修饰变量的初始化时机 * 显示初始化 * 在对象构造完毕前便可 */ class Demo { final int num; //成员变量的默认初始化值是无效值 public Demo() { num = 10; } public void print() { System.out.println(num); } }