方法同名时:方法重载,方法覆盖
同一类内的方法同名:方法重载
子类与父类方法同名:方法覆盖
//在包person下 //Person.java package person; public class Person { public Person(String name, int age) { super(); this.name = name; this.age = age; } protected String name; protected int age; public String getInfo() { return "姓名:"+name+",年龄:"+age+" "; } } //Student.java package person; public class Student extends Person { protected String school; protected String grade; Student(String name,int age,String school,String grade){ super(name,age); this.school=school; this.grade=grade; } public String getInfo() { return super.getInfo()+"学校:" +school+" ,年级:"+grade; } } //Demo.java package person; public class Demo { public static void main(String[] args) { // TODO Auto-generated method stub Person person =new Person("小张",18); System.out.println(person.getInfo()); Student student=new Student("小李",20,"天津大学","大一"); System.out.println(student.getInfo()); } }
输出结果:
满足方法覆盖的原则:
方法覆盖只存在于子类和父类之间(包括直接父类和间接父类)
子类方法签名和返回值类型必须和父类一致(访问权限必须是大于等于父类的访问权限)
父类的静态方法是不能被子类覆盖为非静态方法
父类的非静态方法也不能被子类覆盖为静态方法
父类的私有方法不能被子类覆盖
final
含义是最终的,final修饰变量时,表示该变量是常量,其值不可再变化
final修饰方法,表示该方法为终极方法,可以重载,但不能被子类重写,
final修饰类,表示该类是最终类,不可被继承
修饰变量是final用的最多的地方
1,修饰基本数据类型变量,其数值在初始化后不能更改
2,修饰引用数据类型变量,则在初始化后不能再指向另一个对象,但能修改原引用对象的属性值
package finaldemo; public class Demo { public final double PI=3.14; public final int[] arr= {1,2,3}; public final String str = "Welcome to Java"; public static void main(String[] args) { // TODO Auto-generated method stub Demo demo = new Demo(); demo.PI=3.14159; //error demo.arr=new int[5]; //error demo.arr[2]=4; //right demo.str="that's a error!"; //error System.out.println(demo.PI); for(int i=0;i<3;i++) { System.out.print(demo.arr[i]+" "); } System.out.println(); System.out.println(demo.str); } }
对象转型
除了基础数据类型的转型之外,对象领域里面也有对象之间的转型
分为两种:向上转型:父类类型引用指向子类对象
向下转型:父类类型的引用变量强制转化成子类类型
向上:父类类型 父类引用变量 = new 子类类型();
Person p = new Student();
系统自动完成,父类引用变量可以调用被子类重写的所有方法,但子类独有方法不可用
向下:
子类类型 子类引用变量 =(父类类型) 父类引用变量 //必须要有继承关系
使用instanceof运算符来检测一个对象是否是一个类的实例
//package Demo2 下 //Animal.java package Demo2; public class Animal { public Animal(String name) { super(); this.name = name; } String name; } //Cat.java package Demo2; public class Cat extends Animal { String eyeColor; public Cat(String name,String eyeColor) { super(name); // TODO Auto-generated constructor stub this.eyeColor=eyeColor; } } //Dog.java package Demo2; public class Dog extends Animal { String furColor; public Dog(String name,String furColor) { super(name); // TODO Auto-generated constructor stub this.furColor=furColor; } } //Demo.java package Demo2; public class Demo { public static void main(String[] args) { // TODO Auto-generated method stub Animal animal = new Animal("animal"); Cat cat =new Cat("cat","blue"); Dog dog = new Dog("dog","black"); //判断animal,cat,dog是否为Animal System.out.println("animal is Animal? "+ (animal instanceof Animal)); System.out.println("cat is Animal? "+ (cat instanceof Animal)); System.out.println("dog is Animal? "+ (dog instanceof Animal)); animal=dog; System.out.println(animal.name); System.out.println("animal is Dog? "+ (animal instanceof Dog)); } }
输出结果:
好累·········