Object oriented inheritance in Java (1)
Java object oriented inheritance
introduction
The concept of inheritance also exists in our lives.
Lao Liu is Lao Wang's good friend. Lao Liu can know many of Xiao Wang's characteristics through Lao Wang... Hahaha, there are a lot of features that can be compared with Java inheritance. The following program is actually slightly inappropriate because the pattern is too small when creating classes.. However, it will be easier for me to understand, and the following examples will be improved.
package com.my.pac11;
/**
* @author Summerday
* @date 2019/12/9 19:45
*/
//老王类
public class LaoWang {
private String lastName;//姓氏
public String address;//家庭住址
//super类构造器
public LaoWang() {
System.out.println("未知信息老王创建");
}
public LaoWang(String address) {
this.address = address;
System.out.println("创建家住在" + this.address + "王xx");
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public void doGoodThings() {
System.out.println(this.getLastName() + "做好事……");
}
public void drink() {
System.out.println("老王酗酒……");
}
//重写Object类的toString方法
public String toString(){
return "Object类是所有类的直接父类或间接父类";
}
}
//老李类
class LaoLi {
public LaoLi() {
System.out.println("我是老李!");
}
}
//LaoWang 是LittleXiaoWang的间接父类
class LittleXiaoWang extends XiaoWang {
//没有构造器会报错,子类必须第一步调用父类构造器
LittleXiaoWang(String n,String a) {
super(n,a);
}
//重写XiaoWang的方法,继承谁就改谁的
public void drink() {
System.out.println(this.getLastName() + "不喜欢喝可乐,喜欢雪碧……");
}
}
//一个类最多只有一个直接父类 ,可以有很多间接父类
//false: class XiaoWang extends LaoLi,LaoWang{...}
class XiaoWang extends LaoWang {
//子类构造器
XiaoWang(String lastName,String address) {
//super调用父类构造器
super(address);
setLastName(lastName);
}
//重写父类的方法
public void drink() {
System.out.println(this.getLastName() + "不酗酒,喜欢喝可乐……");
}
public static void main(String[] args) {
//先调用父类适合的构造器
//创建家住在浙江王xx
XiaoWang xw = new XiaoWang("小王","浙江");
//false:System.out.println(l.lastName);
System.out.println(xw.getLastName() + "," + xw.address);//小王,浙江
//做好事……
xw.doGoodThings();
//小王不酗酒,喜欢喝可乐……
xw.drink();
//自动调用toString方法
System.out.println(xw);
System.out.println("********************");
//依旧先调用父类适合的构造器
//创建家住在浙江王xx
LittleXiaoWang lxw = new LittleXiaoWang("小小王","浙江");
System.out.println(lxw.getLastName() + "," + lxw.address);//小小王,浙江
//小小王做好事……
lxw.doGoodThings();
//小小王不喜欢喝可乐,喜欢雪碧……
lxw.drink();
}
}
//测试
创建家住在浙江王xx
小王,浙江
小王做好事……
小王不酗酒,喜欢喝可乐……
Object类是所有类的直接父类或间接父类
********************
创建家住在浙江王xx
小小王,浙江
小小王做好事……
小小王不喜欢喝可乐,喜欢雪碧……
Characteristics of inheritance
Syntax format
[修饰符] class Subclass extends Superclass
{
//类定义部分
}
Parent child relationship
XiaoWang xw = new XiaoWang("小王","浙江");
//false:System.out.println(l.lastName);
System.out.println(xw.getLastName() + ",浙江
//下面的语句错误!
class XiaoWang extends LaoLi,LaoWang{...}
//重写Object类的toString方法
public String toString(){
return "Object类是所有类的直接父类或间接父类";
}
Inheritance points
In addition to the following, there are many, many to be added!
Override parent method
Subclasses can add new member variables or methods based on the parent class. You can also override the method of the parent class, that is, the same method name plus the parameter list, but you need to define different behaviors. (rewrite in the next chapter...)
//父类的方法
public void drink() {
System.out.println("老王酗酒……");
}
//重写父类的方法
public void drink() {
System.out.println(this.getLastName() + "不酗酒,喜欢喝可乐……");
}
Constructor in inheritance
The constructor in inheritance will be explained in detail next, which is temporarily missing.
//super类构造器
public LaoWang() {
System.out.println("未知信息老王创建");
}
public LaoWang(String address) {
this.address = address;
System.out.println("创建家住在" + this.address + "王xx");
}
//子类构造器
XiaoWang(String lastName,String address) {
//super调用父类构造器
super(address);
setLastName(lastName);
}
Super keyword in inheritance
Super keyword is used to limit the instance variable or method inherited by the object's parent class. Super cannot appear in static statement. (added later.)
//super调用父类构造器
LittleXiaoWang(String n,String a) {
super(n,a);
}
...
Note: there are some contents to be added in this article. Although the following contents are covered, thinking about nibbling knowledge bit by bit, so please forgive me and add them at the first time!
Reference book: Crazy Java handout