1. Introduction
1. Grammar
A extends B
A: Subclass/Derived Class
B: parent class/base class/super class
2. Function:
Extract the commonality of multiple classes to achieve code reuse
Such as: Cat class and Dog class, both need to have a name attribute, eating method, you can write an Animal class, the code is as follows
Write an Animal class
class Animal{
String name;
public void eat(){
System.out.println(this.name+"正在吃饭");
}
}
Write a Cat class
class Cat extends Animal{//继承Animal的属性和方法
public void mew(){
System.out.println(this.name+"喵喵喵");//可以写自己的方法
}
}
Write a Dog class
class Dog extends Animal{//继承Animal的属性和方法
public void bark(){
System.out.println(this.name+"汪汪汪");//写自己的方法
}
}
Second, the subclass inherits the member properties/methods of the parent class
1. Access method
It has become the subclass's own member properties/methods, which can be accessed directly in the method, or through object name.property/object name.method, just like subclasses accessing their own member properties/methods
2. Code demo
class A{
int a;
int b;
public void func1(){
System.out.println("父类的方法");
}
}
class B extends A{
int c;
{
a = 100; //访问继承的属性a
c =200;//访问自己的变量
}
}
public class TestDemo {
public static void main(String[] args) {
B b = new B();
System.out.println(b.a);//访问继承的属性a
b.func1();//访问继承下来的func1方法
}
}
3. When the member attribute/method name of the subclass is the same as that of the parent class
1. Access sequence
When the subclass has the same member attribute/method name as the parent class, it will give priority to access its own, which is called the proximity principle.
2. Code demo
class A{
int a = 100;
public void func(){
System.out.println("父类的方法");
}
}
class B extends A{
int c;
int a = 200; //与父类同名的属性a
public void func(){ //与父类同名的方法func()
System.out.println("子类的方法");
}
}
public class TestDemo {
public static void main(String[] args) {
B b = new B();
System.out.println(b.a);//访问同名的属性
b.func();//访问同名的方法
}
}
The result calls the a of the subclass and the func() method of the subclass
Fourth, the super keyword
1. Function
When the subclass member attribute/method names are the same, the subclass's own is called by default. If we want to call the parent class, we need to use the super keyword.
2. Grammar
super.data //Call the properties of the parent class
super.func() //Call the method of the parent class
super() //Call the constructor of the parent class
3. Code demo
class A{
int a =100;
public void func(){
System.out.println("父类的方法");
}
}
class B extends A{
int c;
int a =200; //与父类同名的属性a
public void func(){ //与父类同名的方法func()
System.out.println("子类的方法");
}
public void method(){
System.out.println(super.a);//调用父类的成员属性
super.func(); //调用父类的成员方法
}
}
public class TestDemo {
public static void main(String[] args) {
B b = new B();
b.method();
}
}
4. Pay attention
1. The super keyword is the same as the this keyword and can only be used in non-static methods
2. super is used to access the methods and properties inherited from the parent class
3. this is a hidden parameter of a non-static member method, super is not a hidden parameter
5. Construction method
1. When the parent class writes a parameterless constructor
Subclasses will generate a constructor by default
public subclass name { super();
}
class Animal{
String name;
public void eat(){
System.out.println(this.name+"正在吃饭");
}
public Animal(){
System.out.println("父类的构造方法");
}
}
class Cat extends Animal{
public void mew(){
System.out.println(this.name+"喵喵喵");
}
// public Cat(){ //默认生成的
// super(); //调用父类的构造方法
// }
}
public class TestDemo {
public static void main(String[] args) {
Cat cat = new Cat(); //当实例化对象的时候,就会调用Cat类的构造方法
}
}
2. The parent class writes a constructor with parameters
When constructing a subclass, the subclass needs to help the parent class construct first, so that the code can run normally
Solution:
Write a construction method and call the construction method of the parent class on the first line. Note that super() must be on the first line
class Cat extends Animal{
public Cat(){
super("咪咪"); //显示调用父类的构造方法,来初始化子类从父类继承过来的东西
}
}
Six, talk about initialization
1. Execution sequence
In the code blocks mentioned before, the order of execution is static code block > instance code block > construction code block
In the inheritance relationship, the execution order is still static code block > instance code block > construction code block, but the parent class always takes precedence over the child class
2. Code demo
class Person{
{
System.out.println("Person:实例代码块");
}
static {
System.out.println("Person:静态代码块");
}
public Person(){
System.out.println("Person:构造代码块");
}
}
class Student extends Person{
{
System.out.println("Student:实例代码块");
}
static {
System.out.println("Student:静态代码块");
}
public Student(){
System.out.println("Student:构造代码块");
}
}
public class Test {
public static void main(String[] args) {
Student student1 = new Student();
Student student2 = new Student();
}
}