Java inheritance
1. The concept of inheritance
1. Concept
- Inheritance is a cornerstone of java object-oriented programming technology because it allows
创建分等级层次的类
.- Inheritance is
子类继承父类的特征和行为
to make the subclass object (instance) have the instance domain and methods of the parent class, or the subclass inherits methods from the parent class, so that子类具有父类相同的行为
.- Simple inheritance can be understood as: For the classes that have been put into use, try not to modify them. It is recommended to define a new class, come
重复利用其中共性内 容
, and you can add and change the required information in the new class.
The child class will have the general characteristics of the parent class, as well as its own characteristics
父类
Also基类、超类
子类
known as:派生类
Two, simple inheritance case code
1. Format
class 父类 {
}
class 子类 extends 父类 {
}
2. Simple inheritance relationship
(1) Case analysis
Lecturers and teaching assistants all belong to the teacher, and the information shared by the lecturer and teaching assistant is extracted as the teacher parent. The lecturer class and the teaching assistant class inherit the teacher class as subclasses.
(2) Coding implementation
//将 讲师 和 助教 共有的信息抽取出来,编写 教师 父类
class Teacher {
String name;
String sex;
}
//编写 讲师 类并继承 教师 类,并且添加自己独有的行为(方法)讲课
class Lecturer extends Teacher{
public void lecture(){
System.out.println("讲师讲课");
}
}
//编写 助教 类并继承 教师 类,并且添加自己独有的行为(方法)辅导
class Assistant extends Teacher{
public void coach(){
System.out.println("助教辅导");
}
}
Three, detailed inheritance knowledge collation
1. Three types of inheritance
1. The java language is
单继承
that a class can only inherit from one class
2. The java language can be多重继承
3. One子类的父类只有一个
, but one父类可以有多个子类
2. Inherited features
(1) Subclasses can have
非private的
properties and methods of the parent class
(2) Subclasses can have their own properties and methods, that is, the subclass can对父类进行扩展
(3) The subclass can implement the methods of the parent class in its own way(重写Override)
.
(4) Java inheritance is single inheritance, but multiple inheritance is possible. This is a feature that distinguishes Java inheritance from C++ inheritance.
(5) Improve the coupling between classes (the shortcomings of inheritance, high coupling will cause the closer the connection between the codes, the worse the code independence).
3. Inherited keywords
(1) extends keyword
In Java, class inheritance is single inheritance, that is,,
一个子类只能拥有一个父类
soextends 只能继承一个类
.
java.lang.Object类是所有类的最高公共父类
, if a class has no inherited keywords, it inherits the Object class by default
public class Person {
String name;
String sex;
public void eat() {
}
public void sleep() {
}
}
public class Student extends Person{
//即使子类不编写代码,也会用拥有父类所有非private的属性和方法
}
(2) implements keyword
Use the features that
implements 关键字
can be disguised, and使java具有多继承
the scope of use is the case where the class inherits the interface, yes同时继承多个接口
(the interface and the interface are separated by a comma).
public interface A {
public void aa();
public void bb();
}
public interface B {
public void aa();
}
public class C implements A,B {
}
(3) super keyword
Pass
super关键字来实现对父类成员的访问
, used to refer to the parent class of the current object.
class A{
int a;
public void a(){
}
}
class B extends A{
int a;
public void a(){
}
public void test(){
this.a = 11;//调用自己的成员变量
super.a = 22;//调用父类的成员变量
this.a();//调用自己的成员方法
super.a();//调用父类的成员方法
}
}
(4) Final keyword
final 关键字声明类
The class can be defined as不能继承的
the final class.
final class 类名{
}
final 关键字修饰方法
,this method不能被子类重写.
权限修饰符 final 返回值类型 方法名(){
}
4.重写(override)
Rewrite: override (overwrite, overwrite)
In inheritance, the method name of the subclass and the parent class are the same, and the parameter list is also the same, but
方法体不同
it is called method rewriting.
class Fu {
public void method(){
System.out.println("这是父类方法");
}
}
public class Zi extends Fu {
//Override注解可以检测重写方法是否符合规范,可以不写但是不推荐
@Override
public void method(){
System.out.println("这是子类方法");
}
}
5. Construction method
1.
子类构造方法中默认带有 super()调用父类构造方法
, so it must be先调用父类构造方法
,后执行子类构造方法
2. Can passThe super keyword calls the overloaded structure of the parent class in the subclass
3.父类构造方法的调用必须是子类构造方法的第一条语句
,一个子类构造方法只能调用一个父类构造方法
4.Only the subclass construction method can call the parent class construction method
class ConstructorTest {
public static void main(String[] args) {
Zi z = new Zi();
}
}
//运行结果为:
父类无参构造方法
父类有参构造方法
子类构造方法
The code of the parent class and the child class↓ ↓ ↓
class Fu {
public Fu(){
System.out.println("父类无参构造方法");
}
public Fu(int num){
this();//使用this关键字调用其它构造方法
System.out.println("父类有参构造方法");
}
class Zi extends Fu {
public Zi(){
//super();可以不写,编译器默认包含,如果调用了其它构造方法,则默认super()失效
super(20);
System.out.println("子类构造方法");
}
}
【note】
1.
子类构造方法必须继承一个父类构造方法
2. There is an inheritance relationship, the compilerInherit the parent class construction method by default
3.子类默认构造方法和子类中的super()都为隐式
(Can be called without using the super keyword)
4. If you wantCall other construction methods of the parent classthenMust be called with the super keyword
5. If you want调用两个父类构造方法
, you must在父类另外一个构造方法中,使用this关键字
call other construction methods
6. Access to member variables between parent and child classes
//定义父子类
class Fu {
int numFu = 10;
int num = 100;//这是父子类重名变量
}
class Zi extends Fu{
int numZi = 10;//这是父子类重名变量
int num = 200;
}
The knowledge points are in the notes
public class FieldTest {
public static void main(String[] args) {
//首先对子类父类创建对象,以供下文比较
Fu f = new Fu();//父类对象
Zi z = new Zi();//子类对象
//使用子类对象和父类对象,都可以输出父类的numFu
System.out.println(f.numFu);
System.out.println(z.numFu);
//但是只能使用子类对象输出numZi
System.out.println(z.numZi);
//如果父子类存在同名变量时
//等号左侧是哪个类,就会使用哪个类的成员变量,
//如果找不到继续向上(父类)寻找
System.out.println(f.num);//输出父类num
System.out.println(z.num);//输出子类num
//父类不会知道谁继承了它,但是子类一定知道继承了谁
}
}
7. Access to member methods between parent and child classes
子类方法的访问权限
You must大于等于
父类方法的权限修饰符
子类方法的返回值类型
be小于等于
父类方法的返回值类型
class Fu {
public void methodFu(){
System.out.println("这是父类方法");}
//这是父子类重名方法
Object method(){
System.out.println("这是父类重名方法");
return null;
}
}
class Zi extends Fu {
public void methodZi(){
System.out.println("这是子类方法");}
//这是父子类重名方法
@Override
public String method(){
//子类的访问权限(public)大于父类的访问权限(default)
//子类的返回值类型(String)小于父类的返回值类型(Object)
String str = "这是子类重名方法";
return null;
}
}
The knowledge points are in the notes
public class MethodTest {
public static void main(String[] args) {
Zi z = new Zi();//创建子类对象
z.methodFu();//父类对象访问父类方法
z.methodZi();//子类对象访问子类方法
//使用子类对象访问父子类重名方法时
//创建的对象是谁就优先使用谁,找不到就继续向上(父类)寻找
String str = z.method();
//使用的子类对象所以首先寻找子类中的 method()
}
}