继承 super
protected //受保护的属性
private//私有的东西无法被继承
package oop.Demo01.demo05;
public class Application {
public static void main(String[] args) {
Student student = new Student();//Person无参执行了、Student无参执行了
student.test("月月");
student.test1();
}
}
/*
Person无参执行了
Student无参执行了
月月
滔滔
冉耘滔
Student
Student
Person
*/
package oop.Demo01.demo05;
//在Java中,所有的类,都默认直接或者间接继承object
//Person 父类
public class Person /*extends object*/ {
public Person(String name) {
System.out.println("Person无参执行了");
}
protected String name = "冉耘滔";
public void print(){
System.out.println("Person");
}
}
package oop.Demo01.demo05;
//学生 is 人 子类 派生类
//子类继承了父类 就会拥有父类的全部方法
public class Student extends Person {
public Student() {
//隐藏代码:调用了父类的无参构造
super("冉");//调用父类的构造器,必须在子类构造器的第一行
System.out.println("Student无参执行了");
}
private String name = "滔滔";
public void print(){
System.out.println("Student");
}
public void test1(){
print();//Student
this.print();//Student
super.print();//Person
}
public void test(String name){
System.out.println(name);//月月
System.out.println(this.name);//滔滔
System.out.println(super.name);//冉耘滔
}
}
super注意点:
1.super调用父类的构造方法,必须在构造方法的第一个
2.super必须只能出现在之类的方法或者构造方法中
3.suoer和this 不能同时调用构造方法
Vs this:
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的应用
前提
this:没有继承也可以使用
super:只能在继承条件才可以使用
构造方法
this();本类的构造
super();父类的构造
方法的重写
package oop.Demo01.demo05;
import oop.Demo01.demo05.A;
public class Application {
//静态的方法和非静态的方法区别很大
//静态方法:方法的调用只和左边,定义的数据类型有关
//非静态:重写
public static void main(String[] args) {
//方法的调用只和左边,定义的数据类型有关
A a = new A();
a.test();//A=>test()
//父类的引用指向了子类
B b = new A(); //子类重写了父类的方法
b.test();//B=>test()
}
}
package oop.Demo01.demo05;
//重写都是方法的重写,和属性无关
public class B {
public void test(){
System.out.println("B=>test()");
}
}
package oop.Demo01.demo05;
public class A extends B {
//Override 重写
@Override//注解:有功能的注释!
public void test() {
System.out.println("A=test()");
}
}
重写:需要有继承关系:子类重写父类的方法
1.方法名必须相同
2.参数列表必须相同
3.修饰符:范围可以扩大不能缩小: public>protected>Default>private
4.抛出的异常:范围,可以被缩小,但不能扩大: ClassNotFoundException-->Exception(大)
重写:子类的方法和父类的的必须一致;方法体不同
为什么需要重写:
1.父类的功能,子类不一定需要,或者不一定满足
Alt + Insert ; override;
多态
动态编译:类型:可扩展性
即同一方法可以根据发送对象的不同而采用多种不同的行为方式
一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多
- 多态存在的条件
- 有继承关系
- 子类重写父类方法
- 父类引用指向子类对象
**注意:**多态是方法的多态,属性没有多态性。
inctanceof
package oop.Demo01.demo06;
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student();
//new Person();
//可以指向的引用类型就不确定了
//Student 能调用的方法都是自己的或者继承父类的
Student s1 = new Student();
//Person 父类 可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
s2.run();//子类重写了父类的方法,执行子类的方法
s1.run();//son
s1.eat();
((Student)s2).eat();
}
}
package oop.Demo01.demo06;
public class Person {
public void run(){
System.out.println("run");
}
}
/*
多态注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系 类型转换异常 ClassCastException
3.存在的条件:继承关系,方法需要重写,父类的引用指向子类对象
不能重写的方法:
1.static 方法,属于类,它不属于实例
2.final 常量;
3.private 方法;
*/
package oop.Demo01.demo06;
public class Student extends Person{
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
*/
package oop.Demo01.demo06;
public class Student extends Person{
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}